blob: 1d000717279fb8b9f8f83567bedf3fca34640f71 [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 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200461 ['O' - 'A'] = {
462 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
463 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100464 ['P' - 'A'] = {
465 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
466 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
467 },
468 ['T' - 'A'] = {
469 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
470 },
471 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200472 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100473 */
474};
475
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100476const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100477 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
478 [HTTP_METH_GET] = { "GET", 3 },
479 [HTTP_METH_HEAD] = { "HEAD", 4 },
480 [HTTP_METH_POST] = { "POST", 4 },
481 [HTTP_METH_PUT] = { "PUT", 3 },
482 [HTTP_METH_DELETE] = { "DELETE", 6 },
483 [HTTP_METH_TRACE] = { "TRACE", 5 },
484 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
485};
486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100487/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200488 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100489 * RFC2616 for those chars.
490 */
491
492const char http_is_spht[256] = {
493 [' '] = 1, ['\t'] = 1,
494};
495
496const char http_is_crlf[256] = {
497 ['\r'] = 1, ['\n'] = 1,
498};
499
500const char http_is_lws[256] = {
501 [' '] = 1, ['\t'] = 1,
502 ['\r'] = 1, ['\n'] = 1,
503};
504
505const char http_is_sep[256] = {
506 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
507 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
508 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
509 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
510 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
511};
512
513const char http_is_ctl[256] = {
514 [0 ... 31] = 1,
515 [127] = 1,
516};
517
518/*
519 * A token is any ASCII char that is neither a separator nor a CTL char.
520 * Do not overwrite values in assignment since gcc-2.95 will not handle
521 * them correctly. Instead, define every non-CTL char's status.
522 */
523const char http_is_token[256] = {
524 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
525 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
526 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
527 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
528 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
529 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
530 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
531 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
532 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
533 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
534 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
535 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
536 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
537 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
538 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
539 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
540 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
541 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
542 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
543 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
544 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
545 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
546 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
547 ['|'] = 1, ['}'] = 0, ['~'] = 1,
548};
549
550
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100551/*
552 * An http ver_token is any ASCII which can be found in an HTTP version,
553 * which includes 'H', 'T', 'P', '/', '.' and any digit.
554 */
555const char http_is_ver_token[256] = {
556 ['.'] = 1, ['/'] = 1,
557 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
558 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
Thierry FOURNIER63d692c2015-02-28 19:03:56 +0100559 ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1,
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100560};
561
562
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100563/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100564 * Adds a header and its CRLF at the tail of the message's buffer, just before
565 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100566 * The header is also automatically added to the index <hdr_idx>, and the end
567 * of headers is automatically adjusted. The number of bytes added is returned
568 * on success, otherwise <0 is returned indicating an error.
569 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100570int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100571{
572 int bytes, len;
573
574 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200575 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100576 if (!bytes)
577 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100578 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100579 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
580}
581
582/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100583 * Adds a header and its CRLF at the tail of the message's buffer, just before
584 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100585 * the buffer is only opened and the space reserved, but nothing is copied.
586 * The header is also automatically added to the index <hdr_idx>, and the end
587 * of headers is automatically adjusted. The number of bytes added is returned
588 * on success, otherwise <0 is returned indicating an error.
589 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100590int http_header_add_tail2(struct http_msg *msg,
591 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100592{
593 int bytes;
594
Willy Tarreau9b28e032012-10-12 23:49:43 +0200595 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100596 if (!bytes)
597 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100598 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100599 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
600}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200601
602/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100603 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
604 * If so, returns the position of the first non-space character relative to
605 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
606 * to return a pointer to the place after the first space. Returns 0 if the
607 * header name does not match. Checks are case-insensitive.
608 */
609int http_header_match2(const char *hdr, const char *end,
610 const char *name, int len)
611{
612 const char *val;
613
614 if (hdr + len >= end)
615 return 0;
616 if (hdr[len] != ':')
617 return 0;
618 if (strncasecmp(hdr, name, len) != 0)
619 return 0;
620 val = hdr + len + 1;
621 while (val < end && HTTP_IS_SPHT(*val))
622 val++;
623 if ((val >= end) && (len + 2 <= end - hdr))
624 return len + 2; /* we may replace starting from second space */
625 return val - hdr;
626}
627
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200628/* Find the first or next occurrence of header <name> in message buffer <sol>
629 * using headers index <idx>, and return it in the <ctx> structure. This
630 * structure holds everything necessary to use the header and find next
631 * occurrence. If its <idx> member is 0, the header is searched from the
632 * beginning. Otherwise, the next occurrence is returned. The function returns
633 * 1 when it finds a value, and 0 when there is no more. It is very similar to
634 * http_find_header2() except that it is designed to work with full-line headers
635 * whose comma is not a delimiter but is part of the syntax. As a special case,
636 * if ctx->val is NULL when searching for a new values of a header, the current
637 * header is rescanned. This allows rescanning after a header deletion.
638 */
639int http_find_full_header2(const char *name, int len,
640 char *sol, struct hdr_idx *idx,
641 struct hdr_ctx *ctx)
642{
643 char *eol, *sov;
644 int cur_idx, old_idx;
645
646 cur_idx = ctx->idx;
647 if (cur_idx) {
648 /* We have previously returned a header, let's search another one */
649 sol = ctx->line;
650 eol = sol + idx->v[cur_idx].len;
651 goto next_hdr;
652 }
653
654 /* first request for this header */
655 sol += hdr_idx_first_pos(idx);
656 old_idx = 0;
657 cur_idx = hdr_idx_first_idx(idx);
658 while (cur_idx) {
659 eol = sol + idx->v[cur_idx].len;
660
661 if (len == 0) {
662 /* No argument was passed, we want any header.
663 * To achieve this, we simply build a fake request. */
664 while (sol + len < eol && sol[len] != ':')
665 len++;
666 name = sol;
667 }
668
669 if ((len < eol - sol) &&
670 (sol[len] == ':') &&
671 (strncasecmp(sol, name, len) == 0)) {
672 ctx->del = len;
673 sov = sol + len + 1;
674 while (sov < eol && http_is_lws[(unsigned char)*sov])
675 sov++;
676
677 ctx->line = sol;
678 ctx->prev = old_idx;
679 ctx->idx = cur_idx;
680 ctx->val = sov - sol;
681 ctx->tws = 0;
682 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
683 eol--;
684 ctx->tws++;
685 }
686 ctx->vlen = eol - sov;
687 return 1;
688 }
689 next_hdr:
690 sol = eol + idx->v[cur_idx].cr + 1;
691 old_idx = cur_idx;
692 cur_idx = idx->v[cur_idx].next;
693 }
694 return 0;
695}
696
Willy Tarreauc90dc232015-02-20 13:51:36 +0100697/* Find the first or next header field in message buffer <sol> using headers
698 * index <idx>, and return it in the <ctx> structure. This structure holds
699 * everything necessary to use the header and find next occurrence. If its
700 * <idx> member is 0, the first header is retrieved. Otherwise, the next
701 * occurrence is returned. The function returns 1 when it finds a value, and
702 * 0 when there is no more. It is equivalent to http_find_full_header2() with
703 * no header name.
704 */
705int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
706{
707 char *eol, *sov;
708 int cur_idx, old_idx;
709 int len;
710
711 cur_idx = ctx->idx;
712 if (cur_idx) {
713 /* We have previously returned a header, let's search another one */
714 sol = ctx->line;
715 eol = sol + idx->v[cur_idx].len;
716 goto next_hdr;
717 }
718
719 /* first request for this header */
720 sol += hdr_idx_first_pos(idx);
721 old_idx = 0;
722 cur_idx = hdr_idx_first_idx(idx);
723 while (cur_idx) {
724 eol = sol + idx->v[cur_idx].len;
725
726 len = 0;
727 while (1) {
728 if (len >= eol - sol)
729 goto next_hdr;
730 if (sol[len] == ':')
731 break;
732 len++;
733 }
734
735 ctx->del = len;
736 sov = sol + len + 1;
737 while (sov < eol && http_is_lws[(unsigned char)*sov])
738 sov++;
739
740 ctx->line = sol;
741 ctx->prev = old_idx;
742 ctx->idx = cur_idx;
743 ctx->val = sov - sol;
744 ctx->tws = 0;
745
746 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
747 eol--;
748 ctx->tws++;
749 }
750 ctx->vlen = eol - sov;
751 return 1;
752
753 next_hdr:
754 sol = eol + idx->v[cur_idx].cr + 1;
755 old_idx = cur_idx;
756 cur_idx = idx->v[cur_idx].next;
757 }
758 return 0;
759}
760
Willy Tarreau68085d82010-01-18 14:54:04 +0100761/* Find the end of the header value contained between <s> and <e>. See RFC2616,
762 * par 2.2 for more information. Note that it requires a valid header to return
763 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200764 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100765char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200766{
767 int quoted, qdpair;
768
769 quoted = qdpair = 0;
770 for (; s < e; s++) {
771 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200772 else if (quoted) {
773 if (*s == '\\') qdpair = 1;
774 else if (*s == '"') quoted = 0;
775 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200776 else if (*s == '"') quoted = 1;
777 else if (*s == ',') return s;
778 }
779 return s;
780}
781
782/* Find the first or next occurrence of header <name> in message buffer <sol>
783 * using headers index <idx>, and return it in the <ctx> structure. This
784 * structure holds everything necessary to use the header and find next
785 * occurrence. If its <idx> member is 0, the header is searched from the
786 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100787 * 1 when it finds a value, and 0 when there is no more. It is designed to work
788 * with headers defined as comma-separated lists. As a special case, if ctx->val
789 * is NULL when searching for a new values of a header, the current header is
790 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200791 */
792int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100793 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200794 struct hdr_ctx *ctx)
795{
Willy Tarreau68085d82010-01-18 14:54:04 +0100796 char *eol, *sov;
797 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200798
Willy Tarreau68085d82010-01-18 14:54:04 +0100799 cur_idx = ctx->idx;
800 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200801 /* We have previously returned a value, let's search
802 * another one on the same line.
803 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200804 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200805 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100806 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200807 eol = sol + idx->v[cur_idx].len;
808
809 if (sov >= eol)
810 /* no more values in this header */
811 goto next_hdr;
812
Willy Tarreau68085d82010-01-18 14:54:04 +0100813 /* values remaining for this header, skip the comma but save it
814 * for later use (eg: for header deletion).
815 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200816 sov++;
817 while (sov < eol && http_is_lws[(unsigned char)*sov])
818 sov++;
819
820 goto return_hdr;
821 }
822
823 /* first request for this header */
824 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100825 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200826 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200827 while (cur_idx) {
828 eol = sol + idx->v[cur_idx].len;
829
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200830 if (len == 0) {
831 /* No argument was passed, we want any header.
832 * To achieve this, we simply build a fake request. */
833 while (sol + len < eol && sol[len] != ':')
834 len++;
835 name = sol;
836 }
837
Willy Tarreau33a7e692007-06-10 19:45:56 +0200838 if ((len < eol - sol) &&
839 (sol[len] == ':') &&
840 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100841 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200842 sov = sol + len + 1;
843 while (sov < eol && http_is_lws[(unsigned char)*sov])
844 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100845
Willy Tarreau33a7e692007-06-10 19:45:56 +0200846 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100847 ctx->prev = old_idx;
848 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200849 ctx->idx = cur_idx;
850 ctx->val = sov - sol;
851
852 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200853 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200854 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200855 eol--;
856 ctx->tws++;
857 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200858 ctx->vlen = eol - sov;
859 return 1;
860 }
861 next_hdr:
862 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100863 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200864 cur_idx = idx->v[cur_idx].next;
865 }
866 return 0;
867}
868
869int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100870 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200871 struct hdr_ctx *ctx)
872{
873 return http_find_header2(name, strlen(name), sol, idx, ctx);
874}
875
Willy Tarreau68085d82010-01-18 14:54:04 +0100876/* Remove one value of a header. This only works on a <ctx> returned by one of
877 * the http_find_header functions. The value is removed, as well as surrounding
878 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100879 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100880 * message <msg>. The new index is returned. If it is zero, it means there is
881 * no more header, so any processing may stop. The ctx is always left in a form
882 * that can be handled by http_find_header2() to find next occurrence.
883 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100884int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100885{
886 int cur_idx = ctx->idx;
887 char *sol = ctx->line;
888 struct hdr_idx_elem *hdr;
889 int delta, skip_comma;
890
891 if (!cur_idx)
892 return 0;
893
894 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200895 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100896 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200897 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100898 http_msg_move_end(msg, delta);
899 idx->used--;
900 hdr->len = 0; /* unused entry */
901 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100902 if (idx->tail == ctx->idx)
903 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100904 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100905 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100906 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200907 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100908 return ctx->idx;
909 }
910
911 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200912 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
913 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100914 */
915
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200916 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200917 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200918 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100919 NULL, 0);
920 hdr->len += delta;
921 http_msg_move_end(msg, delta);
922 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200923 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100924 return ctx->idx;
925}
926
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100927/* This function handles a server error at the stream interface level. The
928 * stream interface is assumed to be already in a closed state. An optional
929 * message is copied into the input buffer, and an HTTP status code stored.
930 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100931 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200933static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100934 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200935{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100936 channel_auto_read(si_oc(si));
937 channel_abort(si_oc(si));
938 channel_auto_close(si_oc(si));
939 channel_erase(si_oc(si));
940 channel_auto_close(si_ic(si));
941 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100942 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200943 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100944 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200945 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200946 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200947 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200948 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200949 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200950}
951
Willy Tarreau87b09662015-04-03 00:22:06 +0200952/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100953 * and message.
954 */
955
Willy Tarreau87b09662015-04-03 00:22:06 +0200956struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100957{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200958 if (s->be->errmsg[msgnum].str)
959 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200960 else if (strm_fe(s)->errmsg[msgnum].str)
961 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100962 else
963 return &http_err_chunks[msgnum];
964}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200965
Willy Tarreau53b6c742006-12-17 13:37:46 +0100966/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200967 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
968 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100969 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100970enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100971{
972 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100973 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100974
975 m = ((unsigned)*str - 'A');
976
977 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100978 for (h = http_methods[m]; h->len > 0; h++) {
979 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100980 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100981 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100982 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100983 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100984 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200985 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100986}
987
Willy Tarreau21d2af32008-02-14 20:25:24 +0100988/* Parse the URI from the given transaction (which is assumed to be in request
989 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
990 * It is returned otherwise.
991 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +0200992char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100993{
994 char *ptr, *end;
995
Willy Tarreau9b28e032012-10-12 23:49:43 +0200996 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100997 end = ptr + txn->req.sl.rq.u_l;
998
999 if (ptr >= end)
1000 return NULL;
1001
1002 /* RFC2616, par. 5.1.2 :
1003 * Request-URI = "*" | absuri | abspath | authority
1004 */
1005
1006 if (*ptr == '*')
1007 return NULL;
1008
1009 if (isalpha((unsigned char)*ptr)) {
1010 /* this is a scheme as described by RFC3986, par. 3.1 */
1011 ptr++;
1012 while (ptr < end &&
1013 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1014 ptr++;
1015 /* skip '://' */
1016 if (ptr == end || *ptr++ != ':')
1017 return NULL;
1018 if (ptr == end || *ptr++ != '/')
1019 return NULL;
1020 if (ptr == end || *ptr++ != '/')
1021 return NULL;
1022 }
1023 /* skip [user[:passwd]@]host[:[port]] */
1024
1025 while (ptr < end && *ptr != '/')
1026 ptr++;
1027
1028 if (ptr == end)
1029 return NULL;
1030
1031 /* OK, we got the '/' ! */
1032 return ptr;
1033}
1034
William Lallemand65ad6e12014-01-31 15:08:02 +01001035/* Parse the URI from the given string and look for the "/" beginning the PATH.
1036 * If not found, return NULL. It is returned otherwise.
1037 */
1038static char *
1039http_get_path_from_string(char *str)
1040{
1041 char *ptr = str;
1042
1043 /* RFC2616, par. 5.1.2 :
1044 * Request-URI = "*" | absuri | abspath | authority
1045 */
1046
1047 if (*ptr == '*')
1048 return NULL;
1049
1050 if (isalpha((unsigned char)*ptr)) {
1051 /* this is a scheme as described by RFC3986, par. 3.1 */
1052 ptr++;
1053 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1054 ptr++;
1055 /* skip '://' */
1056 if (*ptr == '\0' || *ptr++ != ':')
1057 return NULL;
1058 if (*ptr == '\0' || *ptr++ != '/')
1059 return NULL;
1060 if (*ptr == '\0' || *ptr++ != '/')
1061 return NULL;
1062 }
1063 /* skip [user[:passwd]@]host[:[port]] */
1064
1065 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1066 ptr++;
1067
1068 if (*ptr == '\0' || *ptr == ' ')
1069 return NULL;
1070
1071 /* OK, we got the '/' ! */
1072 return ptr;
1073}
1074
Willy Tarreau71241ab2012-12-27 11:30:54 +01001075/* Returns a 302 for a redirectable request that reaches a server working in
1076 * in redirect mode. This may only be called just after the stream interface
1077 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1078 * follow normal proxy processing. NOTE: this function is designed to support
1079 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001080 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001081void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001082{
1083 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001084 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001085 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001086 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001087
1088 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001089 trash.len = strlen(HTTP_302);
1090 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001091
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001092 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001093
Willy Tarreauefb453c2008-10-26 20:49:47 +01001094 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001095 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001096 return;
1097
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001098 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001099 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001100 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1101 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001102 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001103
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001104 /* 3: add the request URI. Since it was already forwarded, we need
1105 * to temporarily rewind the buffer.
1106 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001107 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001108 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001109
Willy Tarreauefb453c2008-10-26 20:49:47 +01001110 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001111 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 +02001112
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001113 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001114
Willy Tarreauefb453c2008-10-26 20:49:47 +01001115 if (!path)
1116 return;
1117
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001118 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001119 return;
1120
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001121 memcpy(trash.str + trash.len, path, len);
1122 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001123
1124 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001125 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1126 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001127 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001128 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1129 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001130 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001131
1132 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001133 si_shutr(si);
1134 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001135 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001136 si->state = SI_ST_CLO;
1137
1138 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001139 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001140
1141 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001142 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001143 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001144}
1145
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001146/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001147 * that the server side is closed. Note that err_type is actually a
1148 * bitmask, where almost only aborts may be cumulated with other
1149 * values. We consider that aborted operations are more important
1150 * than timeouts or errors due to the fact that nobody else in the
1151 * logs might explain incomplete retries. All others should avoid
1152 * being cumulated. It should normally not be possible to have multiple
1153 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001154 * Note that connection errors appearing on the second request of a keep-alive
1155 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001156 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001157void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001158{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001159 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001160
1161 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001162 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001163 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001164 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001165 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001166 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001167 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001168 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001169 http_server_error(s, si, SF_ERR_SRVTO, 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_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001172 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001173 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001174 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001175 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001176 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001177 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001178 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001179 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1180 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001181 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001182 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001183 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001184 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001185 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001186 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001187 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001188 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001189}
1190
Willy Tarreau42250582007-04-01 01:30:43 +02001191extern const char sess_term_cond[8];
1192extern const char sess_fin_state[8];
1193extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001194struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001195struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001196struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001197struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001198
Willy Tarreau117f59e2007-03-04 18:17:17 +01001199/*
1200 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001201 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001202 */
1203void capture_headers(char *som, struct hdr_idx *idx,
1204 char **cap, struct cap_hdr *cap_hdr)
1205{
1206 char *eol, *sol, *col, *sov;
1207 int cur_idx;
1208 struct cap_hdr *h;
1209 int len;
1210
1211 sol = som + hdr_idx_first_pos(idx);
1212 cur_idx = hdr_idx_first_idx(idx);
1213
1214 while (cur_idx) {
1215 eol = sol + idx->v[cur_idx].len;
1216
1217 col = sol;
1218 while (col < eol && *col != ':')
1219 col++;
1220
1221 sov = col + 1;
1222 while (sov < eol && http_is_lws[(unsigned char)*sov])
1223 sov++;
1224
1225 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001226 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001227 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1228 if (cap[h->index] == NULL)
1229 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001230 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001231
1232 if (cap[h->index] == NULL) {
1233 Alert("HTTP capture : out of memory.\n");
1234 continue;
1235 }
1236
1237 len = eol - sov;
1238 if (len > h->len)
1239 len = h->len;
1240
1241 memcpy(cap[h->index], sov, len);
1242 cap[h->index][len]=0;
1243 }
1244 }
1245 sol = eol + idx->v[cur_idx].cr + 1;
1246 cur_idx = idx->v[cur_idx].next;
1247 }
1248}
1249
1250
Willy Tarreau42250582007-04-01 01:30:43 +02001251/* either we find an LF at <ptr> or we jump to <bad>.
1252 */
1253#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1254
1255/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1256 * otherwise to <http_msg_ood> with <state> set to <st>.
1257 */
1258#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1259 ptr++; \
1260 if (likely(ptr < end)) \
1261 goto good; \
1262 else { \
1263 state = (st); \
1264 goto http_msg_ood; \
1265 } \
1266 } while (0)
1267
1268
Willy Tarreaubaaee002006-06-26 02:48:02 +02001269/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001270 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001271 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1272 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1273 * will give undefined results.
1274 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1275 * and that msg->sol points to the beginning of the response.
1276 * If a complete line is found (which implies that at least one CR or LF is
1277 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1278 * returned indicating an incomplete line (which does not mean that parts have
1279 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1280 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1281 * upon next call.
1282 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001283 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001284 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1285 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001286 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001287 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001288const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001289 enum ht_state state, const char *ptr, const char *end,
1290 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001291{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001292 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001293
Willy Tarreau8973c702007-01-21 23:58:29 +01001294 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001295 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001296 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001297 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001298 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1299
1300 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001301 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001302 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1303 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001304 state = HTTP_MSG_ERROR;
1305 break;
1306
Willy Tarreau8973c702007-01-21 23:58:29 +01001307 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001308 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001310 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 goto http_msg_rpcode;
1312 }
1313 if (likely(HTTP_IS_SPHT(*ptr)))
1314 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1315 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001316 state = HTTP_MSG_ERROR;
1317 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001318
Willy Tarreau8973c702007-01-21 23:58:29 +01001319 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001320 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001321 if (likely(!HTTP_IS_LWS(*ptr)))
1322 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1323
1324 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001325 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001326 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1327 }
1328
1329 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001330 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001331 http_msg_rsp_reason:
1332 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001333 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 msg->sl.st.r_l = 0;
1335 goto http_msg_rpline_eol;
1336
Willy Tarreau8973c702007-01-21 23:58:29 +01001337 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001338 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001339 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001340 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001341 goto http_msg_rpreason;
1342 }
1343 if (likely(HTTP_IS_SPHT(*ptr)))
1344 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1345 /* so it's a CR/LF, so there is no reason phrase */
1346 goto http_msg_rsp_reason;
1347
Willy Tarreau8973c702007-01-21 23:58:29 +01001348 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001349 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001350 if (likely(!HTTP_IS_CRLF(*ptr)))
1351 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001352 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001353 http_msg_rpline_eol:
1354 /* We have seen the end of line. Note that we do not
1355 * necessarily have the \n yet, but at least we know that we
1356 * have EITHER \r OR \n, otherwise the response would not be
1357 * complete. We can then record the response length and return
1358 * to the caller which will be able to register it.
1359 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001360 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001361 return ptr;
1362
Willy Tarreau8973c702007-01-21 23:58:29 +01001363 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001364#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001365 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1366 exit(1);
1367#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001368 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001369 }
1370
1371 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001372 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001373 if (ret_state)
1374 *ret_state = state;
1375 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001376 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001377 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001378}
1379
Willy Tarreau8973c702007-01-21 23:58:29 +01001380/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381 * This function parses a request line between <ptr> and <end>, starting with
1382 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1383 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1384 * will give undefined results.
1385 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1386 * and that msg->sol points to the beginning of the request.
1387 * If a complete line is found (which implies that at least one CR or LF is
1388 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1389 * returned indicating an incomplete line (which does not mean that parts have
1390 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1391 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1392 * upon next call.
1393 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001394 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1396 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001397 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001398 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001399const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001400 enum ht_state state, const char *ptr, const char *end,
1401 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001402{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001403 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001404
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001407 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 if (likely(HTTP_IS_TOKEN(*ptr)))
1409 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001410
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001412 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1414 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001415
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 if (likely(HTTP_IS_CRLF(*ptr))) {
1417 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001418 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001420 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001422 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001424 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 msg->sl.rq.v_l = 0;
1426 goto http_msg_rqline_eol;
1427 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001428 state = HTTP_MSG_ERROR;
1429 break;
1430
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001432 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001434 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001435 goto http_msg_rquri;
1436 }
1437 if (likely(HTTP_IS_SPHT(*ptr)))
1438 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1439 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1440 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001441
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001443 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001444 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001445 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001448 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1450 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001451
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001452 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001453 /* non-ASCII chars are forbidden unless option
1454 * accept-invalid-http-request is enabled in the frontend.
1455 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001456 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001457 if (msg->err_pos < -1)
1458 goto invalid_char;
1459 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001460 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001461 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1462 }
1463
1464 if (likely(HTTP_IS_CRLF(*ptr))) {
1465 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1466 goto http_msg_req09_uri_e;
1467 }
1468
1469 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001470 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001471 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001472 state = HTTP_MSG_ERROR;
1473 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001476 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001478 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001479 goto http_msg_rqver;
1480 }
1481 if (likely(HTTP_IS_SPHT(*ptr)))
1482 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1483 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1484 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001487 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001488 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001490
1491 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001492 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001493 http_msg_rqline_eol:
1494 /* We have seen the end of line. Note that we do not
1495 * necessarily have the \n yet, but at least we know that we
1496 * have EITHER \r OR \n, otherwise the request would not be
1497 * complete. We can then record the request length and return
1498 * to the caller which will be able to register it.
1499 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001500 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001501 return ptr;
1502 }
1503
1504 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001505 state = HTTP_MSG_ERROR;
1506 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001507
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001509#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1511 exit(1);
1512#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001513 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001514 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001515
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001517 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 if (ret_state)
1519 *ret_state = state;
1520 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001521 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001524
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001525/*
1526 * Returns the data from Authorization header. Function may be called more
1527 * than once so data is stored in txn->auth_data. When no header is found
1528 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001529 * searching again for something we are unable to find anyway. However, if
1530 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001531 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001532 */
1533
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001534/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1535 * set according to global.tune.bufsize.
1536 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001537char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001538
1539int
Willy Tarreau87b09662015-04-03 00:22:06 +02001540get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001541{
1542
Willy Tarreaueee5b512015-04-03 23:46:31 +02001543 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001544 struct chunk auth_method;
1545 struct hdr_ctx ctx;
1546 char *h, *p;
1547 int len;
1548
1549#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001550 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001551#endif
1552
1553 if (txn->auth.method == HTTP_AUTH_WRONG)
1554 return 0;
1555
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001556 txn->auth.method = HTTP_AUTH_WRONG;
1557
1558 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001559
1560 if (txn->flags & TX_USE_PX_CONN) {
1561 h = "Proxy-Authorization";
1562 len = strlen(h);
1563 } else {
1564 h = "Authorization";
1565 len = strlen(h);
1566 }
1567
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001568 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001569 return 0;
1570
1571 h = ctx.line + ctx.val;
1572
1573 p = memchr(h, ' ', ctx.vlen);
1574 if (!p || p == h)
1575 return 0;
1576
1577 chunk_initlen(&auth_method, h, 0, p-h);
1578 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1579
1580 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1581
1582 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001583 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001584
1585 if (len < 0)
1586 return 0;
1587
1588
1589 get_http_auth_buff[len] = '\0';
1590
1591 p = strchr(get_http_auth_buff, ':');
1592
1593 if (!p)
1594 return 0;
1595
1596 txn->auth.user = get_http_auth_buff;
1597 *p = '\0';
1598 txn->auth.pass = p+1;
1599
1600 txn->auth.method = HTTP_AUTH_BASIC;
1601 return 1;
1602 }
1603
1604 return 0;
1605}
1606
Willy Tarreau58f10d72006-12-04 02:26:12 +01001607
Willy Tarreau8973c702007-01-21 23:58:29 +01001608/*
1609 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001610 * depending on the initial msg->msg_state. The caller is responsible for
1611 * ensuring that the message does not wrap. The function can be preempted
1612 * everywhere when data are missing and recalled at the exact same location
1613 * with no information loss. The message may even be realigned between two
1614 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001615 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001616 * fields. Note that msg->sol will be initialized after completing the first
1617 * state, so that none of the msg pointers has to be initialized prior to the
1618 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001619 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001620void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621{
Willy Tarreau3770f232013-12-07 00:01:53 +01001622 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001624 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001625
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001626 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001627 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001628 ptr = buf->p + msg->next;
1629 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001630
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 if (unlikely(ptr >= end))
1632 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001633
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001634 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001635 /*
1636 * First, states that are specific to the response only.
1637 * We check them first so that request and headers are
1638 * closer to each other (accessed more often).
1639 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001640 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001641 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001642 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001643 /* we have a start of message, but we have to check
1644 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001645 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001646 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001647 if (unlikely(ptr != buf->p)) {
1648 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001649 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001650 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001651 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001652 }
Willy Tarreau26927362012-05-18 23:22:52 +02001653 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001654 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001655 hdr_idx_init(idx);
1656 state = HTTP_MSG_RPVER;
1657 goto http_msg_rpver;
1658 }
1659
1660 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1661 goto http_msg_invalid;
1662
1663 if (unlikely(*ptr == '\n'))
1664 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1665 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1666 /* stop here */
1667
Willy Tarreau8973c702007-01-21 23:58:29 +01001668 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001669 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001670 EXPECT_LF_HERE(ptr, http_msg_invalid);
1671 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1672 /* stop here */
1673
Willy Tarreau8973c702007-01-21 23:58:29 +01001674 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001675 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001676 case HTTP_MSG_RPVER_SP:
1677 case HTTP_MSG_RPCODE:
1678 case HTTP_MSG_RPCODE_SP:
1679 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001680 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001681 state, ptr, end,
1682 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001683 if (unlikely(!ptr))
1684 return;
1685
1686 /* we have a full response and we know that we have either a CR
1687 * or an LF at <ptr>.
1688 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001689 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1690
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001691 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001692 if (likely(*ptr == '\r'))
1693 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1694 goto http_msg_rpline_end;
1695
Willy Tarreau8973c702007-01-21 23:58:29 +01001696 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001697 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001698 /* msg->sol must point to the first of CR or LF. */
1699 EXPECT_LF_HERE(ptr, http_msg_invalid);
1700 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1701 /* stop here */
1702
1703 /*
1704 * Second, states that are specific to the request only
1705 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001707 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001709 /* we have a start of message, but we have to check
1710 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001711 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001712 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001713 if (likely(ptr != buf->p)) {
1714 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001715 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001716 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001717 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001718 }
Willy Tarreau26927362012-05-18 23:22:52 +02001719 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001720 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001721 state = HTTP_MSG_RQMETH;
1722 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001723 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001724
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001725 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1726 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001727
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 if (unlikely(*ptr == '\n'))
1729 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1730 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001731 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001732
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001734 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001735 EXPECT_LF_HERE(ptr, http_msg_invalid);
1736 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001737 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001738
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001740 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001741 case HTTP_MSG_RQMETH_SP:
1742 case HTTP_MSG_RQURI:
1743 case HTTP_MSG_RQURI_SP:
1744 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001745 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001746 state, ptr, end,
1747 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748 if (unlikely(!ptr))
1749 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 /* we have a full request and we know that we have either a CR
1752 * or an LF at <ptr>.
1753 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001755
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001756 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 if (likely(*ptr == '\r'))
1758 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001760
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001761 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001762 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001763 /* check for HTTP/0.9 request : no version information available.
1764 * msg->sol must point to the first of CR or LF.
1765 */
1766 if (unlikely(msg->sl.rq.v_l == 0))
1767 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001768
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001769 EXPECT_LF_HERE(ptr, http_msg_invalid);
1770 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001771 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001772
Willy Tarreau8973c702007-01-21 23:58:29 +01001773 /*
1774 * Common states below
1775 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001776 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001777 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001778 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001779 if (likely(!HTTP_IS_CRLF(*ptr))) {
1780 goto http_msg_hdr_name;
1781 }
1782
1783 if (likely(*ptr == '\r'))
1784 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1785 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001786
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001787 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001788 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001789 /* assumes msg->sol points to the first char */
1790 if (likely(HTTP_IS_TOKEN(*ptr)))
1791 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001792
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001793 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001794 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001795
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001796 if (likely(msg->err_pos < -1) || *ptr == '\n')
1797 goto http_msg_invalid;
1798
1799 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001800 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001801
1802 /* and we still accept this non-token character */
1803 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001804
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001805 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001806 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001807 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001808 if (likely(HTTP_IS_SPHT(*ptr)))
1809 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001810
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001811 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001812 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001813
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001814 if (likely(!HTTP_IS_CRLF(*ptr))) {
1815 goto http_msg_hdr_val;
1816 }
1817
1818 if (likely(*ptr == '\r'))
1819 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1820 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001821
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001822 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001823 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001824 EXPECT_LF_HERE(ptr, http_msg_invalid);
1825 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001826
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001827 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001828 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001829 if (likely(HTTP_IS_SPHT(*ptr))) {
1830 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001831 for (; buf->p + msg->sov < ptr; msg->sov++)
1832 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001833 goto http_msg_hdr_l1_sp;
1834 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001835 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001836 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001837 goto http_msg_complete_header;
1838
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001839 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001840 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001841 /* assumes msg->sol points to the first char, and msg->sov
1842 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001843 */
1844 if (likely(!HTTP_IS_CRLF(*ptr)))
1845 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001846
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001847 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001848 /* Note: we could also copy eol into ->eoh so that we have the
1849 * real header end in case it ends with lots of LWS, but is this
1850 * really needed ?
1851 */
1852 if (likely(*ptr == '\r'))
1853 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1854 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001855
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001856 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001857 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001858 EXPECT_LF_HERE(ptr, http_msg_invalid);
1859 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001860
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001861 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001862 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001863 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1864 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001865 for (; buf->p + msg->eol < ptr; msg->eol++)
1866 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 goto http_msg_hdr_val;
1868 }
1869 http_msg_complete_header:
1870 /*
1871 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001872 * Assumes msg->sol points to the first char, msg->sov points
1873 * to the first character of the value and msg->eol to the
1874 * first CR or LF so we know how the line ends. We insert last
1875 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001876 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001877 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001878 idx, idx->tail) < 0))
1879 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001880
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001881 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001882 if (likely(!HTTP_IS_CRLF(*ptr))) {
1883 goto http_msg_hdr_name;
1884 }
1885
1886 if (likely(*ptr == '\r'))
1887 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1888 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001889
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001890 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001891 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001892 /* Assumes msg->sol points to the first of either CR or LF.
1893 * Sets ->sov and ->next to the total header length, ->eoh to
1894 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1895 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 EXPECT_LF_HERE(ptr, http_msg_invalid);
1897 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001898 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001899 msg->eoh = msg->sol;
1900 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001901 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001902 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001903 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001904
1905 case HTTP_MSG_ERROR:
1906 /* this may only happen if we call http_msg_analyser() twice with an error */
1907 break;
1908
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001909 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001910#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001911 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1912 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001913#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001914 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001915 }
1916 http_msg_ood:
1917 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001918 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001919 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001920 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001921
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001922 http_msg_invalid:
1923 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001924 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001925 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001926 return;
1927}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001928
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001929/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1930 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1931 * nothing is done and 1 is returned.
1932 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001933static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001934{
1935 int delta;
1936 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001937 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001938
1939 if (msg->sl.rq.v_l != 0)
1940 return 1;
1941
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001942 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1943 if (txn->meth != HTTP_METH_GET)
1944 return 0;
1945
Willy Tarreau9b28e032012-10-12 23:49:43 +02001946 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001947 delta = 0;
1948
1949 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001950 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1951 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001952 }
1953 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001954 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001955 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001956 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001957 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001958 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001959 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001960 NULL, NULL);
1961 if (unlikely(!cur_end))
1962 return 0;
1963
1964 /* we have a full HTTP/1.0 request now and we know that
1965 * we have either a CR or an LF at <ptr>.
1966 */
1967 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1968 return 1;
1969}
1970
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001971/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001972 * and "keep-alive" values. If we already know that some headers may safely
1973 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001974 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1975 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001976 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001977 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1978 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1979 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001980 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001981 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001982void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001983{
Willy Tarreau5b154472009-12-21 20:11:07 +01001984 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001985 const char *hdr_val = "Connection";
1986 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001987
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001988 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001989 return;
1990
Willy Tarreau88d349d2010-01-25 12:15:43 +01001991 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1992 hdr_val = "Proxy-Connection";
1993 hdr_len = 16;
1994 }
1995
Willy Tarreau5b154472009-12-21 20:11:07 +01001996 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001997 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001998 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001999 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2000 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002001 if (to_del & 2)
2002 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002003 else
2004 txn->flags |= TX_CON_KAL_SET;
2005 }
2006 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2007 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002008 if (to_del & 1)
2009 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002010 else
2011 txn->flags |= TX_CON_CLO_SET;
2012 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002013 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2014 txn->flags |= TX_HDR_CONN_UPG;
2015 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002016 }
2017
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002018 txn->flags |= TX_HDR_CONN_PRS;
2019 return;
2020}
Willy Tarreau5b154472009-12-21 20:11:07 +01002021
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002022/* Apply desired changes on the Connection: header. Values may be removed and/or
2023 * added depending on the <wanted> flags, which are exclusively composed of
2024 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2025 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2026 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002027void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002028{
2029 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002030 const char *hdr_val = "Connection";
2031 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002032
2033 ctx.idx = 0;
2034
Willy Tarreau88d349d2010-01-25 12:15:43 +01002035
2036 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2037 hdr_val = "Proxy-Connection";
2038 hdr_len = 16;
2039 }
2040
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002041 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002042 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002043 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2044 if (wanted & TX_CON_KAL_SET)
2045 txn->flags |= TX_CON_KAL_SET;
2046 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002047 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002048 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002049 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2050 if (wanted & TX_CON_CLO_SET)
2051 txn->flags |= TX_CON_CLO_SET;
2052 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002053 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002054 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002055 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002056
2057 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2058 return;
2059
2060 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2061 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002062 hdr_val = "Connection: close";
2063 hdr_len = 17;
2064 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2065 hdr_val = "Proxy-Connection: close";
2066 hdr_len = 23;
2067 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002068 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002069 }
2070
2071 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2072 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002073 hdr_val = "Connection: keep-alive";
2074 hdr_len = 22;
2075 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2076 hdr_val = "Proxy-Connection: keep-alive";
2077 hdr_len = 28;
2078 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002079 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002080 }
2081 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002082}
2083
Willy Tarreauc24715e2014-04-17 15:21:20 +02002084/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
2085 * the first byte of data after the chunk size, so that we know we can forward
2086 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
2087 * the chunk size. That way it is always possible to differentiate between the
2088 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01002089 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002090 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002091 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002092static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002093{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002094 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002095 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002096 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002097 const char *end = buf->data + buf->size;
2098 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002099 unsigned int chunk = 0;
2100
2101 /* The chunk size is in the following form, though we are only
2102 * interested in the size and CRLF :
2103 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2104 */
2105 while (1) {
2106 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002107 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002108 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002109 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002110 if (c < 0) /* not a hex digit anymore */
2111 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002112 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002113 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002114 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002115 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002116 chunk = (chunk << 4) + c;
2117 }
2118
Willy Tarreaud98cf932009-12-27 22:54:55 +01002119 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002120 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002121 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002122
2123 while (http_is_spht[(unsigned char)*ptr]) {
2124 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002125 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002126 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002127 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002128 }
2129
Willy Tarreaud98cf932009-12-27 22:54:55 +01002130 /* Up to there, we know that at least one byte is present at *ptr. Check
2131 * for the end of chunk size.
2132 */
2133 while (1) {
2134 if (likely(HTTP_IS_CRLF(*ptr))) {
2135 /* we now have a CR or an LF at ptr */
2136 if (likely(*ptr == '\r')) {
2137 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002138 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002139 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002140 return 0;
2141 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002142
Willy Tarreaud98cf932009-12-27 22:54:55 +01002143 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002144 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002145 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002146 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002147 /* done */
2148 break;
2149 }
2150 else if (*ptr == ';') {
2151 /* chunk extension, ends at next CRLF */
2152 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002153 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002154 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002155 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002156
2157 while (!HTTP_IS_CRLF(*ptr)) {
2158 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002159 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002160 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002161 return 0;
2162 }
2163 /* we have a CRLF now, loop above */
2164 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002165 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002166 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002167 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002168 }
2169
Willy Tarreaud98cf932009-12-27 22:54:55 +01002170 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002171 * which may or may not be present. We save that into ->next,
2172 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002173 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002174 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002175 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002176 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002177 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002178 msg->chunk_len = chunk;
2179 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002180 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002181 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002182 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002183 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002184 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002185}
2186
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002187/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002188 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002189 * the trailers is found, it is automatically scheduled to be forwarded,
2190 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2191 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002192 * except maybe msg->next if it could parse some lines, and returns zero.
2193 * If a parse error is encountered, the function returns < 0 and does not
2194 * change anything except maybe msg->next. Note that the message must
2195 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002196 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002197 * forwarding, and that msg->next exactly matches the length of trailers
2198 * already parsed and not forwarded. It is also important to note that this
2199 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002200 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002201static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002202{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002203 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002204
Willy Tarreaua458b672012-03-05 11:17:50 +01002205 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002206 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002207 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002208 const char *ptr = b_ptr(buf, msg->next);
2209 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002210 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002211
2212 /* scan current line and stop at LF or CRLF */
2213 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002214 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002215 return 0;
2216
2217 if (*ptr == '\n') {
2218 if (!p1)
2219 p1 = ptr;
2220 p2 = ptr;
2221 break;
2222 }
2223
2224 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002225 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002226 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002227 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002228 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002229 p1 = ptr;
2230 }
2231
2232 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002233 if (ptr >= buf->data + buf->size)
2234 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002235 }
2236
2237 /* after LF; point to beginning of next line */
2238 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002239 if (p2 >= buf->data + buf->size)
2240 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002241
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002242 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002243 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002244 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002245
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002246 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002247 /* LF/CRLF at beginning of line => end of trailers at p2.
2248 * Everything was scheduled for forwarding, there's nothing
2249 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002250 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002251 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002252 msg->msg_state = HTTP_MSG_DONE;
2253 return 1;
2254 }
2255 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002256 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002257 }
2258}
2259
Willy Tarreauc24715e2014-04-17 15:21:20 +02002260/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2261 * or a possible LF alone at the end of a chunk. It automatically adjusts
2262 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002263 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002264 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2265 * not enough data are available, the function does not change anything and
2266 * returns zero. If a parse error is encountered, the function returns < 0 and
2267 * does not change anything. Note: this function is designed to parse wrapped
2268 * CRLF at the end of the buffer.
2269 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002270static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002271{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002272 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002273 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002274 int bytes;
2275
2276 /* NB: we'll check data availabilty at the end. It's not a
2277 * problem because whatever we match first will be checked
2278 * against the correct length.
2279 */
2280 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002281 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002282 if (*ptr == '\r') {
2283 bytes++;
2284 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002285 if (ptr >= buf->data + buf->size)
2286 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002287 }
2288
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002289 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002290 return 0;
2291
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002292 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002293 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002294 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002295 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002296
2297 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002298 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002299 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002300 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002301 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002302 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2303 return 1;
2304}
Willy Tarreau5b154472009-12-21 20:11:07 +01002305
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002306/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2307 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2308 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2309 * Unparsable qvalues return 1000 as "q=1.000".
2310 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002311int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002312{
2313 int q = 1000;
2314
Willy Tarreau506c69a2014-07-08 00:59:48 +02002315 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002316 goto out;
2317 q = (*qvalue++ - '0') * 1000;
2318
2319 if (*qvalue++ != '.')
2320 goto out;
2321
Willy Tarreau506c69a2014-07-08 00:59:48 +02002322 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002323 goto out;
2324 q += (*qvalue++ - '0') * 100;
2325
Willy Tarreau506c69a2014-07-08 00:59:48 +02002326 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002327 goto out;
2328 q += (*qvalue++ - '0') * 10;
2329
Willy Tarreau506c69a2014-07-08 00:59:48 +02002330 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002331 goto out;
2332 q += (*qvalue++ - '0') * 1;
2333 out:
2334 if (q > 1000)
2335 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002336 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002337 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002338 return q;
2339}
William Lallemand82fe75c2012-10-23 10:25:10 +02002340
2341/*
2342 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002343 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002344int select_compression_request_header(struct stream *s, struct buffer *req)
William Lallemand82fe75c2012-10-23 10:25:10 +02002345{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002346 struct http_txn *txn = s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002347 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002348 struct hdr_ctx ctx;
2349 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002350 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002351
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002352 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2353 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002354 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2355 */
2356 ctx.idx = 0;
2357 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2358 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002359 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2360 (ctx.vlen < 31 ||
2361 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2362 ctx.line[ctx.val + 30] < '6' ||
2363 (ctx.line[ctx.val + 30] == '6' &&
2364 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2365 s->comp_algo = NULL;
2366 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002367 }
2368
William Lallemand82fe75c2012-10-23 10:25:10 +02002369 /* search for the algo in the backend in priority or the frontend */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002370 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 +01002371 int best_q = 0;
2372
William Lallemand82fe75c2012-10-23 10:25:10 +02002373 ctx.idx = 0;
2374 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002375 const char *qval;
2376 int q;
2377 int toklen;
2378
2379 /* try to isolate the token from the optional q-value */
2380 toklen = 0;
2381 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2382 toklen++;
2383
2384 qval = ctx.line + ctx.val + toklen;
2385 while (1) {
2386 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2387 qval++;
2388
2389 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2390 qval = NULL;
2391 break;
2392 }
2393 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002394
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002395 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2396 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002397
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002398 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2399 qval = NULL;
2400 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002401 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002402 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2403 break;
2404
2405 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2406 qval++;
2407 }
2408
2409 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002410 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002411
2412 if (q <= best_q)
2413 continue;
2414
2415 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2416 if (*(ctx.line + ctx.val) == '*' ||
Willy Tarreau615105e2015-03-28 16:40:46 +01002417 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002418 s->comp_algo = comp_algo;
2419 best_q = q;
2420 break;
2421 }
2422 }
2423 }
2424 }
2425
2426 /* remove all occurrences of the header when "compression offload" is set */
2427 if (s->comp_algo) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002428 if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002429 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2430 ctx.idx = 0;
2431 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2432 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002433 }
2434 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002435 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002436 }
2437
2438 /* identity is implicit does not require headers */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002439 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 +02002440 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
Willy Tarreau615105e2015-03-28 16:40:46 +01002441 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002442 s->comp_algo = comp_algo;
2443 return 1;
2444 }
2445 }
2446 }
2447
2448 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002449 return 0;
2450}
2451
2452/*
2453 * Selects a comression algorithm depending of the server response.
2454 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002455int select_compression_response_header(struct stream *s, struct buffer *res)
William Lallemand82fe75c2012-10-23 10:25:10 +02002456{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002457 struct http_txn *txn = s->txn;
William Lallemand82fe75c2012-10-23 10:25:10 +02002458 struct http_msg *msg = &txn->rsp;
2459 struct hdr_ctx ctx;
2460 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002461
2462 /* no common compression algorithm was found in request header */
2463 if (s->comp_algo == NULL)
2464 goto fail;
2465
2466 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002467 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002468 goto fail;
2469
Jesse Hathaway2468d4e2015-03-06 20:16:15 +00002470 /* compress 200,201,202,203 responses only */
2471 if ((txn->status != 200) &&
2472 (txn->status != 201) &&
2473 (txn->status != 202) &&
2474 (txn->status != 203))
William Lallemandd3002612012-11-26 14:34:47 +01002475 goto fail;
2476
William Lallemand82fe75c2012-10-23 10:25:10 +02002477 /* Content-Length is null */
2478 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2479 goto fail;
2480
2481 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002482 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002483 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2484 goto fail;
2485
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002486 /* no compression when Cache-Control: no-transform is present in the message */
2487 ctx.idx = 0;
2488 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2489 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2490 goto fail;
2491 }
2492
William Lallemand82fe75c2012-10-23 10:25:10 +02002493 comp_type = NULL;
2494
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002495 /* we don't want to compress multipart content-types, nor content-types that are
2496 * not listed in the "compression type" directive if any. If no content-type was
2497 * found but configuration requires one, we don't compress either. Backend has
2498 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002499 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002500 ctx.idx = 0;
2501 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2502 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2503 goto fail;
2504
2505 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002506 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002507 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002508 if (ctx.vlen >= comp_type->name_len &&
2509 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002510 /* this Content-Type should be compressed */
2511 break;
2512 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002513 /* this Content-Type should not be compressed */
2514 if (comp_type == NULL)
2515 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002516 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002517 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002518 else { /* no content-type header */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002519 if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->comp->types))
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002520 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002521 }
2522
William Lallemandd85f9172012-11-09 17:05:39 +01002523 /* limit compression rate */
2524 if (global.comp_rate_lim > 0)
2525 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2526 goto fail;
2527
William Lallemand072a2bf2012-11-20 17:01:01 +01002528 /* limit cpu usage */
2529 if (idle_pct < compress_min_idle)
2530 goto fail;
2531
William Lallemand4c49fae2012-11-07 15:00:23 +01002532 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002533 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002534 goto fail;
2535
Willy Tarreaue7dff022015-04-03 01:14:29 +02002536 s->flags |= SF_COMP_READY;
William Lallemandec3e3892012-11-12 17:02:18 +01002537
William Lallemand82fe75c2012-10-23 10:25:10 +02002538 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002539 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002540 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2541 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2542
2543 /* add Transfer-Encoding header */
2544 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2545 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2546
2547 /*
2548 * Add Content-Encoding header when it's not identity encoding.
2549 * RFC 2616 : Identity encoding: This content-coding is used only in the
2550 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2551 * header.
2552 */
Willy Tarreau615105e2015-03-28 16:40:46 +01002553 if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002554 trash.len = 18;
2555 memcpy(trash.str, "Content-Encoding: ", trash.len);
Willy Tarreau615105e2015-03-28 16:40:46 +01002556 memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len);
2557 trash.len += s->comp_algo->ua_name_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002558 trash.str[trash.len] = '\0';
2559 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002560 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002561 return 1;
2562
2563fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002564 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002565 return 0;
2566}
2567
Willy Tarreau87b09662015-04-03 00:22:06 +02002568void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002569{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002570 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002571 int tmp = TX_CON_WANT_KAL;
2572
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002573 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2574 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002575 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2576 tmp = TX_CON_WANT_TUN;
2577
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002578 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002579 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2580 tmp = TX_CON_WANT_TUN;
2581 }
2582
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002583 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002584 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2585 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002586 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002587 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2588 tmp = TX_CON_WANT_CLO;
2589 else
2590 tmp = TX_CON_WANT_SCL;
2591 }
2592
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002593 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002594 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2595 tmp = TX_CON_WANT_CLO;
2596
2597 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2598 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2599
2600 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2601 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2602 /* parse the Connection header and possibly clean it */
2603 int to_del = 0;
2604 if ((msg->flags & HTTP_MSGF_VER_11) ||
2605 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002606 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002607 to_del |= 2; /* remove "keep-alive" */
2608 if (!(msg->flags & HTTP_MSGF_VER_11))
2609 to_del |= 1; /* remove "close" */
2610 http_parse_connection_header(txn, msg, to_del);
2611 }
2612
2613 /* check if client or config asks for explicit close in KAL/SCL */
2614 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2615 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2616 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2617 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2618 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002619 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002620 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2621}
William Lallemand82fe75c2012-10-23 10:25:10 +02002622
Willy Tarreaud787e662009-07-07 10:14:51 +02002623/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2624 * processing can continue on next analysers, or zero if it either needs more
2625 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002626 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002627 * when it has nothing left to do, and may remove any analyser when it wants to
2628 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002629 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002630int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002631{
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 /*
2633 * We will parse the partial (or complete) lines.
2634 * We will check the request syntax, and also join multi-line
2635 * headers. An index of all the lines will be elaborated while
2636 * parsing.
2637 *
2638 * For the parsing, we use a 28 states FSM.
2639 *
2640 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002641 * req->buf->p = beginning of request
2642 * req->buf->p + msg->eoh = end of processed headers / start of current one
2643 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002644 * msg->eol = end of current header or line (LF or CRLF)
2645 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002646 *
2647 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002648 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002649 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2650 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002651 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002652
Willy Tarreau59234e92008-11-30 23:51:27 +01002653 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002654 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002655 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002656 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002657 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002658
Willy Tarreau87b09662015-04-03 00:22:06 +02002659 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 +01002660 now_ms, __FUNCTION__,
2661 s,
2662 req,
2663 req->rex, req->wex,
2664 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002665 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002666 req->analysers);
2667
Willy Tarreau52a0c602009-08-16 22:45:38 +02002668 /* we're speaking HTTP here, so let's speak HTTP to the client */
2669 s->srv_error = http_return_srv_error;
2670
Willy Tarreau83e3af02009-12-28 17:39:57 +01002671 /* There's a protected area at the end of the buffer for rewriting
2672 * purposes. We don't want to start to parse the request if the
2673 * protected area is affected, because we may have to move processed
2674 * data later, which is much more complicated.
2675 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002676 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002677 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002678 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002679 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002680 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002681 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002682 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002683 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002684 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002685 return 0;
2686 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002687 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2688 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2689 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002690 }
2691
Willy Tarreau065e8332010-01-08 00:30:20 +01002692 /* Note that we have the same problem with the response ; we
2693 * may want to send a redirect, error or anything which requires
2694 * some spare space. So we'll ensure that we have at least
2695 * maxrewrite bytes available in the response buffer before
2696 * processing that one. This will only affect pipelined
2697 * keep-alive requests.
2698 */
2699 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002700 unlikely(!channel_is_rewritable(&s->res) ||
2701 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2702 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2703 if (s->res.buf->o) {
2704 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002705 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002706 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002707 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002708 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2709 s->res.flags |= CF_WAKE_WRITE;
2710 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002711 return 0;
2712 }
2713 }
2714
Willy Tarreau9b28e032012-10-12 23:49:43 +02002715 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002716 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002717 }
2718
Willy Tarreau59234e92008-11-30 23:51:27 +01002719 /* 1: we might have to print this header in debug mode */
2720 if (unlikely((global.mode & MODE_DEBUG) &&
2721 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002722 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002724
Willy Tarreau9b28e032012-10-12 23:49:43 +02002725 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002726 /* this is a bit complex : in case of error on the request line,
2727 * we know that rq.l is still zero, so we display only the part
2728 * up to the end of the line (truncated by debug_hdr).
2729 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002730 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002731 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002732
Willy Tarreau59234e92008-11-30 23:51:27 +01002733 sol += hdr_idx_first_pos(&txn->hdr_idx);
2734 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002735
Willy Tarreau59234e92008-11-30 23:51:27 +01002736 while (cur_idx) {
2737 eol = sol + txn->hdr_idx.v[cur_idx].len;
2738 debug_hdr("clihdr", s, sol, eol);
2739 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2740 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002741 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002742 }
2743
Willy Tarreau58f10d72006-12-04 02:26:12 +01002744
Willy Tarreau59234e92008-11-30 23:51:27 +01002745 /*
2746 * Now we quickly check if we have found a full valid request.
2747 * If not so, we check the FD and buffer states before leaving.
2748 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002749 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002750 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002751 * on a keep-alive stream, if we encounter and error, close, t/o,
2752 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002753 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002754 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002755 * Last, we may increase some tracked counters' http request errors on
2756 * the cases that are deliberately the client's fault. For instance,
2757 * a timeout or connection reset is not counted as an error. However
2758 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002759 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002760
Willy Tarreau655dce92009-11-08 13:10:58 +01002761 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002762 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002763 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002764 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002765 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002766 stream_inc_http_req_ctr(s);
2767 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002768 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002769 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002770 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002771
Willy Tarreau59234e92008-11-30 23:51:27 +01002772 /* 1: Since we are in header mode, if there's no space
2773 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002774 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002775 * must terminate it now.
2776 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002777 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002778 /* FIXME: check if URI is set and return Status
2779 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002780 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002781 stream_inc_http_req_ctr(s);
2782 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002783 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002784 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002785 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002786 goto return_bad_req;
2787 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002788
Willy Tarreau59234e92008-11-30 23:51:27 +01002789 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002790 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002791 if (!(s->flags & SF_ERR_MASK))
2792 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002793
Willy Tarreaufcffa692010-01-10 14:21:19 +01002794 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002795 goto failed_keep_alive;
2796
Willy Tarreau0f228a02015-05-01 15:37:53 +02002797 if (sess->fe->options & PR_O_IGNORE_PRB)
2798 goto failed_keep_alive;
2799
Willy Tarreau59234e92008-11-30 23:51:27 +01002800 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002801 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002802 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002803 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002804 }
2805
Willy Tarreaudc979f22012-12-04 10:39:01 +01002806 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002807 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002808 msg->msg_state = HTTP_MSG_ERROR;
2809 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002810
Willy Tarreau87b09662015-04-03 00:22:06 +02002811 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002812 proxy_inc_fe_req_ctr(sess->fe);
2813 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002814 if (sess->listener->counters)
2815 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002816
Willy Tarreaue7dff022015-04-03 01:14:29 +02002817 if (!(s->flags & SF_FINST_MASK))
2818 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002819 return 0;
2820 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002821
Willy Tarreau59234e92008-11-30 23:51:27 +01002822 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002823 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002824 if (!(s->flags & SF_ERR_MASK))
2825 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002826
Willy Tarreaufcffa692010-01-10 14:21:19 +01002827 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002828 goto failed_keep_alive;
2829
Willy Tarreau0f228a02015-05-01 15:37:53 +02002830 if (sess->fe->options & PR_O_IGNORE_PRB)
2831 goto failed_keep_alive;
2832
Willy Tarreau59234e92008-11-30 23:51:27 +01002833 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002834 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002835 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002836 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002837 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002838 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01002839 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002840 msg->msg_state = HTTP_MSG_ERROR;
2841 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002842
Willy Tarreau87b09662015-04-03 00:22:06 +02002843 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002844 proxy_inc_fe_req_ctr(sess->fe);
2845 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002846 if (sess->listener->counters)
2847 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002848
Willy Tarreaue7dff022015-04-03 01:14:29 +02002849 if (!(s->flags & SF_FINST_MASK))
2850 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002851 return 0;
2852 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002853
Willy Tarreau59234e92008-11-30 23:51:27 +01002854 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002855 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002856 if (!(s->flags & SF_ERR_MASK))
2857 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002858
Willy Tarreaufcffa692010-01-10 14:21:19 +01002859 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002860 goto failed_keep_alive;
2861
Willy Tarreau0f228a02015-05-01 15:37:53 +02002862 if (sess->fe->options & PR_O_IGNORE_PRB)
2863 goto failed_keep_alive;
2864
Willy Tarreau4076a152009-04-02 15:18:36 +02002865 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002866 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002867 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002868 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002869 msg->msg_state = HTTP_MSG_ERROR;
2870 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002871
Willy Tarreau87b09662015-04-03 00:22:06 +02002872 stream_inc_http_err_ctr(s);
2873 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002874 proxy_inc_fe_req_ctr(sess->fe);
2875 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002876 if (sess->listener->counters)
2877 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002878
Willy Tarreaue7dff022015-04-03 01:14:29 +02002879 if (!(s->flags & SF_FINST_MASK))
2880 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002881 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002882 }
2883
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002884 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002885 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002886 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002887#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002888 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2889 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002890 /* We need more data, we have to re-enable quick-ack in case we
2891 * previously disabled it, otherwise we might cause the client
2892 * to delay next data.
2893 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002894 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002895 }
2896#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002897
Willy Tarreaufcffa692010-01-10 14:21:19 +01002898 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2899 /* If the client starts to talk, let's fall back to
2900 * request timeout processing.
2901 */
2902 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002903 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002904 }
2905
Willy Tarreau59234e92008-11-30 23:51:27 +01002906 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002907 if (!tick_isset(req->analyse_exp)) {
2908 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2909 (txn->flags & TX_WAIT_NEXT_RQ) &&
2910 tick_isset(s->be->timeout.httpka))
2911 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2912 else
2913 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2914 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002915
Willy Tarreau59234e92008-11-30 23:51:27 +01002916 /* we're not ready yet */
2917 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002918
2919 failed_keep_alive:
2920 /* Here we process low-level errors for keep-alive requests. In
2921 * short, if the request is not the first one and it experiences
2922 * a timeout, read error or shutdown, we just silently close so
2923 * that the client can try again.
2924 */
2925 txn->status = 0;
2926 msg->msg_state = HTTP_MSG_RQBEFORE;
2927 req->analysers = 0;
2928 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002929 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002930 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau350f4872014-11-28 14:42:25 +01002931 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002932 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002933 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002934
Willy Tarreaud787e662009-07-07 10:14:51 +02002935 /* OK now we have a complete HTTP request with indexed headers. Let's
2936 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002937 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002938 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002939 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002940 * byte after the last LF. msg->sov points to the first byte of data.
2941 * msg->eol cannot be trusted because it may have been left uninitialized
2942 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002943 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002944
Willy Tarreau87b09662015-04-03 00:22:06 +02002945 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002946 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002947
Willy Tarreaub16a5742010-01-10 14:46:16 +01002948 if (txn->flags & TX_WAIT_NEXT_RQ) {
2949 /* kill the pending keep-alive timeout */
2950 txn->flags &= ~TX_WAIT_NEXT_RQ;
2951 req->analyse_exp = TICK_ETERNITY;
2952 }
2953
2954
Willy Tarreaud787e662009-07-07 10:14:51 +02002955 /* Maybe we found in invalid header name while we were configured not
2956 * to block on that, so we have to capture it now.
2957 */
2958 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002959 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002960
Willy Tarreau59234e92008-11-30 23:51:27 +01002961 /*
2962 * 1: identify the method
2963 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002964 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002965
2966 /* we can make use of server redirect on GET and HEAD */
2967 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002968 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002969
Willy Tarreau59234e92008-11-30 23:51:27 +01002970 /*
2971 * 2: check if the URI matches the monitor_uri.
2972 * We have to do this for every request which gets in, because
2973 * the monitor-uri is defined by the frontend.
2974 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002975 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2976 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002977 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002978 sess->fe->monitor_uri,
2979 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002980 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002981 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002982 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002983 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002984
Willy Tarreaue7dff022015-04-03 01:14:29 +02002985 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002986 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002987
Willy Tarreau59234e92008-11-30 23:51:27 +01002988 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002989 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002990 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002991
Willy Tarreau59234e92008-11-30 23:51:27 +01002992 ret = acl_pass(ret);
2993 if (cond->pol == ACL_COND_UNLESS)
2994 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002995
Willy Tarreau59234e92008-11-30 23:51:27 +01002996 if (ret) {
2997 /* we fail this request, let's return 503 service unavail */
2998 txn->status = 503;
Willy Tarreau350f4872014-11-28 14:42:25 +01002999 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02003000 if (!(s->flags & SF_ERR_MASK))
3001 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01003002 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01003003 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003004 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01003005
Willy Tarreau59234e92008-11-30 23:51:27 +01003006 /* nothing to fail, let's reply normaly */
3007 txn->status = 200;
Willy Tarreau350f4872014-11-28 14:42:25 +01003008 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02003009 if (!(s->flags & SF_ERR_MASK))
3010 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01003011 goto return_prx_cond;
3012 }
3013
3014 /*
3015 * 3: Maybe we have to copy the original REQURI for the logs ?
3016 * Note: we cannot log anymore if the request has been
3017 * classified as invalid.
3018 */
3019 if (unlikely(s->logs.logwait & LW_REQ)) {
3020 /* we have a complete HTTP request that we must log */
3021 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
3022 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003023
Willy Tarreau59234e92008-11-30 23:51:27 +01003024 if (urilen >= REQURI_LEN)
3025 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02003026 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01003027 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003028
Willy Tarreaud79a3b22012-12-28 09:40:16 +01003029 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01003030 s->do_log(s);
3031 } else {
3032 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003033 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003034 }
Willy Tarreau06619262006-12-17 08:37:22 +01003035
Willy Tarreau91852eb2015-05-01 13:26:00 +02003036 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
3037 * exactly one digit "." one digit. This check may be disabled using
3038 * option accept-invalid-http-request.
3039 */
3040 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
3041 if (msg->sl.rq.v_l != 8) {
3042 msg->err_pos = msg->sl.rq.v;
3043 goto return_bad_req;
3044 }
3045
3046 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
3047 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
3048 req->buf->p[msg->sl.rq.v + 6] != '.' ||
3049 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
3050 msg->err_pos = msg->sl.rq.v + 4;
3051 goto return_bad_req;
3052 }
3053 }
Willy Tarreau13317662015-05-01 13:47:08 +02003054 else {
3055 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
3056 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
3057 goto return_bad_req;
3058 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02003059
Willy Tarreau5b154472009-12-21 20:11:07 +01003060 /* ... and check if the request is HTTP/1.1 or above */
3061 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003062 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
3063 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
3064 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003065 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01003066
3067 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01003068 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 +01003069
Willy Tarreau88d349d2010-01-25 12:15:43 +01003070 /* if the frontend has "option http-use-proxy-header", we'll check if
3071 * we have what looks like a proxied connection instead of a connection,
3072 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
3073 * Note that this is *not* RFC-compliant, however browsers and proxies
3074 * happen to do that despite being non-standard :-(
3075 * We consider that a request not beginning with either '/' or '*' is
3076 * a proxied connection, which covers both "scheme://location" and
3077 * CONNECT ip:port.
3078 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003079 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003080 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01003081 txn->flags |= TX_USE_PX_CONN;
3082
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003083 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003084 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003085
Willy Tarreau59234e92008-11-30 23:51:27 +01003086 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003087 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003088 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003089 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003090
Willy Tarreau557f1992015-05-01 10:05:17 +02003091 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3092 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003093 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003094 * The length of a message body is determined by one of the following
3095 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003096 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003097 * 1. Any response to a HEAD request and any response with a 1xx
3098 * (Informational), 204 (No Content), or 304 (Not Modified) status
3099 * code is always terminated by the first empty line after the
3100 * header fields, regardless of the header fields present in the
3101 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003102 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003103 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3104 * the connection will become a tunnel immediately after the empty
3105 * line that concludes the header fields. A client MUST ignore any
3106 * Content-Length or Transfer-Encoding header fields received in
3107 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003108 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003109 * 3. If a Transfer-Encoding header field is present and the chunked
3110 * transfer coding (Section 4.1) is the final encoding, the message
3111 * body length is determined by reading and decoding the chunked
3112 * data until the transfer coding indicates the data is complete.
3113 *
3114 * If a Transfer-Encoding header field is present in a response and
3115 * the chunked transfer coding is not the final encoding, the
3116 * message body length is determined by reading the connection until
3117 * it is closed by the server. If a Transfer-Encoding header field
3118 * is present in a request and the chunked transfer coding is not
3119 * the final encoding, the message body length cannot be determined
3120 * reliably; the server MUST respond with the 400 (Bad Request)
3121 * status code and then close the connection.
3122 *
3123 * If a message is received with both a Transfer-Encoding and a
3124 * Content-Length header field, the Transfer-Encoding overrides the
3125 * Content-Length. Such a message might indicate an attempt to
3126 * perform request smuggling (Section 9.5) or response splitting
3127 * (Section 9.4) and ought to be handled as an error. A sender MUST
3128 * remove the received Content-Length field prior to forwarding such
3129 * a message downstream.
3130 *
3131 * 4. If a message is received without Transfer-Encoding and with
3132 * either multiple Content-Length header fields having differing
3133 * field-values or a single Content-Length header field having an
3134 * invalid value, then the message framing is invalid and the
3135 * recipient MUST treat it as an unrecoverable error. If this is a
3136 * request message, the server MUST respond with a 400 (Bad Request)
3137 * status code and then close the connection. If this is a response
3138 * message received by a proxy, the proxy MUST close the connection
3139 * to the server, discard the received response, and send a 502 (Bad
3140 * Gateway) response to the client. If this is a response message
3141 * received by a user agent, the user agent MUST close the
3142 * connection to the server and discard the received response.
3143 *
3144 * 5. If a valid Content-Length header field is present without
3145 * Transfer-Encoding, its decimal value defines the expected message
3146 * body length in octets. If the sender closes the connection or
3147 * the recipient times out before the indicated number of octets are
3148 * received, the recipient MUST consider the message to be
3149 * incomplete and close the connection.
3150 *
3151 * 6. If this is a request message and none of the above are true, then
3152 * the message body length is zero (no message body is present).
3153 *
3154 * 7. Otherwise, this is a response message without a declared message
3155 * body length, so the message body length is determined by the
3156 * number of octets received prior to the server closing the
3157 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003158 */
3159
Willy Tarreau32b47f42009-10-18 20:55:02 +02003160 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003161 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003162 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003163 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003164 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3165 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003166 /* chunked not last, return badreq */
3167 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003168 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003169 }
3170
Willy Tarreau1c913912015-04-30 10:57:51 +02003171 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003172 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003173 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3174 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3175 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3176 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003177 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003178 signed long long cl;
3179
Willy Tarreauad14f752011-09-02 20:33:27 +02003180 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003181 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003182 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003183 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003184
Willy Tarreauad14f752011-09-02 20:33:27 +02003185 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003186 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003187 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003188 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003189
Willy Tarreauad14f752011-09-02 20:33:27 +02003190 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003191 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003192 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003193 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003194
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003195 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003196 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003197 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003198 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003199
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003200 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003201 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003202 }
3203
Willy Tarreau34dfc602015-05-01 10:09:49 +02003204 /* even bodyless requests have a known length */
3205 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003206
Willy Tarreau179085c2014-04-28 16:48:56 +02003207 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3208 * only change if both the request and the config reference something else.
3209 * Option httpclose by itself sets tunnel mode where headers are mangled.
3210 * However, if another mode is set, it will affect it (eg: server-close/
3211 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3212 * if FE and BE have the same settings (common). The method consists in
3213 * checking if options changed between the two calls (implying that either
3214 * one is non-null, or one of them is non-null and we are there for the first
3215 * time.
3216 */
3217 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003218 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003219 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003220
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003221 /* we may have to wait for the request's body */
3222 if ((s->be->options & PR_O_WREQ_BODY) &&
3223 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3224 req->analysers |= AN_REQ_HTTP_BODY;
3225
Willy Tarreaud787e662009-07-07 10:14:51 +02003226 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003227 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003228 req->analyse_exp = TICK_ETERNITY;
3229 return 1;
3230
3231 return_bad_req:
3232 /* We centralize bad requests processing here */
3233 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3234 /* we detected a parsing error. We want to archive this request
3235 * in the dedicated proxy area for later troubleshooting.
3236 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003237 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003238 }
3239
3240 txn->req.msg_state = HTTP_MSG_ERROR;
3241 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01003242 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003243
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003244 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003245 if (sess->listener->counters)
3246 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003247
3248 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003249 if (!(s->flags & SF_ERR_MASK))
3250 s->flags |= SF_ERR_PRXCOND;
3251 if (!(s->flags & SF_FINST_MASK))
3252 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003253
3254 req->analysers = 0;
3255 req->analyse_exp = TICK_ETERNITY;
3256 return 0;
3257}
3258
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003259
Willy Tarreau347a35d2013-11-22 17:51:09 +01003260/* This function prepares an applet to handle the stats. It can deal with the
3261 * "100-continue" expectation, check that admin rules are met for POST requests,
3262 * and program a response message if something was unexpected. It cannot fail
3263 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003264 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003265 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003266 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003267 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003268int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003269{
3270 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003271 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003272 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003273 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003274 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003275 struct uri_auth *uri_auth = s->be->uri_auth;
3276 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003277 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003278
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003279 appctx = si_appctx(si);
3280 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3281 appctx->st1 = appctx->st2 = 0;
3282 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3283 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003284 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003285 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003286
3287 uri = msg->chn->buf->p + msg->sl.rq.u;
3288 lookup = uri + uri_auth->uri_len;
3289
3290 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3291 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003292 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003293 break;
3294 }
3295 }
3296
3297 if (uri_auth->refresh) {
3298 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3299 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003300 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003301 break;
3302 }
3303 }
3304 }
3305
3306 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3307 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003308 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003309 break;
3310 }
3311 }
3312
3313 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3314 if (memcmp(h, ";st=", 4) == 0) {
3315 int i;
3316 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003317 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003318 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3319 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003320 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003321 break;
3322 }
3323 }
3324 break;
3325 }
3326 }
3327
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003328 appctx->ctx.stats.scope_str = 0;
3329 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003330 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3331 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3332 int itx = 0;
3333 const char *h2;
3334 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3335 const char *err;
3336
3337 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3338 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003339 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003340 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3341 itx++;
3342 h++;
3343 }
3344
3345 if (itx > STAT_SCOPE_TXT_MAXLEN)
3346 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003347 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003348
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003349 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003350 memcpy(scope_txt, h2, itx);
3351 scope_txt[itx] = '\0';
3352 err = invalid_char(scope_txt);
3353 if (err) {
3354 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003355 appctx->ctx.stats.scope_str = 0;
3356 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003357 }
3358 break;
3359 }
3360 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003361
3362 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003363 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003364 int ret = 1;
3365
3366 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003367 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 +01003368 ret = acl_pass(ret);
3369 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3370 ret = !ret;
3371 }
3372
3373 if (ret) {
3374 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003375 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003376 break;
3377 }
3378 }
3379
3380 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003381 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003382 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003383 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003384 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3385 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003386 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003387 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003388 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003389 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3390 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003391 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003392 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003393 else {
3394 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003395 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003396 }
3397
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003398 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003399 return 1;
3400}
3401
Lukas Tribus67db8df2013-06-23 17:37:13 +02003402/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3403 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3404 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003405void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003406{
3407#ifdef IP_TOS
3408 if (from.ss_family == AF_INET)
3409 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3410#endif
3411#ifdef IPV6_TCLASS
3412 if (from.ss_family == AF_INET6) {
3413 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3414 /* v4-mapped addresses need IP_TOS */
3415 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3416 else
3417 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3418 }
3419#endif
3420}
3421
Willy Tarreau87b09662015-04-03 00:22:06 +02003422int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003423 const char* name, unsigned int name_len,
3424 const char *str, struct my_regex *re,
3425 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003426{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003427 struct hdr_ctx ctx;
3428 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003429 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003430 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3431 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003432 struct chunk *output = get_trash_chunk();
3433
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003434 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003435
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003436 /* Choose the header browsing function. */
3437 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003438 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003439 http_find_hdr_func = http_find_header2;
3440 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003441 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003442 http_find_hdr_func = http_find_full_header2;
3443 break;
3444 default: /* impossible */
3445 return -1;
3446 }
3447
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003448 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3449 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003450 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003451 char *val = ctx.line + ctx.val;
3452 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003453
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003454 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3455 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003456
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003457 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003458 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003459 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003460
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003461 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003462
3463 hdr->len += delta;
3464 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003465
3466 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003467 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003468 }
3469
3470 return 0;
3471}
3472
Willy Tarreau87b09662015-04-03 00:22:06 +02003473static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003474 const char* name, unsigned int name_len,
3475 struct list *fmt, struct my_regex *re,
3476 int action)
3477{
3478 struct chunk *replace = get_trash_chunk();
3479
3480 replace->len = build_logline(s, replace->str, replace->size, fmt);
3481 if (replace->len >= replace->size - 1)
3482 return -1;
3483
3484 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3485}
3486
Willy Tarreau87b09662015-04-03 00:22:06 +02003487/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003488 * transaction <txn>. Returns the verdict of the first rule that prevents
3489 * further processing of the request (auth, deny, ...), and defaults to
3490 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3491 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3492 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003493 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003494enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003495http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003496{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003497 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003498 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003499 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003500 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003501 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003502 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003503 int act_flags = 0;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003504
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003505 /* If "the current_rule_list" match the executed rule list, we are in
3506 * resume condition. If a resume is needed it is always in the action
3507 * and never in the ACL or converters. In this case, we initialise the
3508 * current rule, and go to the action execution point.
3509 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003510 if (s->current_rule) {
3511 rule = s->current_rule;
3512 s->current_rule = NULL;
3513 if (s->current_rule_list == rules)
3514 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003515 }
3516 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003517
Willy Tarreauff011f22011-01-06 17:51:27 +01003518 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003519
Willy Tarreau96257ec2012-12-27 10:46:37 +01003520 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003521 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003522 int ret;
3523
Willy Tarreau192252e2015-04-04 01:47:55 +02003524 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003525 ret = acl_pass(ret);
3526
Willy Tarreauff011f22011-01-06 17:51:27 +01003527 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003528 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003529
3530 if (!ret) /* condition not matched */
3531 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003532 }
3533
Willy Tarreauacc98002015-09-27 23:34:39 +02003534 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003535resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003536 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003537 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003538 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003539
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003540 case ACT_ACTION_DENY:
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 Tarreau96257ec2012-12-27 10:46:37 +01003543
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003544 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003545 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003546 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003547 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003548
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003549 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003550 /* Auth might be performed on regular http-req rules as well as on stats */
3551 auth_realm = rule->arg.auth.realm;
3552 if (!auth_realm) {
3553 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3554 auth_realm = STATS_DEFAULT_REALM;
3555 else
3556 auth_realm = px->id;
3557 }
3558 /* send 401/407 depending on whether we use a proxy or not. We still
3559 * count one error, because normal browsing won't significantly
3560 * increase the counter but brute force attempts will.
3561 */
3562 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3563 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3564 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003565 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003566 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003567
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003568 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003569 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3570 return HTTP_RULE_RES_BADREQ;
3571 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003572
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003573 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003574 s->task->nice = rule->arg.nice;
3575 break;
3576
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003577 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003578 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003579 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003580 break;
3581
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003582 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003583#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003584 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003585 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003586#endif
3587 break;
3588
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003589 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003590 s->logs.level = rule->arg.loglevel;
3591 break;
3592
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003593 case ACT_HTTP_REPLACE_HDR:
3594 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003595 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3596 rule->arg.hdr_add.name_len,
3597 &rule->arg.hdr_add.fmt,
3598 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003599 return HTTP_RULE_RES_BADREQ;
3600 break;
3601
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003602 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003603 ctx.idx = 0;
3604 /* remove all occurrences of the header */
3605 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3606 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3607 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003608 }
Willy Tarreau85603282015-01-21 20:39:27 +01003609 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003610
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003611 case ACT_HTTP_SET_HDR:
3612 case ACT_HTTP_ADD_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003613 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3614 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3615 trash.len = rule->arg.hdr_add.name_len;
3616 trash.str[trash.len++] = ':';
3617 trash.str[trash.len++] = ' ';
3618 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 +01003619
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003620 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003621 /* remove all occurrences of the header */
3622 ctx.idx = 0;
3623 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3624 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3625 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3626 }
3627 }
3628
Willy Tarreau96257ec2012-12-27 10:46:37 +01003629 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3630 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003631
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003632 case ACT_HTTP_DEL_ACL:
3633 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003634 struct pat_ref *ref;
3635 char *key;
3636 int len;
3637
3638 /* collect reference */
3639 ref = pat_ref_lookup(rule->arg.map.ref);
3640 if (!ref)
3641 continue;
3642
3643 /* collect key */
3644 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3645 key = trash.str;
3646 key[len] = '\0';
3647
3648 /* perform update */
3649 /* returned code: 1=ok, 0=ko */
3650 pat_ref_delete(ref, key);
3651
3652 break;
3653 }
3654
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003655 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003656 struct pat_ref *ref;
3657 char *key;
3658 struct chunk *trash_key;
3659 int len;
3660
3661 trash_key = get_trash_chunk();
3662
3663 /* collect reference */
3664 ref = pat_ref_lookup(rule->arg.map.ref);
3665 if (!ref)
3666 continue;
3667
3668 /* collect key */
3669 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3670 key = trash_key->str;
3671 key[len] = '\0';
3672
3673 /* perform update */
3674 /* add entry only if it does not already exist */
3675 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003676 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003677
3678 break;
3679 }
3680
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003681 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003682 struct pat_ref *ref;
3683 char *key, *value;
3684 struct chunk *trash_key, *trash_value;
3685 int len;
3686
3687 trash_key = get_trash_chunk();
3688 trash_value = get_trash_chunk();
3689
3690 /* collect reference */
3691 ref = pat_ref_lookup(rule->arg.map.ref);
3692 if (!ref)
3693 continue;
3694
3695 /* collect key */
3696 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3697 key = trash_key->str;
3698 key[len] = '\0';
3699
3700 /* collect value */
3701 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3702 value = trash_value->str;
3703 value[len] = '\0';
3704
3705 /* perform update */
3706 if (pat_ref_find_elt(ref, key) != NULL)
3707 /* update entry if it exists */
3708 pat_ref_set(ref, key, value, NULL);
3709 else
3710 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003711 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003712
3713 break;
3714 }
William Lallemand73025dd2014-04-24 14:38:37 +02003715
Thierry FOURNIER42148732015-09-02 17:17:33 +02003716 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003717 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3718 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003719
Willy Tarreauacc98002015-09-27 23:34:39 +02003720 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003721 case ACT_RET_ERR:
3722 case ACT_RET_CONT:
3723 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003724 case ACT_RET_STOP:
3725 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003726 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003727 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003728 return HTTP_RULE_RES_YIELD;
3729 }
William Lallemand73025dd2014-04-24 14:38:37 +02003730 break;
3731
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003732 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003733 /* Note: only the first valid tracking parameter of each
3734 * applies.
3735 */
3736
3737 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3738 struct stktable *t;
3739 struct stksess *ts;
3740 struct stktable_key *key;
3741 void *ptr;
3742
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003743 t = rule->arg.trk_ctr.table.t;
3744 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 +02003745
3746 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003747 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003748
3749 /* let's count a new HTTP request as it's the first time we do it */
3750 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3751 if (ptr)
3752 stktable_data_cast(ptr, http_req_cnt)++;
3753
3754 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3755 if (ptr)
3756 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3757 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3758
3759 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003760 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003761 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3762 }
3763 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003764 break;
3765
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003766 case ACT_HTTP_REQ_SET_SRC:
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003767 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
3768 struct sample *smp;
3769
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02003770 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 +02003771
3772 if (smp) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003773 if (smp->data.type == SMP_T_IPV4) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003774 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003775 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003776 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003777 } else if (smp->data.type == SMP_T_IPV6) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003778 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003779 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 +02003780 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0;
3781 }
3782 }
3783 }
3784 break;
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003785
3786 /* other flags exists, but normaly, they never be matched. */
3787 default:
3788 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003789 }
3790 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003791
3792 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003793 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003794}
3795
Willy Tarreau71241ab2012-12-27 11:30:54 +01003796
Willy Tarreau51d861a2015-05-22 17:30:48 +02003797/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3798 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3799 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3800 * is returned, the process can continue the evaluation of next rule list. If
3801 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3802 * is returned, it means the operation could not be processed and a server error
3803 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3804 * deny rule. If *YIELD is returned, the caller must call again the function
3805 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003806 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003807static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003808http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003809{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003810 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003811 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003812 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003813 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003814 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003815 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003816
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003817 /* If "the current_rule_list" match the executed rule list, we are in
3818 * resume condition. If a resume is needed it is always in the action
3819 * and never in the ACL or converters. In this case, we initialise the
3820 * current rule, and go to the action execution point.
3821 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003822 if (s->current_rule) {
3823 rule = s->current_rule;
3824 s->current_rule = NULL;
3825 if (s->current_rule_list == rules)
3826 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003827 }
3828 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003829
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003830 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003831
3832 /* check optional condition */
3833 if (rule->cond) {
3834 int ret;
3835
Willy Tarreau192252e2015-04-04 01:47:55 +02003836 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003837 ret = acl_pass(ret);
3838
3839 if (rule->cond->pol == ACL_COND_UNLESS)
3840 ret = !ret;
3841
3842 if (!ret) /* condition not matched */
3843 continue;
3844 }
3845
Willy Tarreauacc98002015-09-27 23:34:39 +02003846 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003847resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003848 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003849 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003850 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003851
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003852 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003853 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003854 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003855
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003856 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003857 s->task->nice = rule->arg.nice;
3858 break;
3859
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003860 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003861 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003862 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003863 break;
3864
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003865 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003866#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003867 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003868 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003869#endif
3870 break;
3871
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003872 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003873 s->logs.level = rule->arg.loglevel;
3874 break;
3875
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003876 case ACT_HTTP_REPLACE_HDR:
3877 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003878 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3879 rule->arg.hdr_add.name_len,
3880 &rule->arg.hdr_add.fmt,
3881 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003882 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003883 break;
3884
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003885 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003886 ctx.idx = 0;
3887 /* remove all occurrences of the header */
3888 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3889 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3890 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3891 }
Willy Tarreau85603282015-01-21 20:39:27 +01003892 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003893
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003894 case ACT_HTTP_SET_HDR:
3895 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003896 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3897 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3898 trash.len = rule->arg.hdr_add.name_len;
3899 trash.str[trash.len++] = ':';
3900 trash.str[trash.len++] = ' ';
3901 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 +01003902
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003903 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003904 /* remove all occurrences of the header */
3905 ctx.idx = 0;
3906 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3907 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3908 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3909 }
3910 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003911 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3912 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003913
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003914 case ACT_HTTP_DEL_ACL:
3915 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003916 struct pat_ref *ref;
3917 char *key;
3918 int len;
3919
3920 /* collect reference */
3921 ref = pat_ref_lookup(rule->arg.map.ref);
3922 if (!ref)
3923 continue;
3924
3925 /* collect key */
3926 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3927 key = trash.str;
3928 key[len] = '\0';
3929
3930 /* perform update */
3931 /* returned code: 1=ok, 0=ko */
3932 pat_ref_delete(ref, key);
3933
3934 break;
3935 }
3936
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003937 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003938 struct pat_ref *ref;
3939 char *key;
3940 struct chunk *trash_key;
3941 int len;
3942
3943 trash_key = get_trash_chunk();
3944
3945 /* collect reference */
3946 ref = pat_ref_lookup(rule->arg.map.ref);
3947 if (!ref)
3948 continue;
3949
3950 /* collect key */
3951 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3952 key = trash_key->str;
3953 key[len] = '\0';
3954
3955 /* perform update */
3956 /* check if the entry already exists */
3957 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003958 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003959
3960 break;
3961 }
3962
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003963 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003964 struct pat_ref *ref;
3965 char *key, *value;
3966 struct chunk *trash_key, *trash_value;
3967 int len;
3968
3969 trash_key = get_trash_chunk();
3970 trash_value = get_trash_chunk();
3971
3972 /* collect reference */
3973 ref = pat_ref_lookup(rule->arg.map.ref);
3974 if (!ref)
3975 continue;
3976
3977 /* collect key */
3978 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3979 key = trash_key->str;
3980 key[len] = '\0';
3981
3982 /* collect value */
3983 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3984 value = trash_value->str;
3985 value[len] = '\0';
3986
3987 /* perform update */
3988 if (pat_ref_find_elt(ref, key) != NULL)
3989 /* update entry if it exists */
3990 pat_ref_set(ref, key, value, NULL);
3991 else
3992 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003993 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003994
3995 break;
3996 }
William Lallemand73025dd2014-04-24 14:38:37 +02003997
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003998 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003999 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
4000 return HTTP_RULE_RES_BADREQ;
4001 return HTTP_RULE_RES_DONE;
4002
Thierry FOURNIER42148732015-09-02 17:17:33 +02004003 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02004004 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
4005 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02004006
Willy Tarreauacc98002015-09-27 23:34:39 +02004007 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004008 case ACT_RET_ERR:
4009 case ACT_RET_CONT:
4010 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02004011 case ACT_RET_STOP:
4012 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004013 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02004014 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004015 return HTTP_RULE_RES_YIELD;
4016 }
William Lallemand73025dd2014-04-24 14:38:37 +02004017 break;
4018
Thierry FOURNIER22e49012015-08-05 19:13:48 +02004019 /* other flags exists, but normaly, they never be matched. */
4020 default:
4021 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004022 }
4023 }
4024
4025 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004026 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004027}
4028
4029
Willy Tarreau71241ab2012-12-27 11:30:54 +01004030/* Perform an HTTP redirect based on the information in <rule>. The function
4031 * returns non-zero on success, or zero in case of a, irrecoverable error such
4032 * as too large a request to build a valid response.
4033 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004034static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004035{
Willy Tarreaub329a312015-05-22 16:27:37 +02004036 struct http_msg *req = &txn->req;
4037 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004038 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004039 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004040
4041 /* build redirect message */
4042 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004043 case 308:
4044 msg_fmt = HTTP_308;
4045 break;
4046 case 307:
4047 msg_fmt = HTTP_307;
4048 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004049 case 303:
4050 msg_fmt = HTTP_303;
4051 break;
4052 case 301:
4053 msg_fmt = HTTP_301;
4054 break;
4055 case 302:
4056 default:
4057 msg_fmt = HTTP_302;
4058 break;
4059 }
4060
4061 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
4062 return 0;
4063
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004064 location = trash.str + trash.len;
4065
Willy Tarreau71241ab2012-12-27 11:30:54 +01004066 switch(rule->type) {
4067 case REDIRECT_TYPE_SCHEME: {
4068 const char *path;
4069 const char *host;
4070 struct hdr_ctx ctx;
4071 int pathlen;
4072 int hostlen;
4073
4074 host = "";
4075 hostlen = 0;
4076 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004077 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004078 host = ctx.line + ctx.val;
4079 hostlen = ctx.vlen;
4080 }
4081
4082 path = http_get_path(txn);
4083 /* build message using path */
4084 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004085 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004086 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4087 int qs = 0;
4088 while (qs < pathlen) {
4089 if (path[qs] == '?') {
4090 pathlen = qs;
4091 break;
4092 }
4093 qs++;
4094 }
4095 }
4096 } else {
4097 path = "/";
4098 pathlen = 1;
4099 }
4100
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004101 if (rule->rdr_str) { /* this is an old "redirect" rule */
4102 /* check if we can add scheme + "://" + host + path */
4103 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
4104 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004105
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004106 /* add scheme */
4107 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4108 trash.len += rule->rdr_len;
4109 }
4110 else {
4111 /* add scheme with executing log format */
4112 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004113
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004114 /* check if we can add scheme + "://" + host + path */
4115 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
4116 return 0;
4117 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004118 /* add "://" */
4119 memcpy(trash.str + trash.len, "://", 3);
4120 trash.len += 3;
4121
4122 /* add host */
4123 memcpy(trash.str + trash.len, host, hostlen);
4124 trash.len += hostlen;
4125
4126 /* add path */
4127 memcpy(trash.str + trash.len, path, pathlen);
4128 trash.len += pathlen;
4129
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004130 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004131 if (trash.len && trash.str[trash.len - 1] != '/' &&
4132 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4133 if (trash.len > trash.size - 5)
4134 return 0;
4135 trash.str[trash.len] = '/';
4136 trash.len++;
4137 }
4138
4139 break;
4140 }
4141 case REDIRECT_TYPE_PREFIX: {
4142 const char *path;
4143 int pathlen;
4144
4145 path = http_get_path(txn);
4146 /* build message using path */
4147 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004148 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004149 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4150 int qs = 0;
4151 while (qs < pathlen) {
4152 if (path[qs] == '?') {
4153 pathlen = qs;
4154 break;
4155 }
4156 qs++;
4157 }
4158 }
4159 } else {
4160 path = "/";
4161 pathlen = 1;
4162 }
4163
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004164 if (rule->rdr_str) { /* this is an old "redirect" rule */
4165 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4166 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004167
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004168 /* add prefix. Note that if prefix == "/", we don't want to
4169 * add anything, otherwise it makes it hard for the user to
4170 * configure a self-redirection.
4171 */
4172 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4173 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4174 trash.len += rule->rdr_len;
4175 }
4176 }
4177 else {
4178 /* add prefix with executing log format */
4179 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4180
4181 /* Check length */
4182 if (trash.len + pathlen > trash.size - 4)
4183 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004184 }
4185
4186 /* add path */
4187 memcpy(trash.str + trash.len, path, pathlen);
4188 trash.len += pathlen;
4189
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004190 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004191 if (trash.len && trash.str[trash.len - 1] != '/' &&
4192 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4193 if (trash.len > trash.size - 5)
4194 return 0;
4195 trash.str[trash.len] = '/';
4196 trash.len++;
4197 }
4198
4199 break;
4200 }
4201 case REDIRECT_TYPE_LOCATION:
4202 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004203 if (rule->rdr_str) { /* this is an old "redirect" rule */
4204 if (trash.len + rule->rdr_len > trash.size - 4)
4205 return 0;
4206
4207 /* add location */
4208 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4209 trash.len += rule->rdr_len;
4210 }
4211 else {
4212 /* add location with executing log format */
4213 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004214
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004215 /* Check left length */
4216 if (trash.len > trash.size - 4)
4217 return 0;
4218 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004219 break;
4220 }
4221
4222 if (rule->cookie_len) {
4223 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4224 trash.len += 14;
4225 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4226 trash.len += rule->cookie_len;
4227 memcpy(trash.str + trash.len, "\r\n", 2);
4228 trash.len += 2;
4229 }
4230
4231 /* add end of headers and the keep-alive/close status.
4232 * We may choose to set keep-alive if the Location begins
4233 * with a slash, because the client will come back to the
4234 * same server.
4235 */
4236 txn->status = rule->code;
4237 /* let's log the request time */
4238 s->logs.tv_request = now;
4239
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004240 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004241 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004242 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004243 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4244 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4245 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004246 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004247 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4248 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4249 trash.len += 30;
4250 } else {
4251 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4252 trash.len += 24;
4253 }
4254 }
4255 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4256 trash.len += 4;
Willy Tarreaub329a312015-05-22 16:27:37 +02004257 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004258 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004259 bi_fast_delete(req->chn->buf, req->sov);
4260 req->next -= req->sov;
4261 req->sov = 0;
4262 s->req.analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004263 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreaub329a312015-05-22 16:27:37 +02004264 req->msg_state = HTTP_MSG_CLOSED;
4265 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004266 /* Trim any possible response */
4267 res->chn->buf->i = 0;
4268 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004269 } else {
4270 /* keep-alive not possible */
4271 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4272 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4273 trash.len += 29;
4274 } else {
4275 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4276 trash.len += 23;
4277 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004278 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreaub329a312015-05-22 16:27:37 +02004279 req->chn->analysers = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004280 }
4281
Willy Tarreaue7dff022015-04-03 01:14:29 +02004282 if (!(s->flags & SF_ERR_MASK))
4283 s->flags |= SF_ERR_LOCAL;
4284 if (!(s->flags & SF_FINST_MASK))
4285 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004286
4287 return 1;
4288}
4289
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004290/* This stream analyser runs all HTTP request processing which is common to
4291 * frontends and backends, which means blocking ACLs, filters, connection-close,
4292 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004293 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004294 * either needs more data or wants to immediately abort the request (eg: deny,
4295 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004296 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004297int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004298{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004299 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004300 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004301 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004302 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004303 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004304 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004305
Willy Tarreau655dce92009-11-08 13:10:58 +01004306 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004307 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004308 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004309 }
4310
Willy Tarreau87b09662015-04-03 00:22:06 +02004311 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 +02004312 now_ms, __FUNCTION__,
4313 s,
4314 req,
4315 req->rex, req->wex,
4316 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004317 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004318 req->analysers);
4319
Willy Tarreau65410832014-04-28 21:25:43 +02004320 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004321 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004322
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004323 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004324 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004325 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004326
Willy Tarreau0b748332014-04-29 00:13:29 +02004327 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004328 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4329 goto return_prx_yield;
4330
Willy Tarreau0b748332014-04-29 00:13:29 +02004331 case HTTP_RULE_RES_CONT:
4332 case HTTP_RULE_RES_STOP: /* nothing to do */
4333 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004334
Willy Tarreau0b748332014-04-29 00:13:29 +02004335 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4336 if (txn->flags & TX_CLTARPIT)
4337 goto tarpit;
4338 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004339
Willy Tarreau0b748332014-04-29 00:13:29 +02004340 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4341 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004342
Willy Tarreau0b748332014-04-29 00:13:29 +02004343 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004344 goto done;
4345
Willy Tarreau0b748332014-04-29 00:13:29 +02004346 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4347 goto return_bad_req;
4348 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004349 }
4350
Willy Tarreau52542592014-04-28 18:33:26 +02004351 /* OK at this stage, we know that the request was accepted according to
4352 * the http-request rules, we can check for the stats. Note that the
4353 * URI is detected *before* the req* rules in order not to be affected
4354 * by a possible reqrep, while they are processed *after* so that a
4355 * reqdeny can still block them. This clearly needs to change in 1.6!
4356 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004357 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004358 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004359 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004360 txn->status = 500;
4361 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004362 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004363
Willy Tarreaue7dff022015-04-03 01:14:29 +02004364 if (!(s->flags & SF_ERR_MASK))
4365 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004366 goto return_prx_cond;
4367 }
4368
4369 /* parse the whole stats request and extract the relevant information */
4370 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004371 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004372 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004373
Willy Tarreau0b748332014-04-29 00:13:29 +02004374 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4375 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004376
Willy Tarreau0b748332014-04-29 00:13:29 +02004377 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4378 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004379 }
4380
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004381 /* evaluate the req* rules except reqadd */
4382 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004383 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004384 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004385
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004386 if (txn->flags & TX_CLDENY)
4387 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004388
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004389 if (txn->flags & TX_CLTARPIT)
4390 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004391 }
Willy Tarreau06619262006-12-17 08:37:22 +01004392
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004393 /* add request headers from the rule sets in the same order */
4394 list_for_each_entry(wl, &px->req_add, list) {
4395 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004396 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004397 ret = acl_pass(ret);
4398 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4399 ret = !ret;
4400 if (!ret)
4401 continue;
4402 }
4403
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004404 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004405 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004406 }
4407
Willy Tarreau52542592014-04-28 18:33:26 +02004408
4409 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004410 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004411 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004412 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4413 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004414
Willy Tarreaue7dff022015-04-03 01:14:29 +02004415 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4416 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4417 if (!(s->flags & SF_FINST_MASK))
4418 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004419
Willy Tarreau70730dd2014-04-24 18:06:27 +02004420 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004421 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004422 select_compression_request_header(s, req->buf);
4423
4424 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004425 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004426 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004427 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004428
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004429 /* check whether we have some ACLs set to redirect this request */
4430 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004431 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004432 int ret;
4433
Willy Tarreau192252e2015-04-04 01:47:55 +02004434 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004435 ret = acl_pass(ret);
4436 if (rule->cond->pol == ACL_COND_UNLESS)
4437 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004438 if (!ret)
4439 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004440 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004441 if (!http_apply_redirect_rule(rule, s, txn))
4442 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004443 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004444 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004445
Willy Tarreau2be39392010-01-03 17:24:51 +01004446 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4447 * If this happens, then the data will not come immediately, so we must
4448 * send all what we have without waiting. Note that due to the small gain
4449 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004450 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004451 * itself once used.
4452 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004453 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004454
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004455 done: /* done with this analyser, continue with next ones that the calling
4456 * points will have set, if any.
4457 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004458 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004459 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4460 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004461 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004462
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004463 tarpit:
4464 /* When a connection is tarpitted, we use the tarpit timeout,
4465 * which may be the same as the connect timeout if unspecified.
4466 * If unset, then set it to zero because we really want it to
4467 * eventually expire. We build the tarpit as an analyser.
4468 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004469 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004470
4471 /* wipe the request out so that we can drop the connection early
4472 * if the client closes first.
4473 */
4474 channel_dont_connect(req);
4475 req->analysers = 0; /* remove switching rules etc... */
4476 req->analysers |= AN_REQ_HTTP_TARPIT;
4477 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4478 if (!req->analyse_exp)
4479 req->analyse_exp = tick_add(now_ms, 0);
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++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004486 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004487
4488 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004489 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004490 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004491 s->logs.tv_request = now;
CJ Ess108b1dd2015-04-07 12:03:37 -04004492 stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004493 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004494 sess->fe->fe_counters.denied_req++;
4495 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004496 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004497 if (sess->listener->counters)
4498 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004499 goto return_prx_cond;
4500
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004501 return_bad_req:
4502 /* We centralize bad requests processing here */
4503 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4504 /* we detected a parsing error. We want to archive this request
4505 * in the dedicated proxy area for later troubleshooting.
4506 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004507 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004508 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004509
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004510 txn->req.msg_state = HTTP_MSG_ERROR;
4511 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004512 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004513
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004514 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004515 if (sess->listener->counters)
4516 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004517
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004518 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004519 if (!(s->flags & SF_ERR_MASK))
4520 s->flags |= SF_ERR_PRXCOND;
4521 if (!(s->flags & SF_FINST_MASK))
4522 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004523
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004524 req->analysers = 0;
4525 req->analyse_exp = TICK_ETERNITY;
4526 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004527
4528 return_prx_yield:
4529 channel_dont_connect(req);
4530 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004531}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004532
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004533/* This function performs all the processing enabled for the current request.
4534 * It returns 1 if the processing can continue on next analysers, or zero if it
4535 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004536 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004537 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004538int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004539{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004540 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004541 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004542 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004543 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004544
Willy Tarreau655dce92009-11-08 13:10:58 +01004545 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004546 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004547 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004548 return 0;
4549 }
4550
Willy Tarreau87b09662015-04-03 00:22:06 +02004551 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 +02004552 now_ms, __FUNCTION__,
4553 s,
4554 req,
4555 req->rex, req->wex,
4556 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004557 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004558 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004559
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004560 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004561 select_compression_request_header(s, req->buf);
4562
Willy Tarreau59234e92008-11-30 23:51:27 +01004563 /*
4564 * Right now, we know that we have processed the entire headers
4565 * and that unwanted requests have been filtered out. We can do
4566 * whatever we want with the remaining request. Also, now we
4567 * may have separate values for ->fe, ->be.
4568 */
Willy Tarreau06619262006-12-17 08:37:22 +01004569
Willy Tarreau59234e92008-11-30 23:51:27 +01004570 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004571 * If HTTP PROXY is set we simply get remote server address parsing
4572 * incoming request. Note that this requires that a connection is
4573 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004574 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004575 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004576 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004577 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004578
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004579 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004580 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004581 txn->req.msg_state = HTTP_MSG_ERROR;
4582 txn->status = 500;
4583 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004584 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004585
Willy Tarreaue7dff022015-04-03 01:14:29 +02004586 if (!(s->flags & SF_ERR_MASK))
4587 s->flags |= SF_ERR_RESOURCE;
4588 if (!(s->flags & SF_FINST_MASK))
4589 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004590
4591 return 0;
4592 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004593
4594 path = http_get_path(txn);
4595 url2sa(req->buf->p + msg->sl.rq.u,
4596 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004597 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004598 /* if the path was found, we have to remove everything between
4599 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4600 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4601 * u_l characters by a single "/".
4602 */
4603 if (path) {
4604 char *cur_ptr = req->buf->p;
4605 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4606 int delta;
4607
4608 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4609 http_msg_move_end(&txn->req, delta);
4610 cur_end += delta;
4611 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4612 goto return_bad_req;
4613 }
4614 else {
4615 char *cur_ptr = req->buf->p;
4616 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4617 int delta;
4618
4619 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4620 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4621 http_msg_move_end(&txn->req, delta);
4622 cur_end += delta;
4623 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4624 goto return_bad_req;
4625 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004626 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004627
Willy Tarreau59234e92008-11-30 23:51:27 +01004628 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004629 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004630 * Note that doing so might move headers in the request, but
4631 * the fields will stay coherent and the URI will not move.
4632 * This should only be performed in the backend.
4633 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02004634 if ((s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004635 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4636 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004637
William Lallemanda73203e2012-03-12 12:48:57 +01004638 /* add unique-id if "header-unique-id" is specified */
4639
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004640 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004641 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4642 goto return_bad_req;
4643 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004644 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004645 }
William Lallemanda73203e2012-03-12 12:48:57 +01004646
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004647 if (sess->fe->header_unique_id && s->unique_id) {
4648 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004649 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004650 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004651 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004652 goto return_bad_req;
4653 }
4654
Cyril Bontéb21570a2009-11-29 20:04:48 +01004655 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004656 * 9: add X-Forwarded-For if either the frontend or the backend
4657 * asks for it.
4658 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004659 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004660 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004661 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4662 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4663 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004664 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004665 /* The header is set to be added only if none is present
4666 * and we found it, so don't do anything.
4667 */
4668 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004669 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004670 /* Add an X-Forwarded-For header unless the source IP is
4671 * in the 'except' network range.
4672 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004673 if ((!sess->fe->except_mask.s_addr ||
4674 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4675 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004676 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004677 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004678 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004679 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004680 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004681 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004682
4683 /* Note: we rely on the backend to get the header name to be used for
4684 * x-forwarded-for, because the header is really meant for the backends.
4685 * However, if the backend did not specify any option, we have to rely
4686 * on the frontend's header name.
4687 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004688 if (s->be->fwdfor_hdr_len) {
4689 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004690 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004691 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004692 len = sess->fe->fwdfor_hdr_len;
4693 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004694 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004695 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 +01004696
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004697 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004698 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004699 }
4700 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004701 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004702 /* FIXME: for the sake of completeness, we should also support
4703 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004704 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004705 int len;
4706 char pn[INET6_ADDRSTRLEN];
4707 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004708 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004709 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004710
Willy Tarreau59234e92008-11-30 23:51:27 +01004711 /* Note: we rely on the backend to get the header name to be used for
4712 * x-forwarded-for, because the header is really meant for the backends.
4713 * However, if the backend did not specify any option, we have to rely
4714 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004715 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004716 if (s->be->fwdfor_hdr_len) {
4717 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004718 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004719 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004720 len = sess->fe->fwdfor_hdr_len;
4721 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004722 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004723 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004724
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004725 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004726 goto return_bad_req;
4727 }
4728 }
4729
4730 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004731 * 10: add X-Original-To if either the frontend or the backend
4732 * asks for it.
4733 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004734 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004735
4736 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004737 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004738 /* Add an X-Original-To header unless the destination IP is
4739 * in the 'except' network range.
4740 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004741 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004742
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004743 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004744 ((!sess->fe->except_mask_to.s_addr ||
4745 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4746 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004747 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004748 (((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 +02004749 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004750 int len;
4751 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004752 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004753
4754 /* Note: we rely on the backend to get the header name to be used for
4755 * x-original-to, because the header is really meant for the backends.
4756 * However, if the backend did not specify any option, we have to rely
4757 * on the frontend's header name.
4758 */
4759 if (s->be->orgto_hdr_len) {
4760 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004761 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004762 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004763 len = sess->fe->orgto_hdr_len;
4764 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004765 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004766 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 +02004767
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004768 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004769 goto return_bad_req;
4770 }
4771 }
4772 }
4773
Willy Tarreau50fc7772012-11-11 22:19:57 +01004774 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4775 * If an "Upgrade" token is found, the header is left untouched in order not to have
4776 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4777 * "Upgrade" is present in the Connection header.
4778 */
4779 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4780 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004781 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004782 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004783 unsigned int want_flags = 0;
4784
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004785 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004786 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004787 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004788 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004789 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004790 want_flags |= TX_CON_CLO_SET;
4791 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004792 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004793 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004794 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004795 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004796 want_flags |= TX_CON_KAL_SET;
4797 }
4798
4799 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004800 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004801 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004802
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004803
Willy Tarreau522d6c02009-12-06 18:49:18 +01004804 /* If we have no server assigned yet and we're balancing on url_param
4805 * with a POST request, we may be interested in checking the body for
4806 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004807 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004808 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004809 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004810 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004811 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004812 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004813 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004814
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004815 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004816 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004817#ifdef TCP_QUICKACK
4818 /* We expect some data from the client. Unless we know for sure
4819 * we already have a full request, we have to re-enable quick-ack
4820 * in case we previously disabled it, otherwise we might cause
4821 * the client to delay further data.
4822 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004823 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004824 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004825 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004826 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004827 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004828#endif
4829 }
Willy Tarreau03945942009-12-22 16:50:27 +01004830
Willy Tarreau59234e92008-11-30 23:51:27 +01004831 /*************************************************************
4832 * OK, that's finished for the headers. We have done what we *
4833 * could. Let's switch to the DATA state. *
4834 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004835 req->analyse_exp = TICK_ETERNITY;
4836 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004837
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004838 /* if the server closes the connection, we want to immediately react
4839 * and close the socket to save packets and syscalls.
4840 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004841 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004842 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004843
Willy Tarreau59234e92008-11-30 23:51:27 +01004844 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004845 /* OK let's go on with the BODY now */
4846 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004847
Willy Tarreau59234e92008-11-30 23:51:27 +01004848 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004849 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004850 /* we detected a parsing error. We want to archive this request
4851 * in the dedicated proxy area for later troubleshooting.
4852 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004853 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004854 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004855
Willy Tarreau59234e92008-11-30 23:51:27 +01004856 txn->req.msg_state = HTTP_MSG_ERROR;
4857 txn->status = 400;
4858 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004859 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004860
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004861 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004862 if (sess->listener->counters)
4863 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004864
Willy Tarreaue7dff022015-04-03 01:14:29 +02004865 if (!(s->flags & SF_ERR_MASK))
4866 s->flags |= SF_ERR_PRXCOND;
4867 if (!(s->flags & SF_FINST_MASK))
4868 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004869 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004870}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004871
Willy Tarreau60b85b02008-11-30 23:28:40 +01004872/* This function is an analyser which processes the HTTP tarpit. It always
4873 * returns zero, at the beginning because it prevents any other processing
4874 * from occurring, and at the end because it terminates the request.
4875 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004876int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004877{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004878 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004879
4880 /* This connection is being tarpitted. The CLIENT side has
4881 * already set the connect expiration date to the right
4882 * timeout. We just have to check that the client is still
4883 * there and that the timeout has not expired.
4884 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004885 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004886 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004887 !tick_is_expired(req->analyse_exp, now_ms))
4888 return 0;
4889
4890 /* We will set the queue timer to the time spent, just for
4891 * logging purposes. We fake a 500 server error, so that the
4892 * attacker will not suspect his connection has been tarpitted.
4893 * It will not cause trouble to the logs because we can exclude
4894 * the tarpitted connections by filtering on the 'PT' status flags.
4895 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004896 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4897
4898 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004899 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004900 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004901
4902 req->analysers = 0;
4903 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004904
Willy Tarreaue7dff022015-04-03 01:14:29 +02004905 if (!(s->flags & SF_ERR_MASK))
4906 s->flags |= SF_ERR_PRXCOND;
4907 if (!(s->flags & SF_FINST_MASK))
4908 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004909 return 0;
4910}
4911
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004912/* This function is an analyser which waits for the HTTP request body. It waits
4913 * for either the buffer to be full, or the full advertised contents to have
4914 * reached the buffer. It must only be called after the standard HTTP request
4915 * processing has occurred, because it expects the request to be parsed and will
4916 * look for the Expect header. It may send a 100-Continue interim response. It
4917 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4918 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4919 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004920 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004921int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004922{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004923 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004924 struct http_txn *txn = s->txn;
4925 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004926
4927 /* We have to parse the HTTP request body to find any required data.
4928 * "balance url_param check_post" should have been the only way to get
4929 * into this. We were brought here after HTTP header analysis, so all
4930 * related structures are ready.
4931 */
4932
Willy Tarreau890988f2014-04-10 11:59:33 +02004933 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4934 /* This is the first call */
4935 if (msg->msg_state < HTTP_MSG_BODY)
4936 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004937
Willy Tarreau890988f2014-04-10 11:59:33 +02004938 if (msg->msg_state < HTTP_MSG_100_SENT) {
4939 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4940 * send an HTTP/1.1 100 Continue intermediate response.
4941 */
4942 if (msg->flags & HTTP_MSGF_VER_11) {
4943 struct hdr_ctx ctx;
4944 ctx.idx = 0;
4945 /* Expect is allowed in 1.1, look for it */
4946 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4947 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004948 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004949 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004950 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004951 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004952 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004953
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004954 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004955 * req->buf->p still points to the beginning of the message. We
4956 * must save the body in msg->next because it survives buffer
4957 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004958 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004959 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004960
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004961 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004962 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4963 else
4964 msg->msg_state = HTTP_MSG_DATA;
4965 }
4966
Willy Tarreau890988f2014-04-10 11:59:33 +02004967 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4968 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004969 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004970 goto missing_data;
4971
4972 /* OK we have everything we need now */
4973 goto http_end;
4974 }
4975
4976 /* OK here we're parsing a chunked-encoded message */
4977
Willy Tarreau522d6c02009-12-06 18:49:18 +01004978 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004979 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004980 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004981 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004982 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004983 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004984
Willy Tarreau115acb92009-12-26 13:56:06 +01004985 if (!ret)
4986 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004987 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004988 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004989 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004990 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004991 }
4992
Willy Tarreaud98cf932009-12-27 22:54:55 +01004993 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004994 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4995 * for at least a whole chunk or the whole content length bytes after
4996 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004997 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004998 if (msg->msg_state == HTTP_MSG_TRAILERS)
4999 goto http_end;
5000
Willy Tarreaue115b492015-05-01 23:05:14 +02005001 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01005002 goto http_end;
5003
5004 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02005005 /* we get here if we need to wait for more data. If the buffer is full,
5006 * we have the maximum we can expect.
5007 */
5008 if (buffer_full(req->buf, global.tune.maxrewrite))
5009 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01005010
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005011 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01005012 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01005013 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02005014
Willy Tarreaue7dff022015-04-03 01:14:29 +02005015 if (!(s->flags & SF_ERR_MASK))
5016 s->flags |= SF_ERR_CLITO;
5017 if (!(s->flags & SF_FINST_MASK))
5018 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005019 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005020 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005021
5022 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005023 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005024 /* Not enough data. We'll re-use the http-request
5025 * timeout here. Ideally, we should set the timeout
5026 * relative to the accept() date. We just set the
5027 * request timeout once at the beginning of the
5028 * request.
5029 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005030 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005031 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005032 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005033 return 0;
5034 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005035
5036 http_end:
5037 /* The situation will not evolve, so let's give up on the analysis. */
5038 s->logs.tv_request = now; /* update the request timer to reflect full request */
5039 req->analysers &= ~an_bit;
5040 req->analyse_exp = TICK_ETERNITY;
5041 return 1;
5042
5043 return_bad_req: /* let's centralize all bad requests */
5044 txn->req.msg_state = HTTP_MSG_ERROR;
5045 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005046 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005047
Willy Tarreaue7dff022015-04-03 01:14:29 +02005048 if (!(s->flags & SF_ERR_MASK))
5049 s->flags |= SF_ERR_PRXCOND;
5050 if (!(s->flags & SF_FINST_MASK))
5051 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005052
Willy Tarreau522d6c02009-12-06 18:49:18 +01005053 return_err_msg:
5054 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005055 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005056 if (sess->listener->counters)
5057 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005058 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005059}
5060
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005061/* send a server's name with an outgoing request over an established connection.
5062 * Note: this function is designed to be called once the request has been scheduled
5063 * for being forwarded. This is the reason why it rewinds the buffer before
5064 * proceeding.
5065 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005066int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005067
5068 struct hdr_ctx ctx;
5069
Mark Lamourinec2247f02012-01-04 13:02:01 -05005070 char *hdr_name = be->server_id_hdr_name;
5071 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005072 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005073 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005074 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005075
William Lallemandd9e90662012-01-30 17:27:17 +01005076 ctx.idx = 0;
5077
Willy Tarreau211cdec2014-04-17 20:18:08 +02005078 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005079 if (old_o) {
5080 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005081 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005082 txn->req.next += old_o;
5083 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005084 }
5085
Willy Tarreau9b28e032012-10-12 23:49:43 +02005086 old_i = chn->buf->i;
5087 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 -05005088 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005089 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005090 }
5091
5092 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005093 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005094 memcpy(hdr_val, hdr_name, hdr_name_len);
5095 hdr_val += hdr_name_len;
5096 *hdr_val++ = ':';
5097 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005098 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5099 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005100
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005101 if (old_o) {
5102 /* If this was a forwarded request, we must readjust the amount of
5103 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005104 * variations. Note that the current state is >= HTTP_MSG_BODY,
5105 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005106 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005107 old_o += chn->buf->i - old_i;
5108 b_adv(chn->buf, old_o);
5109 txn->req.next -= old_o;
5110 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005111 }
5112
Mark Lamourinec2247f02012-01-04 13:02:01 -05005113 return 0;
5114}
5115
Willy Tarreau610ecce2010-01-04 21:15:02 +01005116/* Terminate current transaction and prepare a new one. This is very tricky
5117 * right now but it works.
5118 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005119void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005120{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005121 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005122 struct proxy *fe = strm_fe(s);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005123 struct connection *srv_conn;
5124 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005125 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005126
Willy Tarreau610ecce2010-01-04 21:15:02 +01005127 /* FIXME: We need a more portable way of releasing a backend's and a
5128 * server's connections. We need a safer way to reinitialize buffer
5129 * flags. We also need a more accurate method for computing per-request
5130 * data.
5131 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005132 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005133
Willy Tarreau4213a112013-12-15 10:25:42 +01005134 /* unless we're doing keep-alive, we want to quickly close the connection
5135 * to the server.
5136 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005137 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005138 !si_conn_ready(&s->si[1])) {
5139 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5140 si_shutr(&s->si[1]);
5141 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005142 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005143
Willy Tarreaue7dff022015-04-03 01:14:29 +02005144 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005145 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005146 if (unlikely(s->srv_conn))
5147 sess_change_server(s, NULL);
5148 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005149
5150 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005151 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005152
Willy Tarreaueee5b512015-04-03 23:46:31 +02005153 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005154 int n;
5155
Willy Tarreaueee5b512015-04-03 23:46:31 +02005156 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005157 if (n < 1 || n > 5)
5158 n = 0;
5159
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005160 if (fe->mode == PR_MODE_HTTP) {
5161 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005162 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005163 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005164 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005165 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005166 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005167 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005168 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005169 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005170 s->be->be_counters.p.http.comp_rsp++;
5171 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005172 }
5173
5174 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005175 s->logs.bytes_in -= s->req.buf->i;
5176 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005177
5178 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005179 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005180 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005181 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005182 s->do_log(s);
5183 }
5184
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005185 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005186 stream_stop_content_counters(s);
5187 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005188
Willy Tarreau610ecce2010-01-04 21:15:02 +01005189 s->logs.accept_date = date; /* user-visible date for logging */
5190 s->logs.tv_accept = now; /* corrected date for internal use */
5191 tv_zero(&s->logs.tv_request);
5192 s->logs.t_queue = -1;
5193 s->logs.t_connect = -1;
5194 s->logs.t_data = -1;
5195 s->logs.t_close = 0;
5196 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5197 s->logs.srv_queue_size = 0; /* we will get this number soon */
5198
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005199 s->logs.bytes_in = s->req.total = s->req.buf->i;
5200 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005201
5202 if (s->pend_pos)
5203 pendconn_free(s->pend_pos);
5204
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005205 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005206 if (s->flags & SF_CURR_SESS) {
5207 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005208 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005209 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005210 if (may_dequeue_tasks(objt_server(s->target), s->be))
5211 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005212 }
5213
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005214 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005215
Willy Tarreau4213a112013-12-15 10:25:42 +01005216 /* only release our endpoint if we don't intend to reuse the
5217 * connection.
5218 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005219 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005220 !si_conn_ready(&s->si[1])) {
5221 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005222 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005223 }
5224
Willy Tarreau350f4872014-11-28 14:42:25 +01005225 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5226 s->si[1].err_type = SI_ET_NONE;
5227 s->si[1].conn_retries = 0; /* used for logging too */
5228 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005229 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 +01005230 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);
5231 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 +02005232 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5233 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5234 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005235
Willy Tarreaueee5b512015-04-03 23:46:31 +02005236 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005237 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005238 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005239
5240 if (prev_status == 401 || prev_status == 407) {
5241 /* In HTTP keep-alive mode, if we receive a 401, we still have
5242 * a chance of being able to send the visitor again to the same
5243 * server over the same connection. This is required by some
5244 * broken protocols such as NTLM, and anyway whenever there is
5245 * an opportunity for sending the challenge to the proper place,
5246 * it's better to do it (at least it helps with debugging).
5247 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005248 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005249 if (srv_conn)
5250 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005251 }
5252
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005253 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005254 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005255
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005256 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005257 s->req.flags |= CF_NEVER_WAIT;
5258 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005259 }
5260
Willy Tarreau610ecce2010-01-04 21:15:02 +01005261 /* if the request buffer is not empty, it means we're
5262 * about to process another request, so send pending
5263 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005264 * Just don't do this if the buffer is close to be full,
5265 * because the request will wait for it to flush a little
5266 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005267 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005268 if (s->req.buf->i) {
5269 if (s->res.buf->o &&
5270 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5271 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5272 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005273 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005274
Willy Tarreau714ea782015-11-25 20:11:11 +01005275 /* we're removing the analysers, we MUST re-enable events detection.
5276 * We don't enable close on the response channel since it's either
5277 * already closed, or in keep-alive with an idle connection handler.
5278 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005279 channel_auto_read(&s->req);
5280 channel_auto_close(&s->req);
5281 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005282
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005283 /* we're in keep-alive with an idle connection, monitor it if not already done */
5284 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005285 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005286 if (!srv)
5287 si_idle_conn(&s->si[1], NULL);
5288 else if ((srv_conn->flags & CO_FL_PRIVATE) ||
5289 ((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR))
5290 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005291 else if (prev_flags & TX_NOT_FIRST)
5292 /* note: we check the request, not the connection, but
5293 * this is valid for strategies SAFE and AGGR, and in
5294 * case of ALWS, we don't care anyway.
5295 */
5296 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005297 else
5298 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005299 }
Willy Tarreau27375622013-12-17 00:00:28 +01005300
Willy Tarreauc29d0cd2015-09-23 12:18:14 +02005301 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005302 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005303}
5304
5305
5306/* This function updates the request state machine according to the response
5307 * state machine and buffer flags. It returns 1 if it changes anything (flag
5308 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5309 * it is only used to find when a request/response couple is complete. Both
5310 * this function and its equivalent should loop until both return zero. It
5311 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5312 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005313int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005314{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005315 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005316 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005317 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005318 unsigned int old_state = txn->req.msg_state;
5319
Willy Tarreau610ecce2010-01-04 21:15:02 +01005320 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5321 return 0;
5322
5323 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005324 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005325 * We can shut the read side unless we want to abort_on_close,
5326 * or we have a POST request. The issue with POST requests is
5327 * that some browsers still send a CRLF after the request, and
5328 * this CRLF must be read so that it does not remain in the kernel
5329 * buffers, otherwise a close could cause an RST on some systems
5330 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005331 * Note that if we're using keep-alive on the client side, we'd
5332 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005333 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005334 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005335 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005336 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5337 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5338 !(s->be->options & PR_O_ABRT_CLOSE) &&
5339 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005340 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005341
Willy Tarreau40f151a2012-12-20 12:10:09 +01005342 /* if the server closes the connection, we want to immediately react
5343 * and close the socket to save packets and syscalls.
5344 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005345 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005346
Willy Tarreau7f876a12015-11-18 11:59:55 +01005347 /* In any case we've finished parsing the request so we must
5348 * disable Nagle when sending data because 1) we're not going
5349 * to shut this side, and 2) the server is waiting for us to
5350 * send pending data.
5351 */
5352 chn->flags |= CF_NEVER_WAIT;
5353
Willy Tarreau610ecce2010-01-04 21:15:02 +01005354 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5355 goto wait_other_side;
5356
5357 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5358 /* The server has not finished to respond, so we
5359 * don't want to move in order not to upset it.
5360 */
5361 goto wait_other_side;
5362 }
5363
5364 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5365 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005366 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005367 txn->req.msg_state = HTTP_MSG_TUNNEL;
5368 goto wait_other_side;
5369 }
5370
5371 /* When we get here, it means that both the request and the
5372 * response have finished receiving. Depending on the connection
5373 * mode, we'll have to wait for the last bytes to leave in either
5374 * direction, and sometimes for a close to be effective.
5375 */
5376
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005377 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5378 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005379 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5380 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005381 }
5382 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5383 /* Option forceclose is set, or either side wants to close,
5384 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005385 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005386 * once both states are CLOSED.
5387 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005388 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5389 channel_shutr_now(chn);
5390 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005391 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005392 }
5393 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005394 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5395 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005396 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005397 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5398 channel_auto_read(chn);
5399 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005400 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005401 }
5402
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005403 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005404 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005405 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005406
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005407 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005408 txn->req.msg_state = HTTP_MSG_CLOSING;
5409 goto http_msg_closing;
5410 }
5411 else {
5412 txn->req.msg_state = HTTP_MSG_CLOSED;
5413 goto http_msg_closed;
5414 }
5415 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005416 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005417 }
5418
5419 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5420 http_msg_closing:
5421 /* nothing else to forward, just waiting for the output buffer
5422 * to be empty and for the shutw_now to take effect.
5423 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005424 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005425 txn->req.msg_state = HTTP_MSG_CLOSED;
5426 goto http_msg_closed;
5427 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005428 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005429 txn->req.msg_state = HTTP_MSG_ERROR;
5430 goto wait_other_side;
5431 }
5432 }
5433
5434 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5435 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005436 /* see above in MSG_DONE why we only do this in these states */
5437 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5438 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5439 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005440 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005441 goto wait_other_side;
5442 }
5443
5444 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005445 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005446}
5447
5448
5449/* This function updates the response state machine according to the request
5450 * state machine and buffer flags. It returns 1 if it changes anything (flag
5451 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5452 * it is only used to find when a request/response couple is complete. Both
5453 * this function and its equivalent should loop until both return zero. It
5454 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5455 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005456int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005457{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005458 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005459 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005460 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005461 unsigned int old_state = txn->rsp.msg_state;
5462
Willy Tarreau610ecce2010-01-04 21:15:02 +01005463 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5464 return 0;
5465
5466 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5467 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005468 * still monitor the server connection for a possible close
5469 * while the request is being uploaded, so we don't disable
5470 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005471 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005472 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005473
5474 if (txn->req.msg_state == HTTP_MSG_ERROR)
5475 goto wait_other_side;
5476
5477 if (txn->req.msg_state < HTTP_MSG_DONE) {
5478 /* The client seems to still be sending data, probably
5479 * because we got an error response during an upload.
5480 * We have the choice of either breaking the connection
5481 * or letting it pass through. Let's do the later.
5482 */
5483 goto wait_other_side;
5484 }
5485
5486 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5487 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005488 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005489 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005490 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005491 goto wait_other_side;
5492 }
5493
5494 /* When we get here, it means that both the request and the
5495 * response have finished receiving. Depending on the connection
5496 * mode, we'll have to wait for the last bytes to leave in either
5497 * direction, and sometimes for a close to be effective.
5498 */
5499
5500 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5501 /* Server-close mode : shut read and wait for the request
5502 * side to close its output buffer. The caller will detect
5503 * when we're in DONE and the other is in CLOSED and will
5504 * catch that for the final cleanup.
5505 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005506 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5507 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005508 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005509 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5510 /* Option forceclose is set, or either side wants to close,
5511 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005512 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005513 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005514 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005515 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5516 channel_shutr_now(chn);
5517 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005518 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005519 }
5520 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005521 /* The last possible modes are keep-alive and tunnel. Tunnel will
5522 * need to forward remaining data. Keep-alive will need to monitor
5523 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005524 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005525 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005526 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005527 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5528 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005529 }
5530
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005531 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005532 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005533 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005534 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5535 goto http_msg_closing;
5536 }
5537 else {
5538 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5539 goto http_msg_closed;
5540 }
5541 }
5542 goto wait_other_side;
5543 }
5544
5545 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5546 http_msg_closing:
5547 /* nothing else to forward, just waiting for the output buffer
5548 * to be empty and for the shutw_now to take effect.
5549 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005550 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005551 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5552 goto http_msg_closed;
5553 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005554 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005555 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005556 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005557 if (objt_server(s->target))
5558 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005559 goto wait_other_side;
5560 }
5561 }
5562
5563 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5564 http_msg_closed:
5565 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005566 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005567 channel_auto_close(chn);
5568 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005569 goto wait_other_side;
5570 }
5571
5572 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005573 /* We force the response to leave immediately if we're waiting for the
5574 * other side, since there is no pending shutdown to push it out.
5575 */
5576 if (!channel_is_empty(chn))
5577 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005578 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005579}
5580
5581
5582/* Resync the request and response state machines. Return 1 if either state
5583 * changes.
5584 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005585int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005586{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005587 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005588 int old_req_state = txn->req.msg_state;
5589 int old_res_state = txn->rsp.msg_state;
5590
Willy Tarreau610ecce2010-01-04 21:15:02 +01005591 http_sync_req_state(s);
5592 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005593 if (!http_sync_res_state(s))
5594 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005595 if (!http_sync_req_state(s))
5596 break;
5597 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005598
Willy Tarreau610ecce2010-01-04 21:15:02 +01005599 /* OK, both state machines agree on a compatible state.
5600 * There are a few cases we're interested in :
5601 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5602 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5603 * directions, so let's simply disable both analysers.
5604 * - HTTP_MSG_CLOSED on the response only means we must abort the
5605 * request.
5606 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5607 * with server-close mode means we've completed one request and we
5608 * must re-initialize the server connection.
5609 */
5610
5611 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5612 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5613 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5614 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005615 s->req.analysers = 0;
5616 channel_auto_close(&s->req);
5617 channel_auto_read(&s->req);
5618 s->res.analysers = 0;
5619 channel_auto_close(&s->res);
5620 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005621 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005622 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005623 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005624 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005625 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005626 s->res.analysers = 0;
5627 channel_auto_close(&s->res);
5628 channel_auto_read(&s->res);
5629 s->req.analysers = 0;
5630 channel_abort(&s->req);
5631 channel_auto_close(&s->req);
5632 channel_auto_read(&s->req);
5633 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005634 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005635 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5636 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005637 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005638 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5639 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5640 /* server-close/keep-alive: terminate this transaction,
5641 * possibly killing the server connection and reinitialize
5642 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005643 */
5644 http_end_txn_clean_session(s);
5645 }
5646
Willy Tarreau610ecce2010-01-04 21:15:02 +01005647 return txn->req.msg_state != old_req_state ||
5648 txn->rsp.msg_state != old_res_state;
5649}
5650
Willy Tarreaud98cf932009-12-27 22:54:55 +01005651/* This function is an analyser which forwards request body (including chunk
5652 * sizes if any). It is called as soon as we must forward, even if we forward
5653 * zero byte. The only situation where it must not be called is when we're in
5654 * tunnel mode and we want to forward till the close. It's used both to forward
5655 * remaining data and to resync after end of body. It expects the msg_state to
5656 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005657 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005658 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005659 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005660 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005661int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005662{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005663 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005664 struct http_txn *txn = s->txn;
5665 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005666
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005667 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5668 return 0;
5669
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005670 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005671 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005672 /* Output closed while we were sending data. We must abort and
5673 * wake the other side up.
5674 */
5675 msg->msg_state = HTTP_MSG_ERROR;
5676 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005677 return 1;
5678 }
5679
Willy Tarreaud98cf932009-12-27 22:54:55 +01005680 /* Note that we don't have to send 100-continue back because we don't
5681 * need the data to complete our job, and it's up to the server to
5682 * decide whether to return 100, 417 or anything else in return of
5683 * an "Expect: 100-continue" header.
5684 */
5685
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005686 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005687 /* we have msg->sov which points to the first byte of message
5688 * body, and req->buf.p still points to the beginning of the
5689 * message. We forward the headers now, as we don't need them
5690 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005691 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005692 b_adv(req->buf, msg->sov);
5693 msg->next -= msg->sov;
5694 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005695
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005696 /* The previous analysers guarantee that the state is somewhere
5697 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5698 * msg->next are always correct.
5699 */
5700 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5701 if (msg->flags & HTTP_MSGF_TE_CHNK)
5702 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5703 else
5704 msg->msg_state = HTTP_MSG_DATA;
5705 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005706 }
5707
Willy Tarreau7ba23542014-04-17 21:50:00 +02005708 /* Some post-connect processing might want us to refrain from starting to
5709 * forward data. Currently, the only reason for this is "balance url_param"
5710 * whichs need to parse/process the request after we've enabled forwarding.
5711 */
5712 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005713 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005714 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005715 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005716 goto missing_data;
5717 }
5718 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5719 }
5720
Willy Tarreau80a92c02014-03-12 10:41:13 +01005721 /* in most states, we should abort in case of early close */
5722 channel_auto_close(req);
5723
Willy Tarreauefdf0942014-04-24 20:08:57 +02005724 if (req->to_forward) {
5725 /* We can't process the buffer's contents yet */
5726 req->flags |= CF_WAKE_WRITE;
5727 goto missing_data;
5728 }
5729
Willy Tarreaud98cf932009-12-27 22:54:55 +01005730 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005731 if (msg->msg_state == HTTP_MSG_DATA) {
5732 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005733 /* we may have some pending data starting at req->buf->p */
5734 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005735 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005736 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005737 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005738 msg->next += msg->chunk_len;
5739 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005740
5741 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005742 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005743 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005744 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005745 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005746 }
5747 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005748 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005749 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005750 * TRAILERS state.
5751 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005752 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005753
Willy Tarreau54d23df2012-10-25 19:04:45 +02005754 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005755 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005756 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005757 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005758 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005759 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005760 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005761 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005762 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005763 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005764 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005765 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005766 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005767
5768 if (ret == 0)
5769 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005770 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005771 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005772 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005773 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005774 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005775 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005776 /* we're in MSG_CHUNK_SIZE now */
5777 }
5778 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005779 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005780
5781 if (ret == 0)
5782 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005783 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005784 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005785 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005786 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005787 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005788 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005789 /* we're in HTTP_MSG_DONE now */
5790 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005791 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005792 int old_state = msg->msg_state;
5793
Willy Tarreau610ecce2010-01-04 21:15:02 +01005794 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005795
5796 /* we may have some pending data starting at req->buf->p
5797 * such as last chunk of data or trailers.
5798 */
5799 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005800 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005801 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005802 msg->next = 0;
5803
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005804 /* we don't want to forward closes on DONE except in
5805 * tunnel mode.
5806 */
5807 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005808 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005809 if (http_resync_states(s)) {
5810 /* some state changes occurred, maybe the analyser
5811 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005812 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005813 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005814 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005815 /* request errors are most likely due to
5816 * the server aborting the transfer.
5817 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005818 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005819 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005820 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005821 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005822 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005823 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005824 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005825 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005826
5827 /* If "option abortonclose" is set on the backend, we
5828 * want to monitor the client's connection and forward
5829 * any shutdown notification to the server, which will
5830 * decide whether to close or to go on processing the
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005831 * request. We only do that in tunnel mode, and not in
5832 * other modes since it can be abused to exhaust source
5833 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005834 */
5835 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005836 channel_auto_read(req);
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005837 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5838 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5839 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005840 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005841 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005842 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005843 /* POST requests may require to read extra CRLF
5844 * sent by broken browsers and which could cause
5845 * an RST to be sent upon close on some systems
5846 * (eg: Linux).
5847 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005848 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005849 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005850
Willy Tarreau610ecce2010-01-04 21:15:02 +01005851 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005852 }
5853 }
5854
Willy Tarreaud98cf932009-12-27 22:54:55 +01005855 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005856 /* we may have some pending data starting at req->buf->p */
5857 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005858 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005859 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5860
Willy Tarreaubed410e2014-04-22 08:19:34 +02005861 msg->next = 0;
5862 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5863
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005864 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005865 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005866 if (!(s->flags & SF_ERR_MASK))
5867 s->flags |= SF_ERR_CLICL;
5868 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005869 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005870 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005871 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005872 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005873 }
5874
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005875 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005876 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005877 if (objt_server(s->target))
5878 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005879
5880 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005881 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005882
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005883 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005884 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005885 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005886
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005887 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005888 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005889 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005890 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005891 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005892
Willy Tarreau5c620922011-05-11 19:56:11 +02005893 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005894 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005895 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005896 * modes are already handled by the stream sock layer. We must not do
5897 * this in content-length mode because it could present the MSG_MORE
5898 * flag with the last block of forwarded data, which would cause an
5899 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005900 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005901 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005902 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005903
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005904 return 0;
5905
Willy Tarreaud98cf932009-12-27 22:54:55 +01005906 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005907 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005908 if (sess->listener->counters)
5909 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005910
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005911 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005912 /* we may have some pending data starting at req->buf->p */
5913 b_adv(req->buf, msg->next);
5914 msg->next = 0;
5915
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005916 txn->req.msg_state = HTTP_MSG_ERROR;
5917 if (txn->status) {
5918 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005919 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005920 } else {
5921 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005922 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005923 }
5924 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005925 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005926
Willy Tarreaue7dff022015-04-03 01:14:29 +02005927 if (!(s->flags & SF_ERR_MASK))
5928 s->flags |= SF_ERR_PRXCOND;
5929 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005930 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005931 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005932 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005933 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005934 }
5935 return 0;
5936
5937 aborted_xfer:
5938 txn->req.msg_state = HTTP_MSG_ERROR;
5939 if (txn->status) {
5940 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005941 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005942 } else {
5943 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005944 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005945 }
5946 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005947 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005948
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005949 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005950 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005951 if (objt_server(s->target))
5952 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005953
Willy Tarreaue7dff022015-04-03 01:14:29 +02005954 if (!(s->flags & SF_ERR_MASK))
5955 s->flags |= SF_ERR_SRVCL;
5956 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005957 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005958 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005959 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005960 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005961 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005962 return 0;
5963}
5964
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005965/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5966 * processing can continue on next analysers, or zero if it either needs more
5967 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005968 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005969 * when it has nothing left to do, and may remove any analyser when it wants to
5970 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005971 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005972int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005973{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005974 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005975 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005976 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005977 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005978 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005979 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005980 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005981
Willy Tarreau87b09662015-04-03 00:22:06 +02005982 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 +02005983 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005984 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005985 rep,
5986 rep->rex, rep->wex,
5987 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005988 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005989 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005990
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005991 /*
5992 * Now parse the partial (or complete) lines.
5993 * We will check the response syntax, and also join multi-line
5994 * headers. An index of all the lines will be elaborated while
5995 * parsing.
5996 *
5997 * For the parsing, we use a 28 states FSM.
5998 *
5999 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02006000 * rep->buf->p = beginning of response
6001 * rep->buf->p + msg->eoh = end of processed headers / start of current one
6002 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02006003 * msg->eol = end of current header or line (LF or CRLF)
6004 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006005 */
6006
Willy Tarreau628c40c2014-04-24 19:11:26 +02006007 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01006008 /* There's a protected area at the end of the buffer for rewriting
6009 * purposes. We don't want to start to parse the request if the
6010 * protected area is affected, because we may have to move processed
6011 * data later, which is much more complicated.
6012 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006013 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01006014 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02006015 /* some data has still not left the buffer, wake us once that's done */
6016 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
6017 goto abort_response;
6018 channel_dont_close(rep);
6019 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01006020 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02006021 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01006022 }
6023
Willy Tarreau379357a2013-06-08 12:55:46 +02006024 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
6025 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
6026 buffer_slow_realign(rep->buf);
6027
Willy Tarreau9b28e032012-10-12 23:49:43 +02006028 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01006029 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01006030 }
6031
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006032 /* 1: we might have to print this header in debug mode */
6033 if (unlikely((global.mode & MODE_DEBUG) &&
6034 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02006035 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006036 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006037
Willy Tarreau9b28e032012-10-12 23:49:43 +02006038 sol = rep->buf->p;
6039 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006040 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006041
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006042 sol += hdr_idx_first_pos(&txn->hdr_idx);
6043 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006044
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006045 while (cur_idx) {
6046 eol = sol + txn->hdr_idx.v[cur_idx].len;
6047 debug_hdr("srvhdr", s, sol, eol);
6048 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
6049 cur_idx = txn->hdr_idx.v[cur_idx].next;
6050 }
6051 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006052
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006053 /*
6054 * Now we quickly check if we have found a full valid response.
6055 * If not so, we check the FD and buffer states before leaving.
6056 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01006057 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006058 * responses are checked first.
6059 *
6060 * Depending on whether the client is still there or not, we
6061 * may send an error response back or not. Note that normally
6062 * we should only check for HTTP status there, and check I/O
6063 * errors somewhere else.
6064 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006065
Willy Tarreau655dce92009-11-08 13:10:58 +01006066 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006067 /* Invalid response */
6068 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6069 /* we detected a parsing error. We want to archive this response
6070 * in the dedicated proxy area for later troubleshooting.
6071 */
6072 hdr_response_bad:
6073 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006074 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006075
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006076 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006077 if (objt_server(s->target)) {
6078 objt_server(s->target)->counters.failed_resp++;
6079 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006080 }
Willy Tarreau64648412010-03-05 10:41:54 +01006081 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006082 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006083 rep->analysers = 0;
6084 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006085 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006086 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006087 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006088
Willy Tarreaue7dff022015-04-03 01:14:29 +02006089 if (!(s->flags & SF_ERR_MASK))
6090 s->flags |= SF_ERR_PRXCOND;
6091 if (!(s->flags & SF_FINST_MASK))
6092 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006093
6094 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006095 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006096
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006097 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006098 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006099 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006100 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006101 goto hdr_response_bad;
6102 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006103
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006104 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006105 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006106 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006107 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006108 else if (txn->flags & TX_NOT_FIRST)
6109 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006110
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006111 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006112 if (objt_server(s->target)) {
6113 objt_server(s->target)->counters.failed_resp++;
6114 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006115 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006116
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006117 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006118 rep->analysers = 0;
6119 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006120 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006121 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006122 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006123
Willy Tarreaue7dff022015-04-03 01:14:29 +02006124 if (!(s->flags & SF_ERR_MASK))
6125 s->flags |= SF_ERR_SRVCL;
6126 if (!(s->flags & SF_FINST_MASK))
6127 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006128 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006129 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006130
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006131 /* read timeout : return a 504 to the client. */
6132 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006133 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006134 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006135
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006136 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006137 if (objt_server(s->target)) {
6138 objt_server(s->target)->counters.failed_resp++;
6139 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006140 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006141
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006142 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006143 rep->analysers = 0;
6144 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006145 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006146 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006147 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006148
Willy Tarreaue7dff022015-04-03 01:14:29 +02006149 if (!(s->flags & SF_ERR_MASK))
6150 s->flags |= SF_ERR_SRVTO;
6151 if (!(s->flags & SF_FINST_MASK))
6152 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006153 return 0;
6154 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006155
Willy Tarreauf003d372012-11-26 13:35:37 +01006156 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006157 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006158 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006159 s->be->be_counters.cli_aborts++;
6160 if (objt_server(s->target))
6161 objt_server(s->target)->counters.cli_aborts++;
6162
6163 rep->analysers = 0;
6164 channel_auto_close(rep);
6165
6166 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006167 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006168 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006169
Willy Tarreaue7dff022015-04-03 01:14:29 +02006170 if (!(s->flags & SF_ERR_MASK))
6171 s->flags |= SF_ERR_CLICL;
6172 if (!(s->flags & SF_FINST_MASK))
6173 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006174
Willy Tarreau87b09662015-04-03 00:22:06 +02006175 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006176 return 0;
6177 }
6178
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006179 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006180 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006181 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006182 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006183 else if (txn->flags & TX_NOT_FIRST)
6184 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006185
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006186 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006187 if (objt_server(s->target)) {
6188 objt_server(s->target)->counters.failed_resp++;
6189 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006190 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006191
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006192 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006193 rep->analysers = 0;
6194 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006195 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006196 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006197 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006198
Willy Tarreaue7dff022015-04-03 01:14:29 +02006199 if (!(s->flags & SF_ERR_MASK))
6200 s->flags |= SF_ERR_SRVCL;
6201 if (!(s->flags & SF_FINST_MASK))
6202 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006203 return 0;
6204 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006205
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006206 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006207 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006208 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006209 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006210 else if (txn->flags & TX_NOT_FIRST)
6211 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006212
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006213 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006214 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006215 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006216
Willy Tarreaue7dff022015-04-03 01:14:29 +02006217 if (!(s->flags & SF_ERR_MASK))
6218 s->flags |= SF_ERR_CLICL;
6219 if (!(s->flags & SF_FINST_MASK))
6220 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006221
Willy Tarreau87b09662015-04-03 00:22:06 +02006222 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006223 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006224 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006225
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006226 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006227 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006228 return 0;
6229 }
6230
6231 /* More interesting part now : we know that we have a complete
6232 * response which at least looks like HTTP. We have an indicator
6233 * of each header's length, so we can parse them quickly.
6234 */
6235
6236 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006237 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006238
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006239 /*
6240 * 1: get the status code
6241 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006242 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006243 if (n < 1 || n > 5)
6244 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006245 /* when the client triggers a 4xx from the server, it's most often due
6246 * to a missing object or permission. These events should be tracked
6247 * because if they happen often, it may indicate a brute force or a
6248 * vulnerability scan.
6249 */
6250 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006251 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006252
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006253 if (objt_server(s->target))
6254 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006255
Willy Tarreau91852eb2015-05-01 13:26:00 +02006256 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6257 * exactly one digit "." one digit. This check may be disabled using
6258 * option accept-invalid-http-response.
6259 */
6260 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6261 if (msg->sl.st.v_l != 8) {
6262 msg->err_pos = 0;
6263 goto hdr_response_bad;
6264 }
6265
6266 if (rep->buf->p[4] != '/' ||
6267 !isdigit((unsigned char)rep->buf->p[5]) ||
6268 rep->buf->p[6] != '.' ||
6269 !isdigit((unsigned char)rep->buf->p[7])) {
6270 msg->err_pos = 4;
6271 goto hdr_response_bad;
6272 }
6273 }
6274
Willy Tarreau5b154472009-12-21 20:11:07 +01006275 /* check if the response is HTTP/1.1 or above */
6276 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006277 ((rep->buf->p[5] > '1') ||
6278 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006279 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006280
6281 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006282 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 +01006283
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006284 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006285 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006286
Willy Tarreau9b28e032012-10-12 23:49:43 +02006287 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006288
Willy Tarreau39650402010-03-15 19:44:39 +01006289 /* Adjust server's health based on status code. Note: status codes 501
6290 * and 505 are triggered on demand by client request, so we must not
6291 * count them as server failures.
6292 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006293 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006294 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006295 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006296 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006297 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006298 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006299
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006300 /*
6301 * 2: check for cacheability.
6302 */
6303
6304 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006305 case 100:
6306 /*
6307 * We may be facing a 100-continue response, in which case this
6308 * is not the right response, and we're waiting for the next one.
6309 * Let's allow this response to go to the client and wait for the
6310 * next one.
6311 */
6312 hdr_idx_init(&txn->hdr_idx);
6313 msg->next -= channel_forward(rep, msg->next);
6314 msg->msg_state = HTTP_MSG_RPBEFORE;
6315 txn->status = 0;
6316 s->logs.t_data = -1; /* was not a response yet */
6317 goto next_one;
6318
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006319 case 200:
6320 case 203:
6321 case 206:
6322 case 300:
6323 case 301:
6324 case 410:
6325 /* RFC2616 @13.4:
6326 * "A response received with a status code of
6327 * 200, 203, 206, 300, 301 or 410 MAY be stored
6328 * by a cache (...) unless a cache-control
6329 * directive prohibits caching."
6330 *
6331 * RFC2616 @9.5: POST method :
6332 * "Responses to this method are not cacheable,
6333 * unless the response includes appropriate
6334 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006335 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006336 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006337 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006338 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6339 break;
6340 default:
6341 break;
6342 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006343
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006344 /*
6345 * 3: we may need to capture headers
6346 */
6347 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006348 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006349 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006350 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006351
Willy Tarreau557f1992015-05-01 10:05:17 +02006352 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6353 * by RFC7230#3.3.3 :
6354 *
6355 * The length of a message body is determined by one of the following
6356 * (in order of precedence):
6357 *
6358 * 1. Any response to a HEAD request and any response with a 1xx
6359 * (Informational), 204 (No Content), or 304 (Not Modified) status
6360 * code is always terminated by the first empty line after the
6361 * header fields, regardless of the header fields present in the
6362 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006363 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006364 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6365 * the connection will become a tunnel immediately after the empty
6366 * line that concludes the header fields. A client MUST ignore any
6367 * Content-Length or Transfer-Encoding header fields received in
6368 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006369 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006370 * 3. If a Transfer-Encoding header field is present and the chunked
6371 * transfer coding (Section 4.1) is the final encoding, the message
6372 * body length is determined by reading and decoding the chunked
6373 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006374 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006375 * If a Transfer-Encoding header field is present in a response and
6376 * the chunked transfer coding is not the final encoding, the
6377 * message body length is determined by reading the connection until
6378 * it is closed by the server. If a Transfer-Encoding header field
6379 * is present in a request and the chunked transfer coding is not
6380 * the final encoding, the message body length cannot be determined
6381 * reliably; the server MUST respond with the 400 (Bad Request)
6382 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006383 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006384 * If a message is received with both a Transfer-Encoding and a
6385 * Content-Length header field, the Transfer-Encoding overrides the
6386 * Content-Length. Such a message might indicate an attempt to
6387 * perform request smuggling (Section 9.5) or response splitting
6388 * (Section 9.4) and ought to be handled as an error. A sender MUST
6389 * remove the received Content-Length field prior to forwarding such
6390 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006391 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006392 * 4. If a message is received without Transfer-Encoding and with
6393 * either multiple Content-Length header fields having differing
6394 * field-values or a single Content-Length header field having an
6395 * invalid value, then the message framing is invalid and the
6396 * recipient MUST treat it as an unrecoverable error. If this is a
6397 * request message, the server MUST respond with a 400 (Bad Request)
6398 * status code and then close the connection. If this is a response
6399 * message received by a proxy, the proxy MUST close the connection
6400 * to the server, discard the received response, and send a 502 (Bad
6401 * Gateway) response to the client. If this is a response message
6402 * received by a user agent, the user agent MUST close the
6403 * connection to the server and discard the received response.
6404 *
6405 * 5. If a valid Content-Length header field is present without
6406 * Transfer-Encoding, its decimal value defines the expected message
6407 * body length in octets. If the sender closes the connection or
6408 * the recipient times out before the indicated number of octets are
6409 * received, the recipient MUST consider the message to be
6410 * incomplete and close the connection.
6411 *
6412 * 6. If this is a request message and none of the above are true, then
6413 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006414 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006415 * 7. Otherwise, this is a response message without a declared message
6416 * body length, so the message body length is determined by the
6417 * number of octets received prior to the server closing the
6418 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006419 */
6420
6421 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006422 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006423 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006424 * FIXME: should we parse anyway and return an error on chunked encoding ?
6425 */
6426 if (txn->meth == HTTP_METH_HEAD ||
6427 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006428 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006429 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006430 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006431 goto skip_content_length;
6432 }
6433
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006434 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006435 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006436 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006437 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006438 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6439 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006440 /* bad transfer-encoding (chunked followed by something else) */
6441 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006442 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006443 break;
6444 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006445 }
6446
Willy Tarreau1c913912015-04-30 10:57:51 +02006447 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006448 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006449 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006450 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6451 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6452 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006453 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006454 signed long long cl;
6455
Willy Tarreauad14f752011-09-02 20:33:27 +02006456 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006457 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006458 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006459 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006460
Willy Tarreauad14f752011-09-02 20:33:27 +02006461 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006462 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006463 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006464 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006465
Willy Tarreauad14f752011-09-02 20:33:27 +02006466 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006467 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006468 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006469 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006470
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006471 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006472 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006473 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006474 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006475
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006476 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006477 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006478 }
6479
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006480 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006481 select_compression_response_header(s, rep->buf);
6482
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006483skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006484 /* Now we have to check if we need to modify the Connection header.
6485 * This is more difficult on the response than it is on the request,
6486 * because we can have two different HTTP versions and we don't know
6487 * how the client will interprete a response. For instance, let's say
6488 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6489 * HTTP/1.1 response without any header. Maybe it will bound itself to
6490 * HTTP/1.0 because it only knows about it, and will consider the lack
6491 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6492 * the lack of header as a keep-alive. Thus we will use two flags
6493 * indicating how a request MAY be understood by the client. In case
6494 * of multiple possibilities, we'll fix the header to be explicit. If
6495 * ambiguous cases such as both close and keepalive are seen, then we
6496 * will fall back to explicit close. Note that we won't take risks with
6497 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006498 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006499 */
6500
Willy Tarreaudc008c52010-02-01 16:20:08 +01006501 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6502 txn->status == 101)) {
6503 /* Either we've established an explicit tunnel, or we're
6504 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006505 * to understand the next protocols. We have to switch to tunnel
6506 * mode, so that we transfer the request and responses then let
6507 * this protocol pass unmodified. When we later implement specific
6508 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006509 * header which contains information about that protocol for
6510 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006511 */
6512 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6513 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006514 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6515 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006516 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006517 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006518 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006519
Willy Tarreau70dffda2014-01-30 03:07:23 +01006520 /* this situation happens when combining pretend-keepalive with httpclose. */
6521 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006522 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006523 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006524 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6525
Willy Tarreau60466522010-01-18 19:08:45 +01006526 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006527 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006528 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6529 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006530
Willy Tarreau60466522010-01-18 19:08:45 +01006531 /* now adjust header transformations depending on current state */
6532 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6533 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6534 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006535 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006536 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006537 }
Willy Tarreau60466522010-01-18 19:08:45 +01006538 else { /* SCL / KAL */
6539 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006540 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006541 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006542 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006543
Willy Tarreau60466522010-01-18 19:08:45 +01006544 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006545 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006546
Willy Tarreau60466522010-01-18 19:08:45 +01006547 /* Some keep-alive responses are converted to Server-close if
6548 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006549 */
Willy Tarreau60466522010-01-18 19:08:45 +01006550 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6551 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006552 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006553 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006554 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006555 }
6556
Willy Tarreau7959a552013-09-23 16:44:27 +02006557 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006558 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006559
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006560 /* end of job, return OK */
6561 rep->analysers &= ~an_bit;
6562 rep->analyse_exp = TICK_ETERNITY;
6563 channel_auto_close(rep);
6564 return 1;
6565
6566 abort_keep_alive:
6567 /* A keep-alive request to the server failed on a network error.
6568 * The client is required to retry. We need to close without returning
6569 * any other information so that the client retries.
6570 */
6571 txn->status = 0;
6572 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006573 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006574 channel_auto_close(rep);
6575 s->logs.logwait = 0;
6576 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006577 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006578 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006579 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006580 return 0;
6581}
6582
6583/* This function performs all the processing enabled for the current response.
6584 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006585 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006586 * other functions. It works like process_request (see indications above).
6587 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006588int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006589{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006590 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006591 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006592 struct http_msg *msg = &txn->rsp;
6593 struct proxy *cur_proxy;
6594 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006595 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006596
Willy Tarreau87b09662015-04-03 00:22:06 +02006597 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 +02006598 now_ms, __FUNCTION__,
6599 s,
6600 rep,
6601 rep->rex, rep->wex,
6602 rep->flags,
6603 rep->buf->i,
6604 rep->analysers);
6605
6606 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6607 return 0;
6608
Willy Tarreau70730dd2014-04-24 18:06:27 +02006609 /* The stats applet needs to adjust the Connection header but we don't
6610 * apply any filter there.
6611 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006612 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6613 rep->analysers &= ~an_bit;
6614 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006615 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006616 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006617
Willy Tarreau58975672014-04-24 21:13:57 +02006618 /*
6619 * We will have to evaluate the filters.
6620 * As opposed to version 1.2, now they will be evaluated in the
6621 * filters order and not in the header order. This means that
6622 * each filter has to be validated among all headers.
6623 *
6624 * Filters are tried with ->be first, then with ->fe if it is
6625 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006626 *
6627 * Maybe we are in resume condiion. In this case I choose the
6628 * "struct proxy" which contains the rule list matching the resume
6629 * pointer. If none of theses "struct proxy" match, I initialise
6630 * the process with the first one.
6631 *
6632 * In fact, I check only correspondance betwwen the current list
6633 * pointer and the ->fe rule list. If it doesn't match, I initialize
6634 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006635 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006636 if (s->current_rule_list == &sess->fe->http_res_rules)
6637 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006638 else
6639 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006640 while (1) {
6641 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006642
Willy Tarreau58975672014-04-24 21:13:57 +02006643 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006644 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006645 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006646
Willy Tarreau51d861a2015-05-22 17:30:48 +02006647 if (ret == HTTP_RULE_RES_BADREQ)
6648 goto return_srv_prx_502;
6649
6650 if (ret == HTTP_RULE_RES_DONE) {
6651 rep->analysers &= ~an_bit;
6652 rep->analyse_exp = TICK_ETERNITY;
6653 return 1;
6654 }
6655 }
6656
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006657 /* we need to be called again. */
6658 if (ret == HTTP_RULE_RES_YIELD) {
6659 channel_dont_close(rep);
6660 return 0;
6661 }
6662
Willy Tarreau58975672014-04-24 21:13:57 +02006663 /* try headers filters */
6664 if (rule_set->rsp_exp != NULL) {
6665 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6666 return_bad_resp:
6667 if (objt_server(s->target)) {
6668 objt_server(s->target)->counters.failed_resp++;
6669 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006670 }
Willy Tarreau58975672014-04-24 21:13:57 +02006671 s->be->be_counters.failed_resp++;
6672 return_srv_prx_502:
6673 rep->analysers = 0;
6674 txn->status = 502;
6675 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006676 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006677 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006678 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006679 if (!(s->flags & SF_ERR_MASK))
6680 s->flags |= SF_ERR_PRXCOND;
6681 if (!(s->flags & SF_FINST_MASK))
6682 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006683 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006684 }
Willy Tarreau58975672014-04-24 21:13:57 +02006685 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006686
Willy Tarreau58975672014-04-24 21:13:57 +02006687 /* has the response been denied ? */
6688 if (txn->flags & TX_SVDENY) {
6689 if (objt_server(s->target))
6690 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006691
Willy Tarreau58975672014-04-24 21:13:57 +02006692 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006693 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006694 if (sess->listener->counters)
6695 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006696
Willy Tarreau58975672014-04-24 21:13:57 +02006697 goto return_srv_prx_502;
6698 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006699
Willy Tarreau58975672014-04-24 21:13:57 +02006700 /* add response headers from the rule sets in the same order */
6701 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006702 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006703 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006704 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006705 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006706 ret = acl_pass(ret);
6707 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6708 ret = !ret;
6709 if (!ret)
6710 continue;
6711 }
6712 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6713 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006714 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006715
Willy Tarreau58975672014-04-24 21:13:57 +02006716 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006717 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006718 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006719 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006720 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006721
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006722 /* After this point, this anayzer can't return yield, so we can
6723 * remove the bit corresponding to this analyzer from the list.
6724 *
6725 * Note that the intermediate returns and goto found previously
6726 * reset the analyzers.
6727 */
6728 rep->analysers &= ~an_bit;
6729 rep->analyse_exp = TICK_ETERNITY;
6730
Willy Tarreau58975672014-04-24 21:13:57 +02006731 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006732 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006733 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006734
Willy Tarreau58975672014-04-24 21:13:57 +02006735 /*
6736 * Now check for a server cookie.
6737 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006738 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006739 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006740
Willy Tarreau58975672014-04-24 21:13:57 +02006741 /*
6742 * Check for cache-control or pragma headers if required.
6743 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006744 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 +02006745 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006746
Willy Tarreau58975672014-04-24 21:13:57 +02006747 /*
6748 * Add server cookie in the response if needed
6749 */
6750 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6751 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006752 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006753 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6754 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6755 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6756 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6757 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006758 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006759 /* the server is known, it's not the one the client requested, or the
6760 * cookie's last seen date needs to be refreshed. We have to
6761 * insert a set-cookie here, except if we want to insert only on POST
6762 * requests and this one isn't. Note that servers which don't have cookies
6763 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006764 */
Willy Tarreau58975672014-04-24 21:13:57 +02006765 if (!objt_server(s->target)->cookie) {
6766 chunk_printf(&trash,
6767 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6768 s->be->cookie_name);
6769 }
6770 else {
6771 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006772
Willy Tarreau58975672014-04-24 21:13:57 +02006773 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6774 /* emit last_date, which is mandatory */
6775 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6776 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6777 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006778
Willy Tarreau58975672014-04-24 21:13:57 +02006779 if (s->be->cookie_maxlife) {
6780 /* emit first_date, which is either the original one or
6781 * the current date.
6782 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006783 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006784 s30tob64(txn->cookie_first_date ?
6785 txn->cookie_first_date >> 2 :
6786 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006787 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006788 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006789 }
Willy Tarreau58975672014-04-24 21:13:57 +02006790 chunk_appendf(&trash, "; path=/");
6791 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006792
Willy Tarreau58975672014-04-24 21:13:57 +02006793 if (s->be->cookie_domain)
6794 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006795
Willy Tarreau58975672014-04-24 21:13:57 +02006796 if (s->be->ck_opts & PR_CK_HTTPONLY)
6797 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006798
Willy Tarreau58975672014-04-24 21:13:57 +02006799 if (s->be->ck_opts & PR_CK_SECURE)
6800 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006801
Willy Tarreau58975672014-04-24 21:13:57 +02006802 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6803 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006804
Willy Tarreau58975672014-04-24 21:13:57 +02006805 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006806 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006807 /* the server did not change, only the date was updated */
6808 txn->flags |= TX_SCK_UPDATED;
6809 else
6810 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006811
Willy Tarreau58975672014-04-24 21:13:57 +02006812 /* Here, we will tell an eventual cache on the client side that we don't
6813 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6814 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6815 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006816 */
Willy Tarreau58975672014-04-24 21:13:57 +02006817 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006818
Willy Tarreau58975672014-04-24 21:13:57 +02006819 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006820
Willy Tarreau58975672014-04-24 21:13:57 +02006821 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6822 "Cache-control: private", 22) < 0))
6823 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006824 }
Willy Tarreau58975672014-04-24 21:13:57 +02006825 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006826
Willy Tarreau58975672014-04-24 21:13:57 +02006827 /*
6828 * Check if result will be cacheable with a cookie.
6829 * We'll block the response if security checks have caught
6830 * nasty things such as a cacheable cookie.
6831 */
6832 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6833 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6834 (s->be->options & PR_O_CHK_CACHE)) {
6835 /* we're in presence of a cacheable response containing
6836 * a set-cookie header. We'll block it as requested by
6837 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006838 */
Willy Tarreau58975672014-04-24 21:13:57 +02006839 if (objt_server(s->target))
6840 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006841
Willy Tarreau58975672014-04-24 21:13:57 +02006842 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006843 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006844 if (sess->listener->counters)
6845 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006846
Willy Tarreau58975672014-04-24 21:13:57 +02006847 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6848 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6849 send_log(s->be, LOG_ALERT,
6850 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6851 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6852 goto return_srv_prx_502;
6853 }
Willy Tarreau03945942009-12-22 16:50:27 +01006854
Willy Tarreau70730dd2014-04-24 18:06:27 +02006855 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006856 /*
6857 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6858 * If an "Upgrade" token is found, the header is left untouched in order
6859 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006860 * if anything but "Upgrade" is present in the Connection header. We don't
6861 * want to touch any 101 response either since it's switching to another
6862 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006863 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006864 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006865 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006866 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006867 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6868 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006869
Willy Tarreau58975672014-04-24 21:13:57 +02006870 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6871 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6872 /* we want a keep-alive response here. Keep-alive header
6873 * required if either side is not 1.1.
6874 */
6875 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6876 want_flags |= TX_CON_KAL_SET;
6877 }
6878 else {
6879 /* we want a close response here. Close header required if
6880 * the server is 1.1, regardless of the client.
6881 */
6882 if (msg->flags & HTTP_MSGF_VER_11)
6883 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006884 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006885
Willy Tarreau58975672014-04-24 21:13:57 +02006886 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6887 http_change_connection_header(txn, msg, want_flags);
6888 }
6889
6890 skip_header_mangling:
6891 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6892 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6893 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006894
Willy Tarreau58975672014-04-24 21:13:57 +02006895 /* if the user wants to log as soon as possible, without counting
6896 * bytes from the server, then this is the right moment. We have
6897 * to temporarily assign bytes_out to log what we currently have.
6898 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006899 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006900 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6901 s->logs.bytes_out = txn->rsp.eoh;
6902 s->do_log(s);
6903 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006904 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006905 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006906}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006907
Willy Tarreaud98cf932009-12-27 22:54:55 +01006908/* This function is an analyser which forwards response body (including chunk
6909 * sizes if any). It is called as soon as we must forward, even if we forward
6910 * zero byte. The only situation where it must not be called is when we're in
6911 * tunnel mode and we want to forward till the close. It's used both to forward
6912 * remaining data and to resync after end of body. It expects the msg_state to
6913 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006914 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006915 *
6916 * It is capable of compressing response data both in content-length mode and
6917 * in chunked mode. The state machines follows different flows depending on
6918 * whether content-length and chunked modes are used, since there are no
6919 * trailers in content-length :
6920 *
6921 * chk-mode cl-mode
6922 * ,----- BODY -----.
6923 * / \
6924 * V size > 0 V chk-mode
6925 * .--> SIZE -------------> DATA -------------> CRLF
6926 * | | size == 0 | last byte |
6927 * | v final crlf v inspected |
6928 * | TRAILERS -----------> DONE |
6929 * | |
6930 * `----------------------------------------------'
6931 *
6932 * Compression only happens in the DATA state, and must be flushed in final
6933 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6934 * is performed at once on final states for all bytes parsed, or when leaving
6935 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006936 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006937int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006938{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006939 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006940 struct http_txn *txn = s->txn;
6941 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006942 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006943 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006944 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006945
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006946 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6947 return 0;
6948
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006949 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006950 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006951 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006952 /* Output closed while we were sending data. We must abort and
6953 * wake the other side up.
6954 */
6955 msg->msg_state = HTTP_MSG_ERROR;
6956 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006957 return 1;
6958 }
6959
Willy Tarreau4fe41902010-06-07 22:27:41 +02006960 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006961 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006962
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006963 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006964 /* we have msg->sov which points to the first byte of message
6965 * body, and res->buf.p still points to the beginning of the
6966 * message. We forward the headers now, as we don't need them
6967 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006968 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006969 b_adv(res->buf, msg->sov);
6970 msg->next -= msg->sov;
6971 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006972
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006973 /* The previous analysers guarantee that the state is somewhere
6974 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6975 * msg->next are always correct.
6976 */
6977 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6978 if (msg->flags & HTTP_MSGF_TE_CHNK)
6979 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6980 else
6981 msg->msg_state = HTTP_MSG_DATA;
6982 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006983 }
6984
Willy Tarreauefdf0942014-04-24 20:08:57 +02006985 if (res->to_forward) {
6986 /* We can't process the buffer's contents yet */
6987 res->flags |= CF_WAKE_WRITE;
6988 goto missing_data;
6989 }
6990
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006991 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6992 /* We need a compression buffer in the DATA state to put the
6993 * output of compressed data, and in CRLF state to let the
6994 * TRAILERS state finish the job of removing the trailing CRLF.
6995 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006996 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006997 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006998 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006999 goto aborted_xfer; /* no memory */
7000 }
7001
7002 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01007003 if (ret < 0) {
7004 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02007005 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01007006 }
William Lallemand82fe75c2012-10-23 10:25:10 +02007007 compressing = 1;
7008 }
7009
Willy Tarreaud98cf932009-12-27 22:54:55 +01007010 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007011 switch (msg->msg_state - HTTP_MSG_DATA) {
7012 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02007013 /* we may have some pending data starting at res->buf->p */
7014 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02007015 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01007016 if (ret < 0)
7017 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02007018
Willy Tarreaud5a67832014-04-21 10:54:27 +02007019 if (msg->chunk_len) {
7020 /* input empty or output full */
7021 if (res->buf->i > msg->next)
7022 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02007023 goto missing_data;
7024 }
William Lallemandbf3ae612012-11-19 12:35:37 +01007025 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007026 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02007027 if (msg->chunk_len > res->buf->i - msg->next) {
7028 /* output full */
7029 res->flags |= CF_WAKE_WRITE;
7030 goto missing_data;
7031 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007032 msg->next += msg->chunk_len;
7033 msg->chunk_len = 0;
7034 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01007035
7036 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01007037 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02007038 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01007039 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01007040 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007041 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01007042 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007043 /* fall through for HTTP_MSG_CHUNK_CRLF */
7044
7045 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
7046 /* we want the CRLF after the data */
7047
7048 ret = http_skip_chunk_crlf(msg);
7049 if (ret == 0)
7050 goto missing_data;
7051 else if (ret < 0) {
7052 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007053 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007054 goto return_bad_res;
7055 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007056 /* we're in MSG_CHUNK_SIZE now, fall through */
7057
7058 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01007059 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02007060 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01007061 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01007062 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007063
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007064 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02007065 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01007066 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_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007070 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007071 }
Willy Tarreau0161d622013-04-02 01:26:55 +02007072 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007073 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01007074
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007075 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
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 }
7081
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007082 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007083 if (ret == 0)
7084 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007085 else if (ret < 0) {
7086 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007087 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007088 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007089 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02007090 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007091
7092 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01007093 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007094 if (unlikely(compressing)) {
7095 /* we need to flush output contents before syncing FSMs */
7096 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7097 compressing = 0;
7098 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007099
Willy Tarreauc623c172014-04-18 09:53:50 +02007100 /* we may have some pending data starting at res->buf->p
7101 * such as a last chunk of data or trailers.
7102 */
7103 b_adv(res->buf, msg->next);
7104 msg->next = 0;
7105
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007106 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02007107 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007108 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7109 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007110 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02007111
Willy Tarreau610ecce2010-01-04 21:15:02 +01007112 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01007113 /* some state changes occurred, maybe the analyser
7114 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01007115 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007116 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007117 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007118 /* response errors are most likely due to
7119 * the client aborting the transfer.
7120 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007121 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007122 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007123 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007124 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01007125 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007126 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007127 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01007128 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007129 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007130 }
7131 }
7132
Willy Tarreaud98cf932009-12-27 22:54:55 +01007133 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02007134 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007135 if (unlikely(compressing)) {
7136 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02007137 compressing = 0;
7138 }
Willy Tarreauf003d372012-11-26 13:35:37 +01007139
Willy Tarreauc623c172014-04-18 09:53:50 +02007140 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
7141 b_adv(res->buf, msg->next);
7142 msg->next = 0;
7143 msg->chunk_len -= channel_forward(res, msg->chunk_len);
7144 }
7145
Willy Tarreauf003d372012-11-26 13:35:37 +01007146 if (res->flags & CF_SHUTW)
7147 goto aborted_xfer;
7148
7149 /* stop waiting for data if the input is closed before the end. If the
7150 * client side was already closed, it means that the client has aborted,
7151 * so we don't want to count this as a server abort. Otherwise it's a
7152 * server abort.
7153 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007154 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007155 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01007156 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007157 if (!(s->flags & SF_ERR_MASK))
7158 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007159 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007160 if (objt_server(s->target))
7161 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007162 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01007163 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01007164
Willy Tarreau40dba092010-03-04 18:14:51 +01007165 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007166 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01007167 goto return_bad_res;
7168
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007169 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007170 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007171 * Similarly, with keep-alive on the client side, we don't want to forward a
7172 * close.
7173 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007174 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007175 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7176 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007177 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007178
Willy Tarreau5c620922011-05-11 19:56:11 +02007179 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007180 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007181 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007182 * modes are already handled by the stream sock layer. We must not do
7183 * this in content-length mode because it could present the MSG_MORE
7184 * flag with the last block of forwarded data, which would cause an
7185 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007186 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007187 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007188 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007189
Willy Tarreau87b09662015-04-03 00:22:06 +02007190 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007191 return 0;
7192
Willy Tarreau40dba092010-03-04 18:14:51 +01007193 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007194 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007195 if (objt_server(s->target))
7196 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007197
7198 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02007199 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02007200 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02007201 compressing = 0;
7202 }
7203
Willy Tarreauc623c172014-04-18 09:53:50 +02007204 /* we may have some pending data starting at res->buf->p */
7205 if (s->comp_algo == NULL) {
7206 b_adv(res->buf, msg->next);
7207 msg->next = 0;
7208 }
7209
Willy Tarreaud98cf932009-12-27 22:54:55 +01007210 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007211 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007212 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007213 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007214 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007215 if (objt_server(s->target))
7216 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007217
Willy Tarreaue7dff022015-04-03 01:14:29 +02007218 if (!(s->flags & SF_ERR_MASK))
7219 s->flags |= SF_ERR_PRXCOND;
7220 if (!(s->flags & SF_FINST_MASK))
7221 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007222 return 0;
7223
7224 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02007225 if (unlikely(compressing)) {
7226 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
7227 compressing = 0;
7228 }
7229
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007230 txn->rsp.msg_state = HTTP_MSG_ERROR;
7231 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007232 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007233 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007234 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007235
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007236 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007237 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007238 if (objt_server(s->target))
7239 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007240
Willy Tarreaue7dff022015-04-03 01:14:29 +02007241 if (!(s->flags & SF_ERR_MASK))
7242 s->flags |= SF_ERR_CLICL;
7243 if (!(s->flags & SF_FINST_MASK))
7244 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007245 return 0;
7246}
7247
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007248/* Iterate the same filter through all request headers.
7249 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007250 * Since it can manage the switch to another backend, it updates the per-proxy
7251 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007252 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007253int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007254{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007255 char *cur_ptr, *cur_end, *cur_next;
7256 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007257 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007258 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007259 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007260
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007261 last_hdr = 0;
7262
Willy Tarreau9b28e032012-10-12 23:49:43 +02007263 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007264 old_idx = 0;
7265
7266 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007267 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007268 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007269 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007270 (exp->action == ACT_ALLOW ||
7271 exp->action == ACT_DENY ||
7272 exp->action == ACT_TARPIT))
7273 return 0;
7274
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007275 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007276 if (!cur_idx)
7277 break;
7278
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007279 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007280 cur_ptr = cur_next;
7281 cur_end = cur_ptr + cur_hdr->len;
7282 cur_next = cur_end + cur_hdr->cr + 1;
7283
7284 /* Now we have one header between cur_ptr and cur_end,
7285 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007286 */
7287
Willy Tarreau15a53a42015-01-21 13:39:42 +01007288 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007289 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007290 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007291 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007292 last_hdr = 1;
7293 break;
7294
7295 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007296 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007297 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007298 break;
7299
7300 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007301 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007302 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007303 break;
7304
7305 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007306 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7307 if (trash.len < 0)
7308 return -1;
7309
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007310 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007311 /* FIXME: if the user adds a newline in the replacement, the
7312 * index will not be recalculated for now, and the new line
7313 * will not be counted as a new header.
7314 */
7315
7316 cur_end += delta;
7317 cur_next += delta;
7318 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007319 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007320 break;
7321
7322 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007323 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007324 cur_next += delta;
7325
Willy Tarreaufa355d42009-11-29 18:12:29 +01007326 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007327 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7328 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007329 cur_hdr->len = 0;
7330 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007331 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007332 break;
7333
7334 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007335 }
7336
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007337 /* keep the link from this header to next one in case of later
7338 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007339 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007340 old_idx = cur_idx;
7341 }
7342 return 0;
7343}
7344
7345
7346/* Apply the filter to the request line.
7347 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7348 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007349 * Since it can manage the switch to another backend, it updates the per-proxy
7350 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007351 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007352int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007353{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007354 char *cur_ptr, *cur_end;
7355 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007356 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007357 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007358
Willy Tarreau3d300592007-03-18 18:34:41 +01007359 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007360 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007361 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007362 (exp->action == ACT_ALLOW ||
7363 exp->action == ACT_DENY ||
7364 exp->action == ACT_TARPIT))
7365 return 0;
7366 else if (exp->action == ACT_REMOVE)
7367 return 0;
7368
7369 done = 0;
7370
Willy Tarreau9b28e032012-10-12 23:49:43 +02007371 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007372 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007373
7374 /* Now we have the request line between cur_ptr and cur_end */
7375
Willy Tarreau15a53a42015-01-21 13:39:42 +01007376 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007377 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007378 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007379 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007380 done = 1;
7381 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007382
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007383 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007384 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007385 done = 1;
7386 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007387
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007388 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007389 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007390 done = 1;
7391 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007393 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007394 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7395 if (trash.len < 0)
7396 return -1;
7397
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007398 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007399 /* FIXME: if the user adds a newline in the replacement, the
7400 * index will not be recalculated for now, and the new line
7401 * will not be counted as a new header.
7402 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007403
Willy Tarreaufa355d42009-11-29 18:12:29 +01007404 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007405 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007406 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007407 HTTP_MSG_RQMETH,
7408 cur_ptr, cur_end + 1,
7409 NULL, NULL);
7410 if (unlikely(!cur_end))
7411 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007412
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007413 /* we have a full request and we know that we have either a CR
7414 * or an LF at <ptr>.
7415 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007416 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7417 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007418 /* there is no point trying this regex on headers */
7419 return 1;
7420 }
7421 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007422 return done;
7423}
Willy Tarreau97de6242006-12-27 17:18:38 +01007424
Willy Tarreau58f10d72006-12-04 02:26:12 +01007425
Willy Tarreau58f10d72006-12-04 02:26:12 +01007426
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007427/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007428 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007429 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007430 * unparsable request. Since it can manage the switch to another backend, it
7431 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007432 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007433int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007434{
Willy Tarreau192252e2015-04-04 01:47:55 +02007435 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007436 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007437 struct hdr_exp *exp;
7438
7439 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007440 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007441
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007442 /*
7443 * The interleaving of transformations and verdicts
7444 * makes it difficult to decide to continue or stop
7445 * the evaluation.
7446 */
7447
Willy Tarreau6c123b12010-01-28 20:22:06 +01007448 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7449 break;
7450
Willy Tarreau3d300592007-03-18 18:34:41 +01007451 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007452 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007453 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007454 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007455
7456 /* if this filter had a condition, evaluate it now and skip to
7457 * next filter if the condition does not match.
7458 */
7459 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007460 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007461 ret = acl_pass(ret);
7462 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7463 ret = !ret;
7464
7465 if (!ret)
7466 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007467 }
7468
7469 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007470 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007471 if (unlikely(ret < 0))
7472 return -1;
7473
7474 if (likely(ret == 0)) {
7475 /* The filter did not match the request, it can be
7476 * iterated through all headers.
7477 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007478 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7479 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007480 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007481 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007482 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007483}
7484
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007485
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007486/* Find the end of a cookie value contained between <s> and <e>. It works the
7487 * same way as with headers above except that the semi-colon also ends a token.
7488 * See RFC2965 for more information. Note that it requires a valid header to
7489 * return a valid result.
7490 */
7491char *find_cookie_value_end(char *s, const char *e)
7492{
7493 int quoted, qdpair;
7494
7495 quoted = qdpair = 0;
7496 for (; s < e; s++) {
7497 if (qdpair) qdpair = 0;
7498 else if (quoted) {
7499 if (*s == '\\') qdpair = 1;
7500 else if (*s == '"') quoted = 0;
7501 }
7502 else if (*s == '"') quoted = 1;
7503 else if (*s == ',' || *s == ';') return s;
7504 }
7505 return s;
7506}
7507
7508/* Delete a value in a header between delimiters <from> and <next> in buffer
7509 * <buf>. The number of characters displaced is returned, and the pointer to
7510 * the first delimiter is updated if required. The function tries as much as
7511 * possible to respect the following principles :
7512 * - replace <from> delimiter by the <next> one unless <from> points to a
7513 * colon, in which case <next> is simply removed
7514 * - set exactly one space character after the new first delimiter, unless
7515 * there are not enough characters in the block being moved to do so.
7516 * - remove unneeded spaces before the previous delimiter and after the new
7517 * one.
7518 *
7519 * It is the caller's responsibility to ensure that :
7520 * - <from> points to a valid delimiter or the colon ;
7521 * - <next> points to a valid delimiter or the final CR/LF ;
7522 * - there are non-space chars before <from> ;
7523 * - there is a CR/LF at or after <next>.
7524 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007525int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007526{
7527 char *prev = *from;
7528
7529 if (*prev == ':') {
7530 /* We're removing the first value, preserve the colon and add a
7531 * space if possible.
7532 */
7533 if (!http_is_crlf[(unsigned char)*next])
7534 next++;
7535 prev++;
7536 if (prev < next)
7537 *prev++ = ' ';
7538
7539 while (http_is_spht[(unsigned char)*next])
7540 next++;
7541 } else {
7542 /* Remove useless spaces before the old delimiter. */
7543 while (http_is_spht[(unsigned char)*(prev-1)])
7544 prev--;
7545 *from = prev;
7546
7547 /* copy the delimiter and if possible a space if we're
7548 * not at the end of the line.
7549 */
7550 if (!http_is_crlf[(unsigned char)*next]) {
7551 *prev++ = *next++;
7552 if (prev + 1 < next)
7553 *prev++ = ' ';
7554 while (http_is_spht[(unsigned char)*next])
7555 next++;
7556 }
7557 }
7558 return buffer_replace2(buf, prev, next, NULL, 0);
7559}
7560
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007561/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007562 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007563 * desirable to call it only when needed. This code is quite complex because
7564 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7565 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007566 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007567void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007568{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007569 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007570 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007571 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007572 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007573 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7574 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007575
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007576 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007577 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007578 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007579
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007580 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007581 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007582 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007583
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007584 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007585 hdr_beg = hdr_next;
7586 hdr_end = hdr_beg + cur_hdr->len;
7587 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007588
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007589 /* We have one full header between hdr_beg and hdr_end, and the
7590 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007591 * "Cookie:" headers.
7592 */
7593
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007594 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007595 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007596 old_idx = cur_idx;
7597 continue;
7598 }
7599
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007600 del_from = NULL; /* nothing to be deleted */
7601 preserve_hdr = 0; /* assume we may kill the whole header */
7602
Willy Tarreau58f10d72006-12-04 02:26:12 +01007603 /* Now look for cookies. Conforming to RFC2109, we have to support
7604 * attributes whose name begin with a '$', and associate them with
7605 * the right cookie, if we want to delete this cookie.
7606 * So there are 3 cases for each cookie read :
7607 * 1) it's a special attribute, beginning with a '$' : ignore it.
7608 * 2) it's a server id cookie that we *MAY* want to delete : save
7609 * some pointers on it (last semi-colon, beginning of cookie...)
7610 * 3) it's an application cookie : we *MAY* have to delete a previous
7611 * "special" cookie.
7612 * At the end of loop, if a "special" cookie remains, we may have to
7613 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007614 * *MUST* delete it.
7615 *
7616 * Note: RFC2965 is unclear about the processing of spaces around
7617 * the equal sign in the ATTR=VALUE form. A careful inspection of
7618 * the RFC explicitly allows spaces before it, and not within the
7619 * tokens (attrs or values). An inspection of RFC2109 allows that
7620 * too but section 10.1.3 lets one think that spaces may be allowed
7621 * after the equal sign too, resulting in some (rare) buggy
7622 * implementations trying to do that. So let's do what servers do.
7623 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7624 * allowed quoted strings in values, with any possible character
7625 * after a backslash, including control chars and delimitors, which
7626 * causes parsing to become ambiguous. Browsers also allow spaces
7627 * within values even without quotes.
7628 *
7629 * We have to keep multiple pointers in order to support cookie
7630 * removal at the beginning, middle or end of header without
7631 * corrupting the header. All of these headers are valid :
7632 *
7633 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7634 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7635 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7636 * | | | | | | | | |
7637 * | | | | | | | | hdr_end <--+
7638 * | | | | | | | +--> next
7639 * | | | | | | +----> val_end
7640 * | | | | | +-----------> val_beg
7641 * | | | | +--------------> equal
7642 * | | | +----------------> att_end
7643 * | | +---------------------> att_beg
7644 * | +--------------------------> prev
7645 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007646 */
7647
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007648 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7649 /* Iterate through all cookies on this line */
7650
7651 /* find att_beg */
7652 att_beg = prev + 1;
7653 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7654 att_beg++;
7655
7656 /* find att_end : this is the first character after the last non
7657 * space before the equal. It may be equal to hdr_end.
7658 */
7659 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007660
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007661 while (equal < hdr_end) {
7662 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007663 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007664 if (http_is_spht[(unsigned char)*equal++])
7665 continue;
7666 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007667 }
7668
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007669 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7670 * is between <att_beg> and <equal>, both may be identical.
7671 */
7672
7673 /* look for end of cookie if there is an equal sign */
7674 if (equal < hdr_end && *equal == '=') {
7675 /* look for the beginning of the value */
7676 val_beg = equal + 1;
7677 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7678 val_beg++;
7679
7680 /* find the end of the value, respecting quotes */
7681 next = find_cookie_value_end(val_beg, hdr_end);
7682
7683 /* make val_end point to the first white space or delimitor after the value */
7684 val_end = next;
7685 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7686 val_end--;
7687 } else {
7688 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007689 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007690
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007691 /* We have nothing to do with attributes beginning with '$'. However,
7692 * they will automatically be removed if a header before them is removed,
7693 * since they're supposed to be linked together.
7694 */
7695 if (*att_beg == '$')
7696 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007697
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007698 /* Ignore cookies with no equal sign */
7699 if (equal == next) {
7700 /* This is not our cookie, so we must preserve it. But if we already
7701 * scheduled another cookie for removal, we cannot remove the
7702 * complete header, but we can remove the previous block itself.
7703 */
7704 preserve_hdr = 1;
7705 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007706 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007707 val_end += delta;
7708 next += delta;
7709 hdr_end += delta;
7710 hdr_next += delta;
7711 cur_hdr->len += delta;
7712 http_msg_move_end(&txn->req, delta);
7713 prev = del_from;
7714 del_from = NULL;
7715 }
7716 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007717 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007718
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007719 /* if there are spaces around the equal sign, we need to
7720 * strip them otherwise we'll get trouble for cookie captures,
7721 * or even for rewrites. Since this happens extremely rarely,
7722 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007723 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007724 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7725 int stripped_before = 0;
7726 int stripped_after = 0;
7727
7728 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007729 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007730 equal += stripped_before;
7731 val_beg += stripped_before;
7732 }
7733
7734 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007735 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007736 val_beg += stripped_after;
7737 stripped_before += stripped_after;
7738 }
7739
7740 val_end += stripped_before;
7741 next += stripped_before;
7742 hdr_end += stripped_before;
7743 hdr_next += stripped_before;
7744 cur_hdr->len += stripped_before;
7745 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007746 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007747 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007748
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007749 /* First, let's see if we want to capture this cookie. We check
7750 * that we don't already have a client side cookie, because we
7751 * can only capture one. Also as an optimisation, we ignore
7752 * cookies shorter than the declared name.
7753 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007754 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7755 (val_end - att_beg >= sess->fe->capture_namelen) &&
7756 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007757 int log_len = val_end - att_beg;
7758
7759 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7760 Alert("HTTP logging : out of memory.\n");
7761 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007762 if (log_len > sess->fe->capture_len)
7763 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007764 memcpy(txn->cli_cookie, att_beg, log_len);
7765 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007766 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007767 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007768
Willy Tarreaubca99692010-10-06 19:25:55 +02007769 /* Persistence cookies in passive, rewrite or insert mode have the
7770 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007771 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007772 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007773 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007774 * For cookies in prefix mode, the form is :
7775 *
7776 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007777 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007778 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7779 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7780 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007781 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007782
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007783 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7784 * have the server ID between val_beg and delim, and the original cookie between
7785 * delim+1 and val_end. Otherwise, delim==val_end :
7786 *
7787 * Cookie: NAME=SRV; # in all but prefix modes
7788 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7789 * | || || | |+-> next
7790 * | || || | +--> val_end
7791 * | || || +---------> delim
7792 * | || |+------------> val_beg
7793 * | || +-------------> att_end = equal
7794 * | |+-----------------> att_beg
7795 * | +------------------> prev
7796 * +-------------------------> hdr_beg
7797 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007798
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007799 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007800 for (delim = val_beg; delim < val_end; delim++)
7801 if (*delim == COOKIE_DELIM)
7802 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007803 } else {
7804 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007805 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007806 /* Now check if the cookie contains a date field, which would
7807 * appear after a vertical bar ('|') just after the server name
7808 * and before the delimiter.
7809 */
7810 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7811 if (vbar1) {
7812 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007813 * right is the last seen date. It is a base64 encoded
7814 * 30-bit value representing the UNIX date since the
7815 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007816 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007817 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007818 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007819 if (val_end - vbar1 >= 5) {
7820 val = b64tos30(vbar1);
7821 if (val > 0)
7822 txn->cookie_last_date = val << 2;
7823 }
7824 /* look for a second vertical bar */
7825 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7826 if (vbar1 && (val_end - vbar1 > 5)) {
7827 val = b64tos30(vbar1 + 1);
7828 if (val > 0)
7829 txn->cookie_first_date = val << 2;
7830 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007831 }
7832 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007833
Willy Tarreauf64d1412010-10-07 20:06:11 +02007834 /* if the cookie has an expiration date and the proxy wants to check
7835 * it, then we do that now. We first check if the cookie is too old,
7836 * then only if it has expired. We detect strict overflow because the
7837 * time resolution here is not great (4 seconds). Cookies with dates
7838 * in the future are ignored if their offset is beyond one day. This
7839 * allows an admin to fix timezone issues without expiring everyone
7840 * and at the same time avoids keeping unwanted side effects for too
7841 * long.
7842 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007843 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7844 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007845 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007846 txn->flags &= ~TX_CK_MASK;
7847 txn->flags |= TX_CK_OLD;
7848 delim = val_beg; // let's pretend we have not found the cookie
7849 txn->cookie_first_date = 0;
7850 txn->cookie_last_date = 0;
7851 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007852 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7853 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007854 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007855 txn->flags &= ~TX_CK_MASK;
7856 txn->flags |= TX_CK_EXPIRED;
7857 delim = val_beg; // let's pretend we have not found the cookie
7858 txn->cookie_first_date = 0;
7859 txn->cookie_last_date = 0;
7860 }
7861
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007862 /* Here, we'll look for the first running server which supports the cookie.
7863 * This allows to share a same cookie between several servers, for example
7864 * to dedicate backup servers to specific servers only.
7865 * However, to prevent clients from sticking to cookie-less backup server
7866 * when they have incidentely learned an empty cookie, we simply ignore
7867 * empty cookies and mark them as invalid.
7868 * The same behaviour is applied when persistence must be ignored.
7869 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007870 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007871 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007872
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007873 while (srv) {
7874 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7875 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007876 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007877 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007878 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007879 /* we found the server and we can use it */
7880 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007881 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007882 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007883 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007884 break;
7885 } else {
7886 /* we found a server, but it's down,
7887 * mark it as such and go on in case
7888 * another one is available.
7889 */
7890 txn->flags &= ~TX_CK_MASK;
7891 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007892 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007893 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007894 srv = srv->next;
7895 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007896
Willy Tarreauf64d1412010-10-07 20:06:11 +02007897 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007898 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007899 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007900 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007901 txn->flags |= TX_CK_UNUSED;
7902 else
7903 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007904 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007905
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007906 /* depending on the cookie mode, we may have to either :
7907 * - delete the complete cookie if we're in insert+indirect mode, so that
7908 * the server never sees it ;
7909 * - remove the server id from the cookie value, and tag the cookie as an
7910 * application cookie so that it does not get accidentely removed later,
7911 * if we're in cookie prefix mode
7912 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007913 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007914 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007915
Willy Tarreau9b28e032012-10-12 23:49:43 +02007916 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007917 val_end += delta;
7918 next += delta;
7919 hdr_end += delta;
7920 hdr_next += delta;
7921 cur_hdr->len += delta;
7922 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007923
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007924 del_from = NULL;
7925 preserve_hdr = 1; /* we want to keep this cookie */
7926 }
7927 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007928 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007929 del_from = prev;
7930 }
7931 } else {
7932 /* This is not our cookie, so we must preserve it. But if we already
7933 * scheduled another cookie for removal, we cannot remove the
7934 * complete header, but we can remove the previous block itself.
7935 */
7936 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007937
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007938 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007939 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007940 if (att_beg >= del_from)
7941 att_beg += delta;
7942 if (att_end >= del_from)
7943 att_end += delta;
7944 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007945 val_end += delta;
7946 next += delta;
7947 hdr_end += delta;
7948 hdr_next += delta;
7949 cur_hdr->len += delta;
7950 http_msg_move_end(&txn->req, delta);
7951 prev = del_from;
7952 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007953 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007954 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007955
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007956 /* continue with next cookie on this header line */
7957 att_beg = next;
7958 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007959
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007960 /* There are no more cookies on this line.
7961 * We may still have one (or several) marked for deletion at the
7962 * end of the line. We must do this now in two ways :
7963 * - if some cookies must be preserved, we only delete from the
7964 * mark to the end of line ;
7965 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007966 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007967 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007968 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007969 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007970 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007971 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007972 cur_hdr->len += delta;
7973 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007974 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007975
7976 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007977 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7978 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007979 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007980 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007981 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007982 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007983 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007984 }
7985
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007986 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007987 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007988 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007989}
7990
7991
Willy Tarreaua15645d2007-03-18 16:22:39 +01007992/* Iterate the same filter through all response headers contained in <rtr>.
7993 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7994 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007995int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007996{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007997 char *cur_ptr, *cur_end, *cur_next;
7998 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007999 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008000 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008001 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008002
8003 last_hdr = 0;
8004
Willy Tarreau9b28e032012-10-12 23:49:43 +02008005 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008006 old_idx = 0;
8007
8008 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008009 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008010 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008011 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008012 (exp->action == ACT_ALLOW ||
8013 exp->action == ACT_DENY))
8014 return 0;
8015
8016 cur_idx = txn->hdr_idx.v[old_idx].next;
8017 if (!cur_idx)
8018 break;
8019
8020 cur_hdr = &txn->hdr_idx.v[cur_idx];
8021 cur_ptr = cur_next;
8022 cur_end = cur_ptr + cur_hdr->len;
8023 cur_next = cur_end + cur_hdr->cr + 1;
8024
8025 /* Now we have one header between cur_ptr and cur_end,
8026 * and the next header starts at cur_next.
8027 */
8028
Willy Tarreau15a53a42015-01-21 13:39:42 +01008029 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008030 switch (exp->action) {
8031 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008032 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008033 last_hdr = 1;
8034 break;
8035
8036 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008037 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008038 last_hdr = 1;
8039 break;
8040
8041 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008042 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8043 if (trash.len < 0)
8044 return -1;
8045
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008046 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008047 /* FIXME: if the user adds a newline in the replacement, the
8048 * index will not be recalculated for now, and the new line
8049 * will not be counted as a new header.
8050 */
8051
8052 cur_end += delta;
8053 cur_next += delta;
8054 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008055 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008056 break;
8057
8058 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008059 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008060 cur_next += delta;
8061
Willy Tarreaufa355d42009-11-29 18:12:29 +01008062 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008063 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8064 txn->hdr_idx.used--;
8065 cur_hdr->len = 0;
8066 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008067 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008068 break;
8069
8070 }
8071 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008072
8073 /* keep the link from this header to next one in case of later
8074 * removal of next header.
8075 */
8076 old_idx = cur_idx;
8077 }
8078 return 0;
8079}
8080
8081
8082/* Apply the filter to the status line in the response buffer <rtr>.
8083 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8084 * or -1 if a replacement resulted in an invalid status line.
8085 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008086int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008087{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008088 char *cur_ptr, *cur_end;
8089 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008090 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008091 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008092
8093
Willy Tarreau3d300592007-03-18 18:34:41 +01008094 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008095 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008096 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008097 (exp->action == ACT_ALLOW ||
8098 exp->action == ACT_DENY))
8099 return 0;
8100 else if (exp->action == ACT_REMOVE)
8101 return 0;
8102
8103 done = 0;
8104
Willy Tarreau9b28e032012-10-12 23:49:43 +02008105 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008106 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008107
8108 /* Now we have the status line between cur_ptr and cur_end */
8109
Willy Tarreau15a53a42015-01-21 13:39:42 +01008110 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008111 switch (exp->action) {
8112 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008113 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008114 done = 1;
8115 break;
8116
8117 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008118 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008119 done = 1;
8120 break;
8121
8122 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008123 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8124 if (trash.len < 0)
8125 return -1;
8126
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008127 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008128 /* FIXME: if the user adds a newline in the replacement, the
8129 * index will not be recalculated for now, and the new line
8130 * will not be counted as a new header.
8131 */
8132
Willy Tarreaufa355d42009-11-29 18:12:29 +01008133 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008134 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008135 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008136 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008137 cur_ptr, cur_end + 1,
8138 NULL, NULL);
8139 if (unlikely(!cur_end))
8140 return -1;
8141
8142 /* we have a full respnse and we know that we have either a CR
8143 * or an LF at <ptr>.
8144 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008145 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008146 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008147 /* there is no point trying this regex on headers */
8148 return 1;
8149 }
8150 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008151 return done;
8152}
8153
8154
8155
8156/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008157 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008158 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8159 * unparsable response.
8160 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008161int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008162{
Willy Tarreau192252e2015-04-04 01:47:55 +02008163 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008164 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008165 struct hdr_exp *exp;
8166
8167 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008168 int ret;
8169
8170 /*
8171 * The interleaving of transformations and verdicts
8172 * makes it difficult to decide to continue or stop
8173 * the evaluation.
8174 */
8175
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008176 if (txn->flags & TX_SVDENY)
8177 break;
8178
Willy Tarreau3d300592007-03-18 18:34:41 +01008179 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008180 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8181 exp->action == ACT_PASS)) {
8182 exp = exp->next;
8183 continue;
8184 }
8185
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008186 /* if this filter had a condition, evaluate it now and skip to
8187 * next filter if the condition does not match.
8188 */
8189 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008190 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008191 ret = acl_pass(ret);
8192 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8193 ret = !ret;
8194 if (!ret)
8195 continue;
8196 }
8197
Willy Tarreaua15645d2007-03-18 16:22:39 +01008198 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008199 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008200 if (unlikely(ret < 0))
8201 return -1;
8202
8203 if (likely(ret == 0)) {
8204 /* The filter did not match the response, it can be
8205 * iterated through all headers.
8206 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008207 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8208 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008209 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008210 }
8211 return 0;
8212}
8213
8214
Willy Tarreaua15645d2007-03-18 16:22:39 +01008215/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008216 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008217 * desirable to call it only when needed. This function is also used when we
8218 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008219 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008220void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008221{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008222 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008223 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008224 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008225 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008226 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008227 char *hdr_beg, *hdr_end, *hdr_next;
8228 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008229
Willy Tarreaua15645d2007-03-18 16:22:39 +01008230 /* Iterate through the headers.
8231 * we start with the start line.
8232 */
8233 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008234 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008235
8236 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8237 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008238 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008239
8240 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008241 hdr_beg = hdr_next;
8242 hdr_end = hdr_beg + cur_hdr->len;
8243 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008244
Willy Tarreau24581ba2010-08-31 22:39:35 +02008245 /* We have one full header between hdr_beg and hdr_end, and the
8246 * next header starts at hdr_next. We're only interested in
8247 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008248 */
8249
Willy Tarreau24581ba2010-08-31 22:39:35 +02008250 is_cookie2 = 0;
8251 prev = hdr_beg + 10;
8252 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008253 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008254 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8255 if (!val) {
8256 old_idx = cur_idx;
8257 continue;
8258 }
8259 is_cookie2 = 1;
8260 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008261 }
8262
Willy Tarreau24581ba2010-08-31 22:39:35 +02008263 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8264 * <prev> points to the colon.
8265 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008266 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008267
Willy Tarreau24581ba2010-08-31 22:39:35 +02008268 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8269 * check-cache is enabled) and we are not interested in checking
8270 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008271 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008272 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008273 return;
8274
Willy Tarreau24581ba2010-08-31 22:39:35 +02008275 /* OK so now we know we have to process this response cookie.
8276 * The format of the Set-Cookie header is slightly different
8277 * from the format of the Cookie header in that it does not
8278 * support the comma as a cookie delimiter (thus the header
8279 * cannot be folded) because the Expires attribute described in
8280 * the original Netscape's spec may contain an unquoted date
8281 * with a comma inside. We have to live with this because
8282 * many browsers don't support Max-Age and some browsers don't
8283 * support quoted strings. However the Set-Cookie2 header is
8284 * clean.
8285 *
8286 * We have to keep multiple pointers in order to support cookie
8287 * removal at the beginning, middle or end of header without
8288 * corrupting the header (in case of set-cookie2). A special
8289 * pointer, <scav> points to the beginning of the set-cookie-av
8290 * fields after the first semi-colon. The <next> pointer points
8291 * either to the end of line (set-cookie) or next unquoted comma
8292 * (set-cookie2). All of these headers are valid :
8293 *
8294 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8295 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8296 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8297 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8298 * | | | | | | | | | |
8299 * | | | | | | | | +-> next hdr_end <--+
8300 * | | | | | | | +------------> scav
8301 * | | | | | | +--------------> val_end
8302 * | | | | | +--------------------> val_beg
8303 * | | | | +----------------------> equal
8304 * | | | +------------------------> att_end
8305 * | | +----------------------------> att_beg
8306 * | +------------------------------> prev
8307 * +-----------------------------------------> hdr_beg
8308 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008309
Willy Tarreau24581ba2010-08-31 22:39:35 +02008310 for (; prev < hdr_end; prev = next) {
8311 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008312
Willy Tarreau24581ba2010-08-31 22:39:35 +02008313 /* find att_beg */
8314 att_beg = prev + 1;
8315 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8316 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008317
Willy Tarreau24581ba2010-08-31 22:39:35 +02008318 /* find att_end : this is the first character after the last non
8319 * space before the equal. It may be equal to hdr_end.
8320 */
8321 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008322
Willy Tarreau24581ba2010-08-31 22:39:35 +02008323 while (equal < hdr_end) {
8324 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8325 break;
8326 if (http_is_spht[(unsigned char)*equal++])
8327 continue;
8328 att_end = equal;
8329 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008330
Willy Tarreau24581ba2010-08-31 22:39:35 +02008331 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8332 * is between <att_beg> and <equal>, both may be identical.
8333 */
8334
8335 /* look for end of cookie if there is an equal sign */
8336 if (equal < hdr_end && *equal == '=') {
8337 /* look for the beginning of the value */
8338 val_beg = equal + 1;
8339 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8340 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008341
Willy Tarreau24581ba2010-08-31 22:39:35 +02008342 /* find the end of the value, respecting quotes */
8343 next = find_cookie_value_end(val_beg, hdr_end);
8344
8345 /* make val_end point to the first white space or delimitor after the value */
8346 val_end = next;
8347 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8348 val_end--;
8349 } else {
8350 /* <equal> points to next comma, semi-colon or EOL */
8351 val_beg = val_end = next = equal;
8352 }
8353
8354 if (next < hdr_end) {
8355 /* Set-Cookie2 supports multiple cookies, and <next> points to
8356 * a colon or semi-colon before the end. So skip all attr-value
8357 * pairs and look for the next comma. For Set-Cookie, since
8358 * commas are permitted in values, skip to the end.
8359 */
8360 if (is_cookie2)
8361 next = find_hdr_value_end(next, hdr_end);
8362 else
8363 next = hdr_end;
8364 }
8365
8366 /* Now everything is as on the diagram above */
8367
8368 /* Ignore cookies with no equal sign */
8369 if (equal == val_end)
8370 continue;
8371
8372 /* If there are spaces around the equal sign, we need to
8373 * strip them otherwise we'll get trouble for cookie captures,
8374 * or even for rewrites. Since this happens extremely rarely,
8375 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008376 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008377 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8378 int stripped_before = 0;
8379 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008380
Willy Tarreau24581ba2010-08-31 22:39:35 +02008381 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008382 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008383 equal += stripped_before;
8384 val_beg += stripped_before;
8385 }
8386
8387 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008388 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008389 val_beg += stripped_after;
8390 stripped_before += stripped_after;
8391 }
8392
8393 val_end += stripped_before;
8394 next += stripped_before;
8395 hdr_end += stripped_before;
8396 hdr_next += stripped_before;
8397 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008398 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008399 }
8400
8401 /* First, let's see if we want to capture this cookie. We check
8402 * that we don't already have a server side cookie, because we
8403 * can only capture one. Also as an optimisation, we ignore
8404 * cookies shorter than the declared name.
8405 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008406 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008407 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008408 (val_end - att_beg >= sess->fe->capture_namelen) &&
8409 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008410 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008411 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008412 Alert("HTTP logging : out of memory.\n");
8413 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008414 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008415 if (log_len > sess->fe->capture_len)
8416 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008417 memcpy(txn->srv_cookie, att_beg, log_len);
8418 txn->srv_cookie[log_len] = 0;
8419 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008420 }
8421
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008422 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008423 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008424 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008425 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8426 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008427 /* assume passive cookie by default */
8428 txn->flags &= ~TX_SCK_MASK;
8429 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008430
8431 /* If the cookie is in insert mode on a known server, we'll delete
8432 * this occurrence because we'll insert another one later.
8433 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008434 * a direct access.
8435 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008436 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008437 /* The "preserve" flag was set, we don't want to touch the
8438 * server's cookie.
8439 */
8440 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008441 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008442 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008443 /* this cookie must be deleted */
8444 if (*prev == ':' && next == hdr_end) {
8445 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008446 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008447 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8448 txn->hdr_idx.used--;
8449 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008450 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008451 hdr_next += delta;
8452 http_msg_move_end(&txn->rsp, delta);
8453 /* note: while both invalid now, <next> and <hdr_end>
8454 * are still equal, so the for() will stop as expected.
8455 */
8456 } else {
8457 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008458 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008459 next = prev;
8460 hdr_end += delta;
8461 hdr_next += delta;
8462 cur_hdr->len += delta;
8463 http_msg_move_end(&txn->rsp, delta);
8464 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008465 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008466 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008467 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008468 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008469 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008470 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008471 * with this server since we know it.
8472 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008473 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008474 next += delta;
8475 hdr_end += delta;
8476 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008477 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008478 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008479
Willy Tarreauf1348312010-10-07 15:54:11 +02008480 txn->flags &= ~TX_SCK_MASK;
8481 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008482 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008483 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008484 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008485 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008486 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008487 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008488 next += delta;
8489 hdr_end += delta;
8490 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008491 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008492 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008493
Willy Tarreau827aee92011-03-10 16:55:02 +01008494 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008495 txn->flags &= ~TX_SCK_MASK;
8496 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008497 }
8498 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008499 /* that's done for this cookie, check the next one on the same
8500 * line when next != hdr_end (only if is_cookie2).
8501 */
8502 }
8503 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008504 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008505 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008506}
8507
8508
Willy Tarreaua15645d2007-03-18 16:22:39 +01008509/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008510 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008511 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008512void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008513{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008514 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008515 char *p1, *p2;
8516
8517 char *cur_ptr, *cur_end, *cur_next;
8518 int cur_idx;
8519
Willy Tarreau5df51872007-11-25 16:20:08 +01008520 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008521 return;
8522
8523 /* Iterate through the headers.
8524 * we start with the start line.
8525 */
8526 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008527 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008528
8529 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8530 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008531 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008532
8533 cur_hdr = &txn->hdr_idx.v[cur_idx];
8534 cur_ptr = cur_next;
8535 cur_end = cur_ptr + cur_hdr->len;
8536 cur_next = cur_end + cur_hdr->cr + 1;
8537
8538 /* We have one full header between cur_ptr and cur_end, and the
8539 * next header starts at cur_next. We're only interested in
8540 * "Cookie:" headers.
8541 */
8542
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008543 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8544 if (val) {
8545 if ((cur_end - (cur_ptr + val) >= 8) &&
8546 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8547 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8548 return;
8549 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008550 }
8551
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008552 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8553 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008554 continue;
8555
8556 /* OK, right now we know we have a cache-control header at cur_ptr */
8557
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008558 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008559
8560 if (p1 >= cur_end) /* no more info */
8561 continue;
8562
8563 /* p1 is at the beginning of the value */
8564 p2 = p1;
8565
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008566 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008567 p2++;
8568
8569 /* we have a complete value between p1 and p2 */
8570 if (p2 < cur_end && *p2 == '=') {
8571 /* we have something of the form no-cache="set-cookie" */
8572 if ((cur_end - p1 >= 21) &&
8573 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8574 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008575 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008576 continue;
8577 }
8578
8579 /* OK, so we know that either p2 points to the end of string or to a comma */
8580 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008581 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008582 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8583 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8584 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008585 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008586 return;
8587 }
8588
8589 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008590 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008591 continue;
8592 }
8593 }
8594}
8595
Willy Tarreau58f10d72006-12-04 02:26:12 +01008596
Willy Tarreaub2513902006-12-17 14:52:38 +01008597/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008598 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008599 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008600 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008601 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008602 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008603 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008604 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008605 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008606int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008607{
8608 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008609 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008610 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008611
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008612 if (!uri_auth)
8613 return 0;
8614
Cyril Bonté70be45d2010-10-12 00:14:35 +02008615 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008616 return 0;
8617
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008618 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008619 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008620 return 0;
8621
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008622 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008623 return 0;
8624
Willy Tarreaub2513902006-12-17 14:52:38 +01008625 return 1;
8626}
8627
Willy Tarreau4076a152009-04-02 15:18:36 +02008628/*
8629 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008630 * By default it tries to report the error position as msg->err_pos. However if
8631 * this one is not set, it will then report msg->next, which is the last known
8632 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008633 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008634 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008635void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008636 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008637 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008638{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008639 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008640 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008641 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008642
Willy Tarreau9b28e032012-10-12 23:49:43 +02008643 es->len = MIN(chn->buf->i, sizeof(es->buf));
8644 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008645 len1 = MIN(len1, es->len);
8646 len2 = es->len - len1; /* remaining data if buffer wraps */
8647
Willy Tarreau9b28e032012-10-12 23:49:43 +02008648 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008649 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008650 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008651
Willy Tarreau4076a152009-04-02 15:18:36 +02008652 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008653 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008654 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008655 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008656
Willy Tarreau4076a152009-04-02 15:18:36 +02008657 es->when = date; // user-visible date
8658 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008659 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008660 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008661 if (objt_conn(sess->origin))
8662 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008663 else
8664 memset(&es->src, 0, sizeof(es->src));
8665
Willy Tarreau078272e2010-12-12 12:46:33 +01008666 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008667 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008668 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008669 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008670 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008671 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008672 es->b_out = chn->buf->o;
8673 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008674 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008675 es->m_clen = msg->chunk_len;
8676 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008677}
Willy Tarreaub2513902006-12-17 14:52:38 +01008678
Willy Tarreau294c4732011-12-16 21:35:50 +01008679/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8680 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8681 * performed over the whole headers. Otherwise it must contain a valid header
8682 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8683 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8684 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8685 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008686 * -1. The value fetch stops at commas, so this function is suited for use with
8687 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008688 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008689 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008690unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008691 struct hdr_idx *idx, int occ,
8692 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008693{
Willy Tarreau294c4732011-12-16 21:35:50 +01008694 struct hdr_ctx local_ctx;
8695 char *ptr_hist[MAX_HDR_HISTORY];
8696 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008697 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008698 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008699
Willy Tarreau294c4732011-12-16 21:35:50 +01008700 if (!ctx) {
8701 local_ctx.idx = 0;
8702 ctx = &local_ctx;
8703 }
8704
Willy Tarreaubce70882009-09-07 11:51:47 +02008705 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008706 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008707 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008708 occ--;
8709 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008710 *vptr = ctx->line + ctx->val;
8711 *vlen = ctx->vlen;
8712 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008713 }
8714 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008715 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008716 }
8717
8718 /* negative occurrence, we scan all the list then walk back */
8719 if (-occ > MAX_HDR_HISTORY)
8720 return 0;
8721
Willy Tarreau294c4732011-12-16 21:35:50 +01008722 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008723 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008724 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8725 len_hist[hist_ptr] = ctx->vlen;
8726 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008727 hist_ptr = 0;
8728 found++;
8729 }
8730 if (-occ > found)
8731 return 0;
8732 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008733 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8734 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8735 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008736 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008737 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008738 if (hist_ptr >= MAX_HDR_HISTORY)
8739 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008740 *vptr = ptr_hist[hist_ptr];
8741 *vlen = len_hist[hist_ptr];
8742 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008743}
8744
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008745/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8746 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8747 * performed over the whole headers. Otherwise it must contain a valid header
8748 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8749 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8750 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8751 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8752 * -1. This function differs from http_get_hdr() in that it only returns full
8753 * line header values and does not stop at commas.
8754 * The return value is 0 if nothing was found, or non-zero otherwise.
8755 */
8756unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8757 struct hdr_idx *idx, int occ,
8758 struct hdr_ctx *ctx, char **vptr, int *vlen)
8759{
8760 struct hdr_ctx local_ctx;
8761 char *ptr_hist[MAX_HDR_HISTORY];
8762 int len_hist[MAX_HDR_HISTORY];
8763 unsigned int hist_ptr;
8764 int found;
8765
8766 if (!ctx) {
8767 local_ctx.idx = 0;
8768 ctx = &local_ctx;
8769 }
8770
8771 if (occ >= 0) {
8772 /* search from the beginning */
8773 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8774 occ--;
8775 if (occ <= 0) {
8776 *vptr = ctx->line + ctx->val;
8777 *vlen = ctx->vlen;
8778 return 1;
8779 }
8780 }
8781 return 0;
8782 }
8783
8784 /* negative occurrence, we scan all the list then walk back */
8785 if (-occ > MAX_HDR_HISTORY)
8786 return 0;
8787
8788 found = hist_ptr = 0;
8789 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8790 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8791 len_hist[hist_ptr] = ctx->vlen;
8792 if (++hist_ptr >= MAX_HDR_HISTORY)
8793 hist_ptr = 0;
8794 found++;
8795 }
8796 if (-occ > found)
8797 return 0;
8798 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8799 * find occurrence -occ, so we have to check [hist_ptr+occ].
8800 */
8801 hist_ptr += occ;
8802 if (hist_ptr >= MAX_HDR_HISTORY)
8803 hist_ptr -= MAX_HDR_HISTORY;
8804 *vptr = ptr_hist[hist_ptr];
8805 *vlen = len_hist[hist_ptr];
8806 return 1;
8807}
8808
Willy Tarreaubaaee002006-06-26 02:48:02 +02008809/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008810 * Print a debug line with a header. Always stop at the first CR or LF char,
8811 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8812 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008813 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008814void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008815{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008816 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008817 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008818
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008819 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008820 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008821 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008822 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 +02008823
8824 for (max = 0; start + max < end; max++)
8825 if (start[max] == '\r' || start[max] == '\n')
8826 break;
8827
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008828 UBOUND(max, trash.size - trash.len - 3);
8829 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8830 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008831 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008832}
8833
Willy Tarreaueee5b512015-04-03 23:46:31 +02008834
8835/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8836 * The hdr_idx is allocated as well. In case of allocation failure, everything
8837 * allocated is freed and NULL is returned. Otherwise the new transaction is
8838 * assigned to the stream and returned.
8839 */
8840struct http_txn *http_alloc_txn(struct stream *s)
8841{
8842 struct http_txn *txn = s->txn;
8843
8844 if (txn)
8845 return txn;
8846
8847 txn = pool_alloc2(pool2_http_txn);
8848 if (!txn)
8849 return txn;
8850
8851 txn->hdr_idx.size = global.tune.max_http_hdr;
8852 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8853 if (!txn->hdr_idx.v) {
8854 pool_free2(pool2_http_txn, txn);
8855 return NULL;
8856 }
8857
8858 s->txn = txn;
8859 return txn;
8860}
8861
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008862void http_txn_reset_req(struct http_txn *txn)
8863{
8864 txn->req.flags = 0;
8865 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8866 txn->req.next = 0;
8867 txn->req.chunk_len = 0LL;
8868 txn->req.body_len = 0LL;
8869 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8870}
8871
8872void http_txn_reset_res(struct http_txn *txn)
8873{
8874 txn->rsp.flags = 0;
8875 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8876 txn->rsp.next = 0;
8877 txn->rsp.chunk_len = 0LL;
8878 txn->rsp.body_len = 0LL;
8879 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8880}
8881
Willy Tarreau0937bc42009-12-22 15:03:09 +01008882/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008883 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008884 * the required fields are properly allocated and that we only need to (re)init
8885 * them. This should be used before processing any new request.
8886 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008887void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008888{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008889 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008890 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008891
8892 txn->flags = 0;
8893 txn->status = -1;
8894
Willy Tarreauf64d1412010-10-07 20:06:11 +02008895 txn->cookie_first_date = 0;
8896 txn->cookie_last_date = 0;
8897
Willy Tarreaueee5b512015-04-03 23:46:31 +02008898 txn->srv_cookie = NULL;
8899 txn->cli_cookie = NULL;
8900 txn->uri = NULL;
8901
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008902 http_txn_reset_req(txn);
8903 http_txn_reset_res(txn);
8904
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008905 txn->req.chn = &s->req;
8906 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008907
8908 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008909
8910 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8911 if (fe->options2 & PR_O2_REQBUG_OK)
8912 txn->req.err_pos = -1; /* let buggy requests pass */
8913
Willy Tarreau0937bc42009-12-22 15:03:09 +01008914 if (txn->hdr_idx.v)
8915 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008916
8917 vars_init(&s->vars_txn, SCOPE_TXN);
8918 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008919}
8920
8921/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008922void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008923{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008924 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008925 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008926
Willy Tarreau75195602014-03-11 15:48:55 +01008927 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008928 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008929 s->comp_algo->end(&s->comp_ctx);
8930 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008931 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008932
Willy Tarreau0937bc42009-12-22 15:03:09 +01008933 /* these ones will have been dynamically allocated */
8934 pool_free2(pool2_requri, txn->uri);
8935 pool_free2(pool2_capture, txn->cli_cookie);
8936 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008937 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008938
William Lallemanda73203e2012-03-12 12:48:57 +01008939 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008940 txn->uri = NULL;
8941 txn->srv_cookie = NULL;
8942 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008943
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008944 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008945 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008946 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008947 pool_free2(h->pool, s->req_cap[h->index]);
8948 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008949 }
8950
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008951 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008952 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008953 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008954 pool_free2(h->pool, s->res_cap[h->index]);
8955 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008956 }
8957
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008958 vars_prune(&s->vars_txn, s);
8959 vars_prune(&s->vars_reqres, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008960}
8961
8962/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008963void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008964{
8965 http_end_txn(s);
8966 http_init_txn(s);
8967
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008968 /* reinitialise the current rule list pointer to NULL. We are sure that
8969 * any rulelist match the NULL pointer.
8970 */
8971 s->current_rule_list = NULL;
8972
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008973 s->be = strm_fe(s);
8974 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008975 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008976 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008977 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008978 /* re-init store persistence */
8979 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008980 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008981
Willy Tarreau0937bc42009-12-22 15:03:09 +01008982 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008983
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008984 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008985
Willy Tarreau739cfba2010-01-25 23:11:14 +01008986 /* We must trim any excess data from the response buffer, because we
8987 * may have blocked an invalid response from a server that we don't
8988 * want to accidentely forward once we disable the analysers, nor do
8989 * we want those data to come along with next response. A typical
8990 * example of such data would be from a buggy server responding to
8991 * a HEAD with some data, or sending more than the advertised
8992 * content-length.
8993 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008994 if (unlikely(s->res.buf->i))
8995 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008996
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008997 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008998 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008999
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009000 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009001 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009002
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009003 s->req.rex = TICK_ETERNITY;
9004 s->req.wex = TICK_ETERNITY;
9005 s->req.analyse_exp = TICK_ETERNITY;
9006 s->res.rex = TICK_ETERNITY;
9007 s->res.wex = TICK_ETERNITY;
9008 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009009}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009010
Sasha Pachev218f0642014-06-16 12:05:59 -06009011void free_http_res_rules(struct list *r)
9012{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009013 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06009014
9015 list_for_each_entry_safe(pr, tr, r, list) {
9016 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009017 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009018 free(pr);
9019 }
9020}
9021
9022void free_http_req_rules(struct list *r)
9023{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009024 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009025
9026 list_for_each_entry_safe(pr, tr, r, list) {
9027 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009028 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009029 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009030
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009031 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009032 free(pr);
9033 }
9034}
9035
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009036/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009037struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009038{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009039 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009040 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009041 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009042 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009043
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009044 rule = (struct act_rule*)calloc(1, sizeof(struct act_rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009045 if (!rule) {
9046 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009047 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009048 }
9049
CJ Ess108b1dd2015-04-07 12:03:37 -04009050 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009051 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009052 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009053 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009054 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009055 int code;
9056 int hc;
9057
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009058 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01009059 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009060 if (strcmp(args[cur_arg], "deny_status") == 0) {
9061 cur_arg++;
9062 if (!args[cur_arg]) {
9063 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9064 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9065 goto out_err;
9066 }
9067
9068 code = atol(args[cur_arg]);
9069 cur_arg++;
9070 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9071 if (http_err_codes[hc] == code) {
9072 rule->deny_status = hc;
9073 break;
9074 }
9075 }
9076
9077 if (hc >= HTTP_ERR_SIZE) {
9078 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9079 file, linenum, code);
9080 }
9081 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009082 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009083 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009084 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009085 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009086 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009087 cur_arg = 1;
9088
9089 while(*args[cur_arg]) {
9090 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009091 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009092 cur_arg+=2;
9093 continue;
9094 } else
9095 break;
9096 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009097 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009098 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009099 cur_arg = 1;
9100
9101 if (!*args[cur_arg] ||
9102 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9103 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9104 file, linenum, args[0]);
9105 goto out_err;
9106 }
9107 rule->arg.nice = atoi(args[cur_arg]);
9108 if (rule->arg.nice < -1024)
9109 rule->arg.nice = -1024;
9110 else if (rule->arg.nice > 1024)
9111 rule->arg.nice = 1024;
9112 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009113 } else if (!strcmp(args[0], "set-tos")) {
9114#ifdef IP_TOS
9115 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009116 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009117 cur_arg = 1;
9118
9119 if (!*args[cur_arg] ||
9120 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9121 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9122 file, linenum, args[0]);
9123 goto out_err;
9124 }
9125
9126 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9127 if (err && *err != '\0') {
9128 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9129 file, linenum, err, args[0]);
9130 goto out_err;
9131 }
9132 cur_arg++;
9133#else
9134 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9135 goto out_err;
9136#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009137 } else if (!strcmp(args[0], "set-mark")) {
9138#ifdef SO_MARK
9139 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009140 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009141 cur_arg = 1;
9142
9143 if (!*args[cur_arg] ||
9144 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9145 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9146 file, linenum, args[0]);
9147 goto out_err;
9148 }
9149
9150 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9151 if (err && *err != '\0') {
9152 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9153 file, linenum, err, args[0]);
9154 goto out_err;
9155 }
9156 cur_arg++;
9157 global.last_checks |= LSTCHK_NETADM;
9158#else
9159 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9160 goto out_err;
9161#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009162 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009163 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009164 cur_arg = 1;
9165
9166 if (!*args[cur_arg] ||
9167 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9168 bad_log_level:
9169 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9170 file, linenum, args[0]);
9171 goto out_err;
9172 }
9173 if (strcmp(args[cur_arg], "silent") == 0)
9174 rule->arg.loglevel = -1;
9175 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9176 goto bad_log_level;
9177 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009178 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009179 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009180 cur_arg = 1;
9181
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009182 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9183 (*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 +01009184 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9185 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009186 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009187 }
9188
9189 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9190 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9191 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009192
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009193 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009194 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009195 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9196 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009197 free(proxy->conf.lfs_file);
9198 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9199 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009200 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009201 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009202 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009203 cur_arg = 1;
9204
9205 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009206 (*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 -06009207 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9208 file, linenum, args[0]);
9209 goto out_err;
9210 }
9211
9212 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9213 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9214 LIST_INIT(&rule->arg.hdr_add.fmt);
9215
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009216 error = NULL;
9217 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9218 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9219 args[cur_arg + 1], error);
9220 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009221 goto out_err;
9222 }
9223
9224 proxy->conf.args.ctx = ARGC_HRQ;
9225 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9226 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9227 file, linenum);
9228
9229 free(proxy->conf.lfs_file);
9230 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9231 proxy->conf.lfs_line = proxy->conf.args.line;
9232 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009233 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009234 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009235 cur_arg = 1;
9236
9237 if (!*args[cur_arg] ||
9238 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9239 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9240 file, linenum, args[0]);
9241 goto out_err;
9242 }
9243
9244 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9245 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9246
9247 proxy->conf.args.ctx = ARGC_HRQ;
9248 free(proxy->conf.lfs_file);
9249 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9250 proxy->conf.lfs_line = proxy->conf.args.line;
9251 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009252 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9253 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009254 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009255 struct sample_expr *expr;
9256 unsigned int where;
9257 char *err = NULL;
9258
9259 cur_arg = 1;
9260 proxy->conf.args.ctx = ARGC_TRK;
9261
9262 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9263 if (!expr) {
9264 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9265 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9266 free(err);
9267 goto out_err;
9268 }
9269
9270 where = 0;
9271 if (proxy->cap & PR_CAP_FE)
9272 where |= SMP_VAL_FE_HRQ_HDR;
9273 if (proxy->cap & PR_CAP_BE)
9274 where |= SMP_VAL_BE_HRQ_HDR;
9275
9276 if (!(expr->fetch->val & where)) {
9277 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9278 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9279 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9280 args[cur_arg-1], sample_src_names(expr->fetch->use));
9281 free(expr);
9282 goto out_err;
9283 }
9284
9285 if (strcmp(args[cur_arg], "table") == 0) {
9286 cur_arg++;
9287 if (!args[cur_arg]) {
9288 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9289 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9290 free(expr);
9291 goto out_err;
9292 }
9293 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009294 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009295 cur_arg++;
9296 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009297 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009298 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009299 } else if (strcmp(args[0], "redirect") == 0) {
9300 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009301 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009302
Willy Tarreaube4653b2015-05-28 15:26:58 +02009303 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009304 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9305 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9306 goto out_err;
9307 }
9308
9309 /* this redirect rule might already contain a parsed condition which
9310 * we'll pass to the http-request rule.
9311 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009312 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009313 rule->arg.redir = redir;
9314 rule->cond = redir->cond;
9315 redir->cond = NULL;
9316 cur_arg = 2;
9317 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009318 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9319 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009320 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009321 /*
9322 * '+ 8' for 'add-acl('
9323 * '- 9' for 'add-acl(' + trailing ')'
9324 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009325 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009326
9327 cur_arg = 1;
9328
9329 if (!*args[cur_arg] ||
9330 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9331 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9332 file, linenum, args[0]);
9333 goto out_err;
9334 }
9335
9336 LIST_INIT(&rule->arg.map.key);
9337 proxy->conf.args.ctx = ARGC_HRQ;
9338 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9339 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9340 file, linenum);
9341 free(proxy->conf.lfs_file);
9342 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9343 proxy->conf.lfs_line = proxy->conf.args.line;
9344 cur_arg += 1;
9345 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9346 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009347 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009348 /*
9349 * '+ 8' for 'del-acl('
9350 * '- 9' for 'del-acl(' + trailing ')'
9351 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009352 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009353
9354 cur_arg = 1;
9355
9356 if (!*args[cur_arg] ||
9357 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9358 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9359 file, linenum, args[0]);
9360 goto out_err;
9361 }
9362
9363 LIST_INIT(&rule->arg.map.key);
9364 proxy->conf.args.ctx = ARGC_HRQ;
9365 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9366 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9367 file, linenum);
9368 free(proxy->conf.lfs_file);
9369 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9370 proxy->conf.lfs_line = proxy->conf.args.line;
9371 cur_arg += 1;
9372 } else if (strncmp(args[0], "del-map", 7) == 0) {
9373 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009374 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009375 /*
9376 * '+ 8' for 'del-map('
9377 * '- 9' for 'del-map(' + trailing ')'
9378 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009379 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009380
9381 cur_arg = 1;
9382
9383 if (!*args[cur_arg] ||
9384 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9385 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9386 file, linenum, args[0]);
9387 goto out_err;
9388 }
9389
9390 LIST_INIT(&rule->arg.map.key);
9391 proxy->conf.args.ctx = ARGC_HRQ;
9392 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9393 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9394 file, linenum);
9395 free(proxy->conf.lfs_file);
9396 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9397 proxy->conf.lfs_line = proxy->conf.args.line;
9398 cur_arg += 1;
9399 } else if (strncmp(args[0], "set-map", 7) == 0) {
9400 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009401 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009402 /*
9403 * '+ 8' for 'set-map('
9404 * '- 9' for 'set-map(' + trailing ')'
9405 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009406 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009407
9408 cur_arg = 1;
9409
9410 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9411 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9412 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9413 file, linenum, args[0]);
9414 goto out_err;
9415 }
9416
9417 LIST_INIT(&rule->arg.map.key);
9418 LIST_INIT(&rule->arg.map.value);
9419 proxy->conf.args.ctx = ARGC_HRQ;
9420
9421 /* key pattern */
9422 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9423 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9424 file, linenum);
9425
9426 /* value pattern */
9427 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9428 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9429 file, linenum);
9430 free(proxy->conf.lfs_file);
9431 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9432 proxy->conf.lfs_line = proxy->conf.args.line;
9433
9434 cur_arg += 2;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009435 } else if (strncmp(args[0], "set-src", 7) == 0) {
9436 struct sample_expr *expr;
9437 unsigned int where;
9438 char *err = NULL;
9439
9440 cur_arg = 1;
9441 proxy->conf.args.ctx = ARGC_HRQ;
9442
9443 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9444 if (!expr) {
9445 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9446 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9447 free(err);
9448 goto out_err;
9449 }
9450
9451 where = 0;
9452 if (proxy->cap & PR_CAP_FE)
9453 where |= SMP_VAL_FE_HRQ_HDR;
9454 if (proxy->cap & PR_CAP_BE)
9455 where |= SMP_VAL_BE_HRQ_HDR;
9456
9457 if (!(expr->fetch->val & where)) {
9458 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9459 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9460 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9461 args[cur_arg-1], sample_src_names(expr->fetch->use));
9462 free(expr);
9463 goto out_err;
9464 }
9465
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02009466 rule->arg.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009467 rule->action = ACT_HTTP_REQ_SET_SRC;
William Lallemand73025dd2014-04-24 14:38:37 +02009468 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9469 char *errmsg = NULL;
9470 cur_arg = 1;
9471 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009472 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009473 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009474 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009475 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9476 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9477 free(errmsg);
9478 goto out_err;
9479 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009480 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009481 action_build_list(&http_req_keywords.list, &trash);
9482 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9483 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9484 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', "
9485 "'set-src'%s%s, but got '%s'%s.\n",
9486 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009487 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009488 }
9489
9490 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9491 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009492 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009493
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009494 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9495 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9496 file, linenum, args[0], errmsg);
9497 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009498 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009499 }
9500 rule->cond = cond;
9501 }
9502 else if (*args[cur_arg]) {
9503 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9504 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9505 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009506 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009507 }
9508
9509 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009510 out_err:
9511 free(rule);
9512 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009513}
9514
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009515/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009516struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009517{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009518 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009519 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009520 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009521 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009522
9523 rule = calloc(1, sizeof(*rule));
9524 if (!rule) {
9525 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9526 goto out_err;
9527 }
9528
9529 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009530 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009531 cur_arg = 1;
9532 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009533 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009534 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009535 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009536 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009537 cur_arg = 1;
9538
9539 if (!*args[cur_arg] ||
9540 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9541 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9542 file, linenum, args[0]);
9543 goto out_err;
9544 }
9545 rule->arg.nice = atoi(args[cur_arg]);
9546 if (rule->arg.nice < -1024)
9547 rule->arg.nice = -1024;
9548 else if (rule->arg.nice > 1024)
9549 rule->arg.nice = 1024;
9550 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009551 } else if (!strcmp(args[0], "set-tos")) {
9552#ifdef IP_TOS
9553 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009554 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009555 cur_arg = 1;
9556
9557 if (!*args[cur_arg] ||
9558 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9559 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9560 file, linenum, args[0]);
9561 goto out_err;
9562 }
9563
9564 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9565 if (err && *err != '\0') {
9566 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9567 file, linenum, err, args[0]);
9568 goto out_err;
9569 }
9570 cur_arg++;
9571#else
9572 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9573 goto out_err;
9574#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009575 } else if (!strcmp(args[0], "set-mark")) {
9576#ifdef SO_MARK
9577 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009578 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009579 cur_arg = 1;
9580
9581 if (!*args[cur_arg] ||
9582 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9583 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9584 file, linenum, args[0]);
9585 goto out_err;
9586 }
9587
9588 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9589 if (err && *err != '\0') {
9590 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9591 file, linenum, err, args[0]);
9592 goto out_err;
9593 }
9594 cur_arg++;
9595 global.last_checks |= LSTCHK_NETADM;
9596#else
9597 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9598 goto out_err;
9599#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009600 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009601 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009602 cur_arg = 1;
9603
9604 if (!*args[cur_arg] ||
9605 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9606 bad_log_level:
9607 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9608 file, linenum, args[0]);
9609 goto out_err;
9610 }
9611 if (strcmp(args[cur_arg], "silent") == 0)
9612 rule->arg.loglevel = -1;
9613 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9614 goto bad_log_level;
9615 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009616 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009617 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009618 cur_arg = 1;
9619
9620 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9621 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9622 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9623 file, linenum, args[0]);
9624 goto out_err;
9625 }
9626
9627 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9628 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9629 LIST_INIT(&rule->arg.hdr_add.fmt);
9630
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009631 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009632 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009633 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9634 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009635 free(proxy->conf.lfs_file);
9636 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9637 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009638 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009639 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009640 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009641 cur_arg = 1;
9642
9643 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009644 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9645 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009646 file, linenum, args[0]);
9647 goto out_err;
9648 }
9649
9650 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9651 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9652 LIST_INIT(&rule->arg.hdr_add.fmt);
9653
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009654 error = NULL;
9655 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9656 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9657 args[cur_arg + 1], error);
9658 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009659 goto out_err;
9660 }
9661
9662 proxy->conf.args.ctx = ARGC_HRQ;
9663 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9664 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9665 file, linenum);
9666
9667 free(proxy->conf.lfs_file);
9668 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9669 proxy->conf.lfs_line = proxy->conf.args.line;
9670 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009671 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009672 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009673 cur_arg = 1;
9674
9675 if (!*args[cur_arg] ||
9676 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9677 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9678 file, linenum, args[0]);
9679 goto out_err;
9680 }
9681
9682 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9683 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9684
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009685 proxy->conf.args.ctx = ARGC_HRS;
9686 free(proxy->conf.lfs_file);
9687 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9688 proxy->conf.lfs_line = proxy->conf.args.line;
9689 cur_arg += 1;
9690 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9691 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009692 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009693 /*
9694 * '+ 8' for 'add-acl('
9695 * '- 9' for 'add-acl(' + trailing ')'
9696 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009697 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009698
9699 cur_arg = 1;
9700
9701 if (!*args[cur_arg] ||
9702 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9703 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9704 file, linenum, args[0]);
9705 goto out_err;
9706 }
9707
9708 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009709 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009710 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9711 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9712 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009713 free(proxy->conf.lfs_file);
9714 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9715 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009716
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009717 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009718 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9719 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009720 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009721 /*
9722 * '+ 8' for 'del-acl('
9723 * '- 9' for 'del-acl(' + trailing ')'
9724 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009725 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009726
9727 cur_arg = 1;
9728
9729 if (!*args[cur_arg] ||
9730 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9731 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9732 file, linenum, args[0]);
9733 goto out_err;
9734 }
9735
9736 LIST_INIT(&rule->arg.map.key);
9737 proxy->conf.args.ctx = ARGC_HRS;
9738 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9739 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9740 file, linenum);
9741 free(proxy->conf.lfs_file);
9742 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9743 proxy->conf.lfs_line = proxy->conf.args.line;
9744 cur_arg += 1;
9745 } else if (strncmp(args[0], "del-map", 7) == 0) {
9746 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009747 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009748 /*
9749 * '+ 8' for 'del-map('
9750 * '- 9' for 'del-map(' + trailing ')'
9751 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009752 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009753
9754 cur_arg = 1;
9755
9756 if (!*args[cur_arg] ||
9757 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9758 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9759 file, linenum, args[0]);
9760 goto out_err;
9761 }
9762
9763 LIST_INIT(&rule->arg.map.key);
9764 proxy->conf.args.ctx = ARGC_HRS;
9765 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9766 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9767 file, linenum);
9768 free(proxy->conf.lfs_file);
9769 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9770 proxy->conf.lfs_line = proxy->conf.args.line;
9771 cur_arg += 1;
9772 } else if (strncmp(args[0], "set-map", 7) == 0) {
9773 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009774 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009775 /*
9776 * '+ 8' for 'set-map('
9777 * '- 9' for 'set-map(' + trailing ')'
9778 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009779 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009780
9781 cur_arg = 1;
9782
9783 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9784 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9785 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9786 file, linenum, args[0]);
9787 goto out_err;
9788 }
9789
9790 LIST_INIT(&rule->arg.map.key);
9791 LIST_INIT(&rule->arg.map.value);
9792
9793 proxy->conf.args.ctx = ARGC_HRS;
9794
9795 /* key pattern */
9796 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9797 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9798 file, linenum);
9799
9800 /* value pattern */
9801 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9802 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9803 file, linenum);
9804
9805 free(proxy->conf.lfs_file);
9806 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9807 proxy->conf.lfs_line = proxy->conf.args.line;
9808
9809 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009810 } else if (strcmp(args[0], "redirect") == 0) {
9811 struct redirect_rule *redir;
9812 char *errmsg = NULL;
9813
9814 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9815 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9816 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9817 goto out_err;
9818 }
9819
9820 /* this redirect rule might already contain a parsed condition which
9821 * we'll pass to the http-request rule.
9822 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009823 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009824 rule->arg.redir = redir;
9825 rule->cond = redir->cond;
9826 redir->cond = NULL;
9827 cur_arg = 2;
9828 return rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009829 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9830 char *errmsg = NULL;
9831 cur_arg = 1;
9832 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009833 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009834 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009835 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009836 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9837 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9838 free(errmsg);
9839 goto out_err;
9840 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009841 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009842 action_build_list(&http_res_keywords.list, &trash);
9843 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9844 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9845 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', "
9846 "'set-src'%s%s, but got '%s'%s.\n",
9847 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009848 goto out_err;
9849 }
9850
9851 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9852 struct acl_cond *cond;
9853 char *errmsg = NULL;
9854
9855 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9856 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9857 file, linenum, args[0], errmsg);
9858 free(errmsg);
9859 goto out_err;
9860 }
9861 rule->cond = cond;
9862 }
9863 else if (*args[cur_arg]) {
9864 Alert("parsing [%s:%d]: 'http-response %s' expects"
9865 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9866 file, linenum, args[0], args[cur_arg]);
9867 goto out_err;
9868 }
9869
9870 return rule;
9871 out_err:
9872 free(rule);
9873 return NULL;
9874}
9875
Willy Tarreau4baae242012-12-27 12:00:31 +01009876/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009877 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009878 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9879 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009880 */
9881struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009882 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009883{
9884 struct redirect_rule *rule;
9885 int cur_arg;
9886 int type = REDIRECT_TYPE_NONE;
9887 int code = 302;
9888 const char *destination = NULL;
9889 const char *cookie = NULL;
9890 int cookie_set = 0;
9891 unsigned int flags = REDIRECT_FLAG_NONE;
9892 struct acl_cond *cond = NULL;
9893
9894 cur_arg = 0;
9895 while (*(args[cur_arg])) {
9896 if (strcmp(args[cur_arg], "location") == 0) {
9897 if (!*args[cur_arg + 1])
9898 goto missing_arg;
9899
9900 type = REDIRECT_TYPE_LOCATION;
9901 cur_arg++;
9902 destination = args[cur_arg];
9903 }
9904 else if (strcmp(args[cur_arg], "prefix") == 0) {
9905 if (!*args[cur_arg + 1])
9906 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009907 type = REDIRECT_TYPE_PREFIX;
9908 cur_arg++;
9909 destination = args[cur_arg];
9910 }
9911 else if (strcmp(args[cur_arg], "scheme") == 0) {
9912 if (!*args[cur_arg + 1])
9913 goto missing_arg;
9914
9915 type = REDIRECT_TYPE_SCHEME;
9916 cur_arg++;
9917 destination = args[cur_arg];
9918 }
9919 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9920 if (!*args[cur_arg + 1])
9921 goto missing_arg;
9922
9923 cur_arg++;
9924 cookie = args[cur_arg];
9925 cookie_set = 1;
9926 }
9927 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9928 if (!*args[cur_arg + 1])
9929 goto missing_arg;
9930
9931 cur_arg++;
9932 cookie = args[cur_arg];
9933 cookie_set = 0;
9934 }
9935 else if (strcmp(args[cur_arg], "code") == 0) {
9936 if (!*args[cur_arg + 1])
9937 goto missing_arg;
9938
9939 cur_arg++;
9940 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009941 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009942 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009943 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009944 args[cur_arg - 1], args[cur_arg]);
9945 return NULL;
9946 }
9947 }
9948 else if (!strcmp(args[cur_arg],"drop-query")) {
9949 flags |= REDIRECT_FLAG_DROP_QS;
9950 }
9951 else if (!strcmp(args[cur_arg],"append-slash")) {
9952 flags |= REDIRECT_FLAG_APPEND_SLASH;
9953 }
9954 else if (strcmp(args[cur_arg], "if") == 0 ||
9955 strcmp(args[cur_arg], "unless") == 0) {
9956 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9957 if (!cond) {
9958 memprintf(errmsg, "error in condition: %s", *errmsg);
9959 return NULL;
9960 }
9961 break;
9962 }
9963 else {
9964 memprintf(errmsg,
9965 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9966 args[cur_arg]);
9967 return NULL;
9968 }
9969 cur_arg++;
9970 }
9971
9972 if (type == REDIRECT_TYPE_NONE) {
9973 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9974 return NULL;
9975 }
9976
Willy Tarreaube4653b2015-05-28 15:26:58 +02009977 if (dir && type != REDIRECT_TYPE_LOCATION) {
9978 memprintf(errmsg, "response only supports redirect type 'location'");
9979 return NULL;
9980 }
9981
Willy Tarreau4baae242012-12-27 12:00:31 +01009982 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9983 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009984 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009985
9986 if (!use_fmt) {
9987 /* old-style static redirect rule */
9988 rule->rdr_str = strdup(destination);
9989 rule->rdr_len = strlen(destination);
9990 }
9991 else {
9992 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009993
9994 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9995 * if prefix == "/", we don't want to add anything, otherwise it
9996 * makes it hard for the user to configure a self-redirection.
9997 */
Godbachd9722032014-12-18 15:44:58 +08009998 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009999 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +010010000 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +020010001 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
10002 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +010010003 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010004 free(curproxy->conf.lfs_file);
10005 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10006 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010007 }
10008 }
10009
Willy Tarreau4baae242012-12-27 12:00:31 +010010010 if (cookie) {
10011 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10012 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10013 */
10014 rule->cookie_len = strlen(cookie);
10015 if (cookie_set) {
10016 rule->cookie_str = malloc(rule->cookie_len + 10);
10017 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10018 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10019 rule->cookie_len += 9;
10020 } else {
10021 rule->cookie_str = malloc(rule->cookie_len + 21);
10022 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10023 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10024 rule->cookie_len += 20;
10025 }
10026 }
10027 rule->type = type;
10028 rule->code = code;
10029 rule->flags = flags;
10030 LIST_INIT(&rule->list);
10031 return rule;
10032
10033 missing_arg:
10034 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10035 return NULL;
10036}
10037
Willy Tarreau8797c062007-05-07 00:55:35 +020010038/************************************************************************/
10039/* The code below is dedicated to ACL parsing and matching */
10040/************************************************************************/
10041
10042
Willy Tarreau14174bc2012-04-16 14:34:04 +020010043/* This function ensures that the prerequisites for an L7 fetch are ready,
10044 * which means that a request or response is ready. If some data is missing,
10045 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010046 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10047 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010048 *
10049 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010050 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10051 * decide whether or not an HTTP message is present ;
10052 * 0 if the requested data cannot be fetched or if it is certain that
10053 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010054 * 1 if an HTTP message is ready
10055 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010056int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010057 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010058{
Willy Tarreau192252e2015-04-04 01:47:55 +020010059 struct http_txn *txn;
10060 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010061
Willy Tarreaueee5b512015-04-03 23:46:31 +020010062 /* Note: this function may only be used from places where
10063 * http_init_txn() has already been done, and implies that <s>,
10064 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
10065 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010066 */
10067
Willy Tarreau192252e2015-04-04 01:47:55 +020010068 if (!s)
10069 return 0;
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010070 if (!s->txn) {
10071 if (unlikely(!http_alloc_txn(s)))
10072 return 0; /* not enough memory */
10073 http_init_txn(s);
10074 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010075 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010076 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010077
10078 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010079 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010080
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010081 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010082 /* If the buffer does not leave enough free space at the end,
10083 * we must first realign it.
10084 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010085 if (s->req.buf->p > s->req.buf->data &&
10086 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10087 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010088
Willy Tarreau14174bc2012-04-16 14:34:04 +020010089 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010090 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010091 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010092
10093 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010094 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010095 http_msg_analyzer(msg, &txn->hdr_idx);
10096
10097 /* Still no valid request ? */
10098 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010099 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010100 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010101 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010102 }
10103 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010104 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010105 return 0;
10106 }
10107
10108 /* OK we just got a valid HTTP request. We have some minor
10109 * preparation to perform so that further checks can rely
10110 * on HTTP tests.
10111 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010112
10113 /* If the request was parsed but was too large, we must absolutely
10114 * return an error so that it is not processed. At the moment this
10115 * cannot happen, but if the parsers are to change in the future,
10116 * we want this check to be maintained.
10117 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010118 if (unlikely(s->req.buf->i + s->req.buf->p >
10119 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010120 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010121 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010122 return 1;
10123 }
10124
Willy Tarreau9b28e032012-10-12 23:49:43 +020010125 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010126 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010127 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010128
Willy Tarreau506d0502013-07-06 13:29:24 +020010129 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10130 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010131 }
10132
Willy Tarreau506d0502013-07-06 13:29:24 +020010133 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010134 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010135 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010136
10137 /* otherwise everything's ready for the request */
10138 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010139 else {
10140 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010141 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10142 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010143 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010144 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010145 }
10146
10147 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010148 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010149 return 1;
10150}
Willy Tarreau8797c062007-05-07 00:55:35 +020010151
Willy Tarreau8797c062007-05-07 00:55:35 +020010152/* 1. Check on METHOD
10153 * We use the pre-parsed method if it is known, and store its number as an
10154 * integer. If it is unknown, we use the pointer and the length.
10155 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010156static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010157{
10158 int len, meth;
10159
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010160 len = strlen(text);
10161 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010162
10163 pattern->val.i = meth;
10164 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010165 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010166 pattern->len = len;
10167 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010168 else {
10169 pattern->ptr.str = NULL;
10170 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010171 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010172 return 1;
10173}
10174
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010175/* This function fetches the method of current HTTP request and stores
10176 * it in the global pattern struct as a chunk. There are two possibilities :
10177 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10178 * in <len> and <ptr> is NULL ;
10179 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10180 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010181 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010182 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010183static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010184smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010185{
10186 int meth;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010187 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010188
Willy Tarreau24e32d82012-04-23 23:55:44 +020010189 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010190
Willy Tarreau8797c062007-05-07 00:55:35 +020010191 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010192 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010193 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010194 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010195 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10196 /* ensure the indexes are not affected */
10197 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010198 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010199 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10200 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010201 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010202 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010203 return 1;
10204}
10205
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010206/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010207static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010208{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010209 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010210 struct pattern_list *lst;
10211 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010212
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010213 list_for_each_entry(lst, &expr->patterns, list) {
10214 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010215
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010216 /* well-known method */
10217 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010218 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010219 return pattern;
10220 else
10221 continue;
10222 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010223
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010224 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010225 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010226 continue;
10227
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010228 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010229 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10230 (!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 +010010231 return pattern;
10232 }
10233 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010234}
10235
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010236static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010237smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010238{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010239 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010240 char *ptr;
10241 int len;
10242
Willy Tarreauc0239e02012-04-16 14:42:55 +020010243 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010244
Willy Tarreau8797c062007-05-07 00:55:35 +020010245 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010246 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010247
10248 while ((len-- > 0) && (*ptr++ != '/'));
10249 if (len <= 0)
10250 return 0;
10251
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010252 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010253 smp->data.u.str.str = ptr;
10254 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010255
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010256 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010257 return 1;
10258}
10259
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010260static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010261smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010262{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010263 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010264 char *ptr;
10265 int len;
10266
Willy Tarreauc0239e02012-04-16 14:42:55 +020010267 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010268
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010269 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010270 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10271 return 0;
10272
Willy Tarreau8797c062007-05-07 00:55:35 +020010273 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010274 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010275
10276 while ((len-- > 0) && (*ptr++ != '/'));
10277 if (len <= 0)
10278 return 0;
10279
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010280 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010281 smp->data.u.str.str = ptr;
10282 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010283
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010284 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010285 return 1;
10286}
10287
10288/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010289static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010290smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010291{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010292 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010293 char *ptr;
10294 int len;
10295
Willy Tarreauc0239e02012-04-16 14:42:55 +020010296 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010297
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010298 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010299 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10300 return 0;
10301
Willy Tarreau8797c062007-05-07 00:55:35 +020010302 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010303 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010304
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010305 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010306 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010307 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010308 return 1;
10309}
10310
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010311/* returns the longest available part of the body. This requires that the body
10312 * has been waited for using http-buffer-request.
10313 */
10314static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010315smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010316{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010317 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010318 struct http_msg *msg;
10319 unsigned long len;
10320 unsigned long block1;
10321 char *body;
10322 struct chunk *temp;
10323
10324 CHECK_HTTP_MESSAGE_FIRST();
10325
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010326 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010327 msg = &txn->req;
10328 else
10329 msg = &txn->rsp;
10330
10331 len = http_body_bytes(msg);
10332 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10333
10334 block1 = len;
10335 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10336 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10337
10338 if (block1 == len) {
10339 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010340 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010341 smp->data.u.str.str = body;
10342 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010343 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10344 }
10345 else {
10346 /* buffer is wrapped, we need to defragment it */
10347 temp = get_trash_chunk();
10348 memcpy(temp->str, body, block1);
10349 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010350 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010351 smp->data.u.str.str = temp->str;
10352 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010353 smp->flags = SMP_F_VOL_TEST;
10354 }
10355 return 1;
10356}
10357
10358
10359/* returns the available length of the body. This requires that the body
10360 * has been waited for using http-buffer-request.
10361 */
10362static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010363smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010364{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010365 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010366 struct http_msg *msg;
10367
10368 CHECK_HTTP_MESSAGE_FIRST();
10369
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010370 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010371 msg = &txn->req;
10372 else
10373 msg = &txn->rsp;
10374
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010375 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010376 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010377
10378 smp->flags = SMP_F_VOL_TEST;
10379 return 1;
10380}
10381
10382
10383/* returns the advertised length of the body, or the advertised size of the
10384 * chunks available in the buffer. This requires that the body has been waited
10385 * for using http-buffer-request.
10386 */
10387static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010388smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010389{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010390 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010391 struct http_msg *msg;
10392
10393 CHECK_HTTP_MESSAGE_FIRST();
10394
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010395 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010396 msg = &txn->req;
10397 else
10398 msg = &txn->rsp;
10399
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010400 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010401 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010402
10403 smp->flags = SMP_F_VOL_TEST;
10404 return 1;
10405}
10406
10407
Willy Tarreau8797c062007-05-07 00:55:35 +020010408/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010409static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010410smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010411{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010412 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010413
Willy Tarreauc0239e02012-04-16 14:42:55 +020010414 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010415
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010416 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010417 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010418 smp->data.u.str.len = txn->req.sl.rq.u_l;
10419 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010420 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010421 return 1;
10422}
10423
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010424static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010425smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010426{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010427 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010428 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010429
Willy Tarreauc0239e02012-04-16 14:42:55 +020010430 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010431
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010432 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010433 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 +020010434 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010435 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010436
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010437 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010438 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010439 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010440 return 1;
10441}
10442
10443static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010444smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010445{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010446 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010447 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010448
Willy Tarreauc0239e02012-04-16 14:42:55 +020010449 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010450
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010451 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010452 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 +020010453 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10454 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010455
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010456 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010457 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010458 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010459 return 1;
10460}
10461
Willy Tarreau185b5c42012-04-26 15:11:51 +020010462/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10463 * Accepts an optional argument of type string containing the header field name,
10464 * and an optional argument of type signed or unsigned integer to request an
10465 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010466 * headers are considered from the first one. It does not stop on commas and
10467 * returns full lines instead (useful for User-Agent or Date for example).
10468 */
10469static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010470smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010471{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010472 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010473 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010474 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010475 int occ = 0;
10476 const char *name_str = NULL;
10477 int name_len = 0;
10478
10479 if (!ctx) {
10480 /* first call */
10481 ctx = &static_hdr_ctx;
10482 ctx->idx = 0;
10483 smp->ctx.a[0] = ctx;
10484 }
10485
10486 if (args) {
10487 if (args[0].type != ARGT_STR)
10488 return 0;
10489 name_str = args[0].data.str.str;
10490 name_len = args[0].data.str.len;
10491
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010492 if (args[1].type == ARGT_SINT)
10493 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010494 }
10495
10496 CHECK_HTTP_MESSAGE_FIRST();
10497
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010498 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010499 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 +020010500
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010501 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10502 /* search for header from the beginning */
10503 ctx->idx = 0;
10504
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010505 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010506 /* no explicit occurrence and single fetch => last header by default */
10507 occ = -1;
10508
10509 if (!occ)
10510 /* prepare to report multiple occurrences for ACL fetches */
10511 smp->flags |= SMP_F_NOT_LAST;
10512
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010513 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010514 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010515 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 +020010516 return 1;
10517
10518 smp->flags &= ~SMP_F_NOT_LAST;
10519 return 0;
10520}
10521
10522/* 6. Check on HTTP header count. The number of occurrences is returned.
10523 * Accepts exactly 1 argument of type string. It does not stop on commas and
10524 * returns full lines instead (useful for User-Agent or Date for example).
10525 */
10526static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010527smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010528{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010529 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010530 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010531 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010532 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010533 const char *name = NULL;
10534 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010535
Willy Tarreau601a4d12015-04-01 19:16:09 +020010536 if (args && args->type == ARGT_STR) {
10537 name = args->data.str.str;
10538 len = args->data.str.len;
10539 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010540
10541 CHECK_HTTP_MESSAGE_FIRST();
10542
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010543 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010544 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 +020010545
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010546 ctx.idx = 0;
10547 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010548 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010549 cnt++;
10550
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010551 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010552 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010553 smp->flags = SMP_F_VOL_HDR;
10554 return 1;
10555}
10556
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010557static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010558smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010559{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010560 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010561 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010562 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010563 struct chunk *temp;
10564 char del = ',';
10565
10566 if (args && args->type == ARGT_STR)
10567 del = *args[0].data.str.str;
10568
10569 CHECK_HTTP_MESSAGE_FIRST();
10570
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010571 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010572 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 +020010573
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010574 temp = get_trash_chunk();
10575
10576 ctx.idx = 0;
10577 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10578 if (temp->len)
10579 temp->str[temp->len++] = del;
10580 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10581 temp->len += ctx.del;
10582 }
10583
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010584 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010585 smp->data.u.str.str = temp->str;
10586 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010587 smp->flags = SMP_F_VOL_HDR;
10588 return 1;
10589}
10590
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010591/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10592 * Accepts an optional argument of type string containing the header field name,
10593 * and an optional argument of type signed or unsigned integer to request an
10594 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010595 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010596 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010597static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010598smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010599{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010600 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010601 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010602 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010603 int occ = 0;
10604 const char *name_str = NULL;
10605 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010606
Willy Tarreaua890d072013-04-02 12:01:06 +020010607 if (!ctx) {
10608 /* first call */
10609 ctx = &static_hdr_ctx;
10610 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010611 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010612 }
10613
Willy Tarreau185b5c42012-04-26 15:11:51 +020010614 if (args) {
10615 if (args[0].type != ARGT_STR)
10616 return 0;
10617 name_str = args[0].data.str.str;
10618 name_len = args[0].data.str.len;
10619
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010620 if (args[1].type == ARGT_SINT)
10621 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010622 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010623
Willy Tarreaue333ec92012-04-16 16:26:40 +020010624 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010625
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010626 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010627 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 +020010628
Willy Tarreau185b5c42012-04-26 15:11:51 +020010629 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010630 /* search for header from the beginning */
10631 ctx->idx = 0;
10632
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010633 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010634 /* no explicit occurrence and single fetch => last header by default */
10635 occ = -1;
10636
10637 if (!occ)
10638 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010639 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010640
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010641 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010642 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010643 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 +020010644 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010645
Willy Tarreau37406352012-04-23 16:16:37 +020010646 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010647 return 0;
10648}
10649
Willy Tarreauc11416f2007-06-17 16:58:38 +020010650/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010651 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010652 */
10653static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010654smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010655{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010656 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010657 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010658 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010659 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010660 const char *name = NULL;
10661 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010662
Willy Tarreau601a4d12015-04-01 19:16:09 +020010663 if (args && args->type == ARGT_STR) {
10664 name = args->data.str.str;
10665 len = args->data.str.len;
10666 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010667
Willy Tarreaue333ec92012-04-16 16:26:40 +020010668 CHECK_HTTP_MESSAGE_FIRST();
10669
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010670 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010671 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 +020010672
Willy Tarreau33a7e692007-06-10 19:45:56 +020010673 ctx.idx = 0;
10674 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010675 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010676 cnt++;
10677
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010678 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010679 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010680 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010681 return 1;
10682}
10683
Willy Tarreau185b5c42012-04-26 15:11:51 +020010684/* Fetch an HTTP header's integer value. The integer value is returned. It
10685 * takes a mandatory argument of type string and an optional one of type int
10686 * to designate a specific occurrence. It returns an unsigned integer, which
10687 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010688 */
10689static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010690smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010691{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010692 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010693
Willy Tarreauf853c462012-04-23 18:53:56 +020010694 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010695 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010696 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010697 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010698
Willy Tarreaud53e2422012-04-16 17:21:11 +020010699 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010700}
10701
Cyril Bonté69fa9922012-10-25 00:01:06 +020010702/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10703 * and an optional one of type int to designate a specific occurrence.
10704 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010705 */
10706static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010707smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010708{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010709 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010710
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010711 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010712 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010713 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010714 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010715 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010716 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010717 if (smp->data.u.str.len < temp->size - 1) {
10718 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10719 temp->str[smp->data.u.str.len] = '\0';
10720 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010721 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010722 break;
10723 }
10724 }
10725 }
10726
Willy Tarreaud53e2422012-04-16 17:21:11 +020010727 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010728 if (!(smp->flags & SMP_F_NOT_LAST))
10729 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010730 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010731 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010732}
10733
Willy Tarreau737b0c12007-06-10 21:28:46 +020010734/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10735 * the first '/' after the possible hostname, and ends before the possible '?'.
10736 */
10737static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010738smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010739{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010740 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010741 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010742
Willy Tarreauc0239e02012-04-16 14:42:55 +020010743 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010744
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010745 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010746 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010747 ptr = http_get_path(txn);
10748 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010749 return 0;
10750
10751 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010752 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010753 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010754
10755 while (ptr < end && *ptr != '?')
10756 ptr++;
10757
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010758 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010759 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010760 return 1;
10761}
10762
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010763/* This produces a concatenation of the first occurrence of the Host header
10764 * followed by the path component if it begins with a slash ('/'). This means
10765 * that '*' will not be added, resulting in exactly the first Host entry.
10766 * If no Host header is found, then the path is returned as-is. The returned
10767 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010768 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010769 */
10770static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010771smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010772{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010773 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010774 char *ptr, *end, *beg;
10775 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010776 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010777
10778 CHECK_HTTP_MESSAGE_FIRST();
10779
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010780 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010781 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010782 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010783 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010784
10785 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010786 temp = get_trash_chunk();
10787 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010788 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010789 smp->data.u.str.str = temp->str;
10790 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010791
10792 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010793 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010794 beg = http_get_path(txn);
10795 if (!beg)
10796 beg = end;
10797
10798 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10799
10800 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010801 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10802 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010803 }
10804
10805 smp->flags = SMP_F_VOL_1ST;
10806 return 1;
10807}
10808
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010809/* This produces a 32-bit hash of the concatenation of the first occurrence of
10810 * the Host header followed by the path component if it begins with a slash ('/').
10811 * This means that '*' will not be added, resulting in exactly the first Host
10812 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010813 * is hashed using the path hash followed by a full avalanche hash and provides a
10814 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010815 * high-traffic sites without having to store whole paths.
10816 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010817int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010818smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010819{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010820 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010821 struct hdr_ctx ctx;
10822 unsigned int hash = 0;
10823 char *ptr, *beg, *end;
10824 int len;
10825
10826 CHECK_HTTP_MESSAGE_FIRST();
10827
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010828 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010829 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010830 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010831 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10832 ptr = ctx.line + ctx.val;
10833 len = ctx.vlen;
10834 while (len--)
10835 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10836 }
10837
10838 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010839 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010840 beg = http_get_path(txn);
10841 if (!beg)
10842 beg = end;
10843
10844 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10845
10846 if (beg < ptr && *beg == '/') {
10847 while (beg < ptr)
10848 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10849 }
10850 hash = full_hash(hash);
10851
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010852 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010853 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010854 smp->flags = SMP_F_VOL_1ST;
10855 return 1;
10856}
10857
Willy Tarreau4a550602012-12-09 14:53:32 +010010858/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010859 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10860 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10861 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010862 * that in environments where IPv6 is insignificant, truncating the output to
10863 * 8 bytes would still work.
10864 */
10865static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010866smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010867{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010868 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010869 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010870
10871 if (!cli_conn)
10872 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010873
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010874 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010875 return 0;
10876
Willy Tarreau47ca5452012-12-23 20:22:19 +010010877 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010878 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010879 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010880
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010881 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010882 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010883 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010884 temp->len += 4;
10885 break;
10886 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010887 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010888 temp->len += 16;
10889 break;
10890 default:
10891 return 0;
10892 }
10893
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010894 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010895 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010896 return 1;
10897}
10898
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010899/* Extracts the query string, which comes after the question mark '?'. If no
10900 * question mark is found, nothing is returned. Otherwise it returns a sample
10901 * of type string carrying the whole query string.
10902 */
10903static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010904smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010905{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010906 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010907 char *ptr, *end;
10908
10909 CHECK_HTTP_MESSAGE_FIRST();
10910
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010911 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010912 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10913 end = ptr + txn->req.sl.rq.u_l;
10914
10915 /* look up the '?' */
10916 do {
10917 if (ptr == end)
10918 return 0;
10919 } while (*ptr++ != '?');
10920
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010921 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010922 smp->data.u.str.str = ptr;
10923 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010924 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10925 return 1;
10926}
10927
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010928static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010929smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010930{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010931 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10932 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10933 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010934
Willy Tarreau24e32d82012-04-23 23:55:44 +020010935 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010936
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010937 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010938 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010939 return 1;
10940}
10941
Willy Tarreau7f18e522010-10-22 20:04:13 +020010942/* return a valid test if the current request is the first one on the connection */
10943static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010944smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010945{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010946 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010947 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010948 return 1;
10949}
10950
Willy Tarreau34db1082012-04-19 17:16:54 +020010951/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010952static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010953smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010954{
10955
Willy Tarreau24e32d82012-04-23 23:55:44 +020010956 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010957 return 0;
10958
Willy Tarreauc0239e02012-04-16 14:42:55 +020010959 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010960
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010961 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010962 return 0;
10963
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010964 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010965 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010966 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010967 return 1;
10968}
Willy Tarreau8797c062007-05-07 00:55:35 +020010969
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010970/* Accepts exactly 1 argument of type userlist */
10971static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010972smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010973{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010974 if (!args || args->type != ARGT_USR)
10975 return 0;
10976
10977 CHECK_HTTP_MESSAGE_FIRST();
10978
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010979 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010980 return 0;
10981
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010982 /* if the user does not belong to the userlist or has a wrong password,
10983 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010984 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010985 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010986 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10987 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010988 return 0;
10989
10990 /* pat_match_auth() will need the user list */
10991 smp->ctx.a[0] = args->data.usr;
10992
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010993 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010994 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010995 smp->data.u.str.str = smp->strm->txn->auth.user;
10996 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010997
10998 return 1;
10999}
11000
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011001/* Try to find the next occurrence of a cookie name in a cookie header value.
11002 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11003 * the cookie value is returned into *value and *value_l, and the function
11004 * returns a pointer to the next pointer to search from if the value was found.
11005 * Otherwise if the cookie was not found, NULL is returned and neither value
11006 * nor value_l are touched. The input <hdr> string should first point to the
11007 * header's value, and the <hdr_end> pointer must point to the first character
11008 * not part of the value. <list> must be non-zero if value may represent a list
11009 * of values (cookie headers). This makes it faster to abort parsing when no
11010 * list is expected.
11011 */
David Carlier4686f792015-09-25 14:10:50 +010011012char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011013extract_cookie_value(char *hdr, const char *hdr_end,
11014 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011015 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011016{
11017 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11018 char *next;
11019
11020 /* we search at least a cookie name followed by an equal, and more
11021 * generally something like this :
11022 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11023 */
11024 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11025 /* Iterate through all cookies on this line */
11026
11027 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
11028 att_beg++;
11029
11030 /* find att_end : this is the first character after the last non
11031 * space before the equal. It may be equal to hdr_end.
11032 */
11033 equal = att_end = att_beg;
11034
11035 while (equal < hdr_end) {
11036 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11037 break;
11038 if (http_is_spht[(unsigned char)*equal++])
11039 continue;
11040 att_end = equal;
11041 }
11042
11043 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11044 * is between <att_beg> and <equal>, both may be identical.
11045 */
11046
11047 /* look for end of cookie if there is an equal sign */
11048 if (equal < hdr_end && *equal == '=') {
11049 /* look for the beginning of the value */
11050 val_beg = equal + 1;
11051 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
11052 val_beg++;
11053
11054 /* find the end of the value, respecting quotes */
11055 next = find_cookie_value_end(val_beg, hdr_end);
11056
11057 /* make val_end point to the first white space or delimitor after the value */
11058 val_end = next;
11059 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
11060 val_end--;
11061 } else {
11062 val_beg = val_end = next = equal;
11063 }
11064
11065 /* We have nothing to do with attributes beginning with '$'. However,
11066 * they will automatically be removed if a header before them is removed,
11067 * since they're supposed to be linked together.
11068 */
11069 if (*att_beg == '$')
11070 continue;
11071
11072 /* Ignore cookies with no equal sign */
11073 if (equal == next)
11074 continue;
11075
11076 /* Now we have the cookie name between att_beg and att_end, and
11077 * its value between val_beg and val_end.
11078 */
11079
11080 if (att_end - att_beg == cookie_name_l &&
11081 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11082 /* let's return this value and indicate where to go on from */
11083 *value = val_beg;
11084 *value_l = val_end - val_beg;
11085 return next + 1;
11086 }
11087
11088 /* Set-Cookie headers only have the name in the first attr=value part */
11089 if (!list)
11090 break;
11091 }
11092
11093 return NULL;
11094}
11095
William Lallemanda43ba4e2014-01-28 18:14:25 +010011096/* Fetch a captured HTTP request header. The index is the position of
11097 * the "capture" option in the configuration file
11098 */
11099static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011100smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011101{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011102 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011103 int idx;
11104
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011105 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011106 return 0;
11107
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011108 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011109
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011110 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 +010011111 return 0;
11112
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011113 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011114 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011115 smp->data.u.str.str = smp->strm->req_cap[idx];
11116 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011117
11118 return 1;
11119}
11120
11121/* Fetch a captured HTTP response header. The index is the position of
11122 * the "capture" option in the configuration file
11123 */
11124static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011125smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011126{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011127 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011128 int idx;
11129
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011130 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011131 return 0;
11132
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011133 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011134
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011135 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 +010011136 return 0;
11137
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011138 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011139 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011140 smp->data.u.str.str = smp->strm->res_cap[idx];
11141 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011142
11143 return 1;
11144}
11145
William Lallemand65ad6e12014-01-31 15:08:02 +010011146/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11147static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011148smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011149{
11150 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011151 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011152 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011153
Willy Tarreau15e91e12015-04-04 00:52:09 +020011154 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011155 return 0;
11156
William Lallemand96a77852014-02-05 00:30:02 +010011157 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011158
William Lallemand96a77852014-02-05 00:30:02 +010011159 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11160 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011161
William Lallemand96a77852014-02-05 00:30:02 +010011162 temp = get_trash_chunk();
11163 temp->str = txn->uri;
11164 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011165 smp->data.u.str = *temp;
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}
11172
11173/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11174static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011175smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011176{
11177 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011178 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011179 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011180
Willy Tarreau15e91e12015-04-04 00:52:09 +020011181 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011182 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011183
William Lallemand65ad6e12014-01-31 15:08:02 +010011184 ptr = txn->uri;
11185
11186 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11187 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011188
William Lallemand65ad6e12014-01-31 15:08:02 +010011189 if (!*ptr)
11190 return 0;
11191
11192 ptr++; /* skip the space */
11193
11194 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011195 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011196 if (!ptr)
11197 return 0;
11198 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11199 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011200
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011201 smp->data.u.str = *temp;
11202 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011203 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011204 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011205
11206 return 1;
11207}
11208
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011209/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11210 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11211 */
11212static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011213smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011214{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011215 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011216
Willy Tarreau15e91e12015-04-04 00:52:09 +020011217 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011218 return 0;
11219
11220 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011221 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011222 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011223 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011224
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011225 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011226 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011227 smp->flags = SMP_F_CONST;
11228 return 1;
11229
11230}
11231
11232/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11233 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11234 */
11235static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011236smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011237{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011238 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011239
Willy Tarreau15e91e12015-04-04 00:52:09 +020011240 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011241 return 0;
11242
11243 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011244 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011245 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011246 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011247
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011248 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011249 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011250 smp->flags = SMP_F_CONST;
11251 return 1;
11252
11253}
11254
William Lallemand65ad6e12014-01-31 15:08:02 +010011255
Willy Tarreaue333ec92012-04-16 16:26:40 +020011256/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011257 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011258 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011259 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011260 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011261 * Accepts exactly 1 argument of type string. If the input options indicate
11262 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011263 * The returned sample is of type CSTR. Can be used to parse cookies in other
11264 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011265 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011266int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011267{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011268 struct http_txn *txn;
11269 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011270 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011271 const struct http_msg *msg;
11272 const char *hdr_name;
11273 int hdr_name_len;
11274 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011275 int occ = 0;
11276 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011277
Willy Tarreau24e32d82012-04-23 23:55:44 +020011278 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011279 return 0;
11280
Willy Tarreaua890d072013-04-02 12:01:06 +020011281 if (!ctx) {
11282 /* first call */
11283 ctx = &static_hdr_ctx;
11284 ctx->idx = 0;
11285 smp->ctx.a[2] = ctx;
11286 }
11287
Willy Tarreaue333ec92012-04-16 16:26:40 +020011288 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011289
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011290 txn = smp->strm->txn;
11291 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011292
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011293 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011294 msg = &txn->req;
11295 hdr_name = "Cookie";
11296 hdr_name_len = 6;
11297 } else {
11298 msg = &txn->rsp;
11299 hdr_name = "Set-Cookie";
11300 hdr_name_len = 10;
11301 }
11302
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011303 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011304 /* no explicit occurrence and single fetch => last cookie by default */
11305 occ = -1;
11306
11307 /* OK so basically here, either we want only one value and it's the
11308 * last one, or we want to iterate over all of them and we fetch the
11309 * next one.
11310 */
11311
Willy Tarreau9b28e032012-10-12 23:49:43 +020011312 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011313 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011314 /* search for the header from the beginning, we must first initialize
11315 * the search parameters.
11316 */
Willy Tarreau37406352012-04-23 16:16:37 +020011317 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011318 ctx->idx = 0;
11319 }
11320
Willy Tarreau28376d62012-04-26 21:26:10 +020011321 smp->flags |= SMP_F_VOL_HDR;
11322
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011323 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011324 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11325 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011326 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11327 goto out;
11328
Willy Tarreau24e32d82012-04-23 23:55:44 +020011329 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011330 continue;
11331
Willy Tarreau37406352012-04-23 16:16:37 +020011332 smp->ctx.a[0] = ctx->line + ctx->val;
11333 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011334 }
11335
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011336 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011337 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011338 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011339 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011340 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011341 &smp->data.u.str.str,
11342 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011343 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011344 found = 1;
11345 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011346 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011347 smp->flags |= SMP_F_NOT_LAST;
11348 return 1;
11349 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011350 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011351 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011352 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011353 /* all cookie headers and values were scanned. If we're looking for the
11354 * last occurrence, we may return it now.
11355 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011356 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011357 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011358 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011359}
11360
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011361/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011362 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011363 * multiple cookies may be parsed on the same line. The returned sample is of
11364 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011365 */
11366static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011367smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011368{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011369 struct http_txn *txn;
11370 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011371 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011372 const struct http_msg *msg;
11373 const char *hdr_name;
11374 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011375 int cnt;
11376 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011377 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011378
Willy Tarreau24e32d82012-04-23 23:55:44 +020011379 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011380 return 0;
11381
Willy Tarreaue333ec92012-04-16 16:26:40 +020011382 CHECK_HTTP_MESSAGE_FIRST();
11383
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011384 txn = smp->strm->txn;
11385 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011386
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011387 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011388 msg = &txn->req;
11389 hdr_name = "Cookie";
11390 hdr_name_len = 6;
11391 } else {
11392 msg = &txn->rsp;
11393 hdr_name = "Set-Cookie";
11394 hdr_name_len = 10;
11395 }
11396
Willy Tarreau9b28e032012-10-12 23:49:43 +020011397 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011398 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011399 ctx.idx = 0;
11400 cnt = 0;
11401
11402 while (1) {
11403 /* Note: val_beg == NULL every time we need to fetch a new header */
11404 if (!val_beg) {
11405 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11406 break;
11407
Willy Tarreau24e32d82012-04-23 23:55:44 +020011408 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011409 continue;
11410
11411 val_beg = ctx.line + ctx.val;
11412 val_end = val_beg + ctx.vlen;
11413 }
11414
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011415 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011416 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011417 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011418 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011419 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011420 &smp->data.u.str.str,
11421 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011422 cnt++;
11423 }
11424 }
11425
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011426 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011427 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011428 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011429 return 1;
11430}
11431
Willy Tarreau51539362012-05-08 12:46:28 +020011432/* Fetch an cookie's integer value. The integer value is returned. It
11433 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11434 */
11435static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011436smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011437{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011438 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011439
11440 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011441 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011442 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011443 }
11444
11445 return ret;
11446}
11447
Willy Tarreau8797c062007-05-07 00:55:35 +020011448/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011449/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011450/************************************************************************/
11451
David Cournapeau16023ee2010-12-23 20:55:41 +090011452/*
11453 * Given a path string and its length, find the position of beginning of the
11454 * query string. Returns NULL if no query string is found in the path.
11455 *
11456 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11457 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011458 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011459 */
bedis4c75cca2012-10-05 08:38:24 +020011460static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011461{
11462 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011463
bedis4c75cca2012-10-05 08:38:24 +020011464 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011465 return p ? p + 1 : NULL;
11466}
11467
bedis4c75cca2012-10-05 08:38:24 +020011468static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011469{
bedis4c75cca2012-10-05 08:38:24 +020011470 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011471}
11472
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011473/* after increasing a pointer value, it can exceed the first buffer
11474 * size. This function transform the value of <ptr> according with
11475 * the expected position. <chunks> is an array of the one or two
11476 * avalaible chunks. The first value is the start of the first chunk,
11477 * the second value if the end+1 of the first chunks. The third value
11478 * is NULL or the start of the second chunk and the fourth value is
11479 * the end+1 of the second chunk. The function returns 1 if does a
11480 * wrap, else returns 0.
11481 */
11482static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11483{
11484 if (*ptr < chunks[1])
11485 return 0;
11486 if (!chunks[2])
11487 return 0;
11488 *ptr = chunks[2] + ( *ptr - chunks[1] );
11489 return 1;
11490}
11491
David Cournapeau16023ee2010-12-23 20:55:41 +090011492/*
11493 * Given a url parameter, find the starting position of the first occurence,
11494 * or NULL if the parameter is not found.
11495 *
11496 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11497 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011498 *
11499 * Warning:this function returns a pointer that can be point to the first chunk
11500 * or the second chunk. The caller must be check the position before using the
11501 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011502 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011503static const char *
11504find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011505 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011506 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011507{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011508 const char *pos, *last, *equal;
11509 const char **bufs = chunks;
11510 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011511
David Cournapeau16023ee2010-12-23 20:55:41 +090011512
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011513 pos = bufs[0];
11514 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011515 while (pos <= last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011516 /* Check the equal. */
11517 equal = pos + url_param_name_l;
11518 if (fix_pointer_if_wrap(chunks, &equal)) {
11519 if (equal >= chunks[3])
11520 return NULL;
11521 } else {
11522 if (equal >= chunks[1])
11523 return NULL;
11524 }
11525 if (*equal == '=') {
11526 if (pos + url_param_name_l > last) {
11527 /* process wrap case, we detect a wrap. In this case, the
11528 * comparison is performed in two parts.
11529 */
11530
11531 /* This is the end, we dont have any other chunk. */
11532 if (bufs != chunks || !bufs[2])
11533 return NULL;
11534
11535 /* Compute the length of each part of the comparison. */
11536 l1 = last - pos;
11537 l2 = url_param_name_l - l1;
11538
11539 /* The second buffer is too short to contain the compared string. */
11540 if (bufs[2] + l2 > bufs[3])
11541 return NULL;
11542
11543 if (memcmp(pos, url_param_name, l1) == 0 &&
11544 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11545 return pos;
11546
11547 /* Perform wrapping and jump the string who fail the comparison. */
11548 bufs += 2;
11549 pos = bufs[0] + l2;
11550 last = bufs[1];
11551
11552 } else {
11553 /* process a simple comparison. */
11554 if (memcmp(pos, url_param_name, url_param_name_l) == 0) {
11555 return pos; }
11556 pos += url_param_name_l + 1;
11557 if (fix_pointer_if_wrap(chunks, &pos))
11558 last = bufs[2];
11559 }
11560 }
11561
11562 while (1) {
11563 /* Look for the next delimiter. */
11564 while (pos <= last && !is_param_delimiter(*pos, delim))
11565 pos++;
11566 if (pos < last)
11567 break;
11568 /* process buffer wrapping. */
11569 if (bufs != chunks || !bufs[2])
11570 return NULL;
11571 bufs += 2;
11572 pos = bufs[0];
11573 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011574 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011575 pos++;
11576 }
11577 return NULL;
11578}
11579
11580/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011581 * Given a url parameter name and a query string, find the next value.
11582 * An empty url_param_name matches the first available parameter.
11583 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11584 * respectively provide a pointer to the value and its end.
11585 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011586 */
11587static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011588find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011589 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011590 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011591{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011592 const char *arg_start, *qs_end;
11593 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011594
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011595 arg_start = chunks[0];
11596 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011597 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011598 /* Looks for an argument name. */
11599 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011600 url_param_name, url_param_name_l,
11601 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011602 /* Check for wrapping. */
11603 if (arg_start > qs_end)
11604 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011605 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011606 if (!arg_start)
11607 return 0;
11608
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011609 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011610 while (1) {
11611 /* looks for the first argument. */
11612 value_start = memchr(arg_start, '=', qs_end - arg_start);
11613 if (!value_start) {
11614
11615 /* Check for wrapping. */
11616 if (arg_start >= chunks[0] &&
11617 arg_start <= chunks[1] &&
11618 chunks[2]) {
11619 arg_start = chunks[2];
11620 qs_end = chunks[3];
11621 continue;
11622 }
11623 return 0;
11624 }
11625 break;
11626 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011627 value_start++;
11628 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011629 else {
11630 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011631 value_start = arg_start + url_param_name_l + 1;
11632
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011633 /* Check for pointer wrapping. */
11634 if (fix_pointer_if_wrap(chunks, &value_start)) {
11635 /* Update the end pointer. */
11636 qs_end = chunks[3];
11637
11638 /* Check for overflow. */
11639 if (value_start > qs_end)
11640 return 0;
11641 }
11642 }
11643
David Cournapeau16023ee2010-12-23 20:55:41 +090011644 value_end = value_start;
11645
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011646 while (1) {
11647 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11648 value_end++;
11649 if (value_end < qs_end)
11650 break;
11651 /* process buffer wrapping. */
11652 if (value_end >= chunks[0] &&
11653 value_end <= chunks[1] &&
11654 chunks[2]) {
11655 value_end = chunks[2];
11656 qs_end = chunks[3];
11657 continue;
11658 }
11659 break;
11660 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011661
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011662 *vstart = value_start;
11663 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011664 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011665}
11666
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011667/* This scans a URL-encoded query string. It takes an optionally wrapping
11668 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11669 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11670 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011671 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011672static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011673smp_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 +090011674{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011675 const char *vstart, *vend;
11676 struct chunk *temp;
11677 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011678
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011679 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011680 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011681 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011682 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011683 return 0;
11684
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011685 /* Create sample. If the value is contiguous, return the pointer as CONST,
11686 * if the value is wrapped, copy-it in a buffer.
11687 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011688 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011689 if (chunks[2] &&
11690 vstart >= chunks[0] && vstart <= chunks[1] &&
11691 vend >= chunks[2] && vend <= chunks[3]) {
11692 /* Wrapped case. */
11693 temp = get_trash_chunk();
11694 memcpy(temp->str, vstart, chunks[1] - vstart);
11695 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011696 smp->data.u.str.str = temp->str;
11697 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011698 } else {
11699 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011700 smp->data.u.str.str = (char *)vstart;
11701 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011702 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11703 }
11704
11705 /* Update context, check wrapping. */
11706 chunks[0] = vend;
11707 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11708 chunks[1] = chunks[3];
11709 chunks[2] = NULL;
11710 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011711
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011712 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011713 smp->flags |= SMP_F_NOT_LAST;
11714
David Cournapeau16023ee2010-12-23 20:55:41 +090011715 return 1;
11716}
11717
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011718/* This function iterates over each parameter of the query string. It uses
11719 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011720 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11721 * An optional parameter name is passed in args[0], otherwise any parameter is
11722 * considered. It supports an optional delimiter argument for the beginning of
11723 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011724 */
11725static int
11726smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11727{
11728 struct http_msg *msg;
11729 char delim = '?';
11730 const char *name;
11731 int name_len;
11732
Dragan Dosen26f77e52015-05-25 10:02:11 +020011733 if (!args ||
11734 (args[0].type && args[0].type != ARGT_STR) ||
11735 (args[1].type && args[1].type != ARGT_STR))
11736 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011737
Dragan Dosen26f77e52015-05-25 10:02:11 +020011738 name = "";
11739 name_len = 0;
11740 if (args->type == ARGT_STR) {
11741 name = args->data.str.str;
11742 name_len = args->data.str.len;
11743 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011744
Dragan Dosen26f77e52015-05-25 10:02:11 +020011745 if (args[1].type)
11746 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011747
Dragan Dosen26f77e52015-05-25 10:02:11 +020011748 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011749 CHECK_HTTP_MESSAGE_FIRST();
11750
11751 msg = &smp->strm->txn->req;
11752
11753 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11754 msg->sl.rq.u_l, delim);
11755 if (!smp->ctx.a[0])
11756 return 0;
11757
11758 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011759
11760 /* Assume that the context is filled with NULL pointer
11761 * before the first call.
11762 * smp->ctx.a[2] = NULL;
11763 * smp->ctx.a[3] = NULL;
11764 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011765 }
11766
11767 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11768}
11769
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011770/* This function iterates over each parameter of the body. This requires
11771 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011772 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11773 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11774 * optional second part if the body wraps at the end of the buffer. An optional
11775 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011776 */
11777static int
11778smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11779{
11780 struct http_txn *txn = smp->strm->txn;
11781 struct http_msg *msg;
11782 unsigned long len;
11783 unsigned long block1;
11784 char *body;
11785 const char *name;
11786 int name_len;
11787
11788 if (!args || (args[0].type && args[0].type != ARGT_STR))
11789 return 0;
11790
11791 name = "";
11792 name_len = 0;
11793 if (args[0].type == ARGT_STR) {
11794 name = args[0].data.str.str;
11795 name_len = args[0].data.str.len;
11796 }
11797
11798 if (!smp->ctx.a[0]) { // first call, find the query string
11799 CHECK_HTTP_MESSAGE_FIRST();
11800
11801 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
11802 msg = &txn->req;
11803 else
11804 msg = &txn->rsp;
11805
11806 len = http_body_bytes(msg);
11807 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11808
11809 block1 = len;
11810 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11811 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11812
11813 if (block1 == len) {
11814 /* buffer is not wrapped (or empty) */
11815 smp->ctx.a[0] = body;
11816 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011817
11818 /* Assume that the context is filled with NULL pointer
11819 * before the first call.
11820 * smp->ctx.a[2] = NULL;
11821 * smp->ctx.a[3] = NULL;
11822 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011823 }
11824 else {
11825 /* buffer is wrapped, we need to defragment it */
11826 smp->ctx.a[0] = body;
11827 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011828 smp->ctx.a[2] = msg->chn->buf->data;
11829 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011830 }
11831 }
11832 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11833}
11834
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011835/* Return the signed integer value for the specified url parameter (see url_param
11836 * above).
11837 */
11838static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011839smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011840{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011841 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011842
11843 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011844 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011845 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011846 }
11847
11848 return ret;
11849}
11850
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011851/* This produces a 32-bit hash of the concatenation of the first occurrence of
11852 * the Host header followed by the path component if it begins with a slash ('/').
11853 * This means that '*' will not be added, resulting in exactly the first Host
11854 * entry. If no Host header is found, then the path is used. The resulting value
11855 * is hashed using the url hash followed by a full avalanche hash and provides a
11856 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11857 * high-traffic sites without having to store whole paths.
11858 * this differs from the base32 functions in that it includes the url parameters
11859 * as well as the path
11860 */
11861static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011862smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011863{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011864 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011865 struct hdr_ctx ctx;
11866 unsigned int hash = 0;
11867 char *ptr, *beg, *end;
11868 int len;
11869
11870 CHECK_HTTP_MESSAGE_FIRST();
11871
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011872 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011873 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011874 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011875 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11876 ptr = ctx.line + ctx.val;
11877 len = ctx.vlen;
11878 while (len--)
11879 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11880 }
11881
11882 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011883 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 +000011884 beg = http_get_path(txn);
11885 if (!beg)
11886 beg = end;
11887
11888 for (ptr = beg; ptr < end ; ptr++);
11889
11890 if (beg < ptr && *beg == '/') {
11891 while (beg < ptr)
11892 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11893 }
11894 hash = full_hash(hash);
11895
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011896 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011897 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011898 smp->flags = SMP_F_VOL_1ST;
11899 return 1;
11900}
11901
11902/* This concatenates the source address with the 32-bit hash of the Host and
11903 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11904 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11905 * on the source address length. The URL hash is stored before the address so
11906 * that in environments where IPv6 is insignificant, truncating the output to
11907 * 8 bytes would still work.
11908 */
11909static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011910smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011911{
11912 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011913 struct connection *cli_conn = objt_conn(smp->sess->origin);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011914 unsigned int hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011915
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011916 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011917 return 0;
11918
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011919 /* The returned hash is a 32 bytes integer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011920 hash = smp->data.u.sint;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011921
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011922 temp = get_trash_chunk();
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011923 memcpy(temp->str + temp->len, &hash, sizeof(hash));
11924 temp->len += sizeof(hash);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011925
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011926 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011927 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011928 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011929 temp->len += 4;
11930 break;
11931 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011932 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011933 temp->len += 16;
11934 break;
11935 default:
11936 return 0;
11937 }
11938
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011939 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011940 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011941 return 1;
11942}
11943
Willy Tarreau185b5c42012-04-26 15:11:51 +020011944/* This function is used to validate the arguments passed to any "hdr" fetch
11945 * keyword. These keywords support an optional positive or negative occurrence
11946 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11947 * is assumed that the types are already the correct ones. Returns 0 on error,
11948 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11949 * error message in case of error, that the caller is responsible for freeing.
11950 * The initial location must either be freeable or NULL.
11951 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011952int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011953{
11954 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011955 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011956 return 0;
11957 }
11958 return 1;
11959}
11960
Willy Tarreau276fae92013-07-25 14:36:01 +020011961/* takes an UINT value on input supposed to represent the time since EPOCH,
11962 * adds an optional offset found in args[0] and emits a string representing
11963 * the date in RFC-1123/5322 format.
11964 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011965static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011966{
11967 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11968 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11969 struct chunk *temp;
11970 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011971 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011972 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011973
11974 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011975 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011976 curr_date += args[0].data.sint;
11977
11978 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011979 if (!tm)
11980 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011981
11982 temp = get_trash_chunk();
11983 temp->len = snprintf(temp->str, temp->size - temp->len,
11984 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11985 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11986 tm->tm_hour, tm->tm_min, tm->tm_sec);
11987
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011988 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011989 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011990 return 1;
11991}
11992
Thierry FOURNIERad903512014-04-11 17:51:01 +020011993/* Match language range with language tag. RFC2616 14.4:
11994 *
11995 * A language-range matches a language-tag if it exactly equals
11996 * the tag, or if it exactly equals a prefix of the tag such
11997 * that the first tag character following the prefix is "-".
11998 *
11999 * Return 1 if the strings match, else return 0.
12000 */
12001static inline int language_range_match(const char *range, int range_len,
12002 const char *tag, int tag_len)
12003{
12004 const char *end = range + range_len;
12005 const char *tend = tag + tag_len;
12006 while (range < end) {
12007 if (*range == '-' && tag == tend)
12008 return 1;
12009 if (*range != *tag || tag == tend)
12010 return 0;
12011 range++;
12012 tag++;
12013 }
12014 /* Return true only if the last char of the tag is matched. */
12015 return tag == tend;
12016}
12017
12018/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012019static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012020{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012021 const char *al = smp->data.u.str.str;
12022 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012023 const char *token;
12024 int toklen;
12025 int qvalue;
12026 const char *str;
12027 const char *w;
12028 int best_q = 0;
12029
12030 /* Set the constant to the sample, because the output of the
12031 * function will be peek in the constant configuration string.
12032 */
12033 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012034 smp->data.u.str.size = 0;
12035 smp->data.u.str.str = "";
12036 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012037
12038 /* Parse the accept language */
12039 while (1) {
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 break;
12046
12047 /* Start of the fisrt word. */
12048 token = al;
12049
12050 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012051 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012052 al++;
12053 if (al == token)
12054 goto expect_comma;
12055
12056 /* Length of the token. */
12057 toklen = al - token;
12058 qvalue = 1000;
12059
12060 /* Check if the token exists in the list. If the token not exists,
12061 * jump to the next token.
12062 */
12063 str = args[0].data.str.str;
12064 w = str;
12065 while (1) {
12066 if (*str == ';' || *str == '\0') {
12067 if (language_range_match(token, toklen, w, str-w))
12068 goto look_for_q;
12069 if (*str == '\0')
12070 goto expect_comma;
12071 w = str + 1;
12072 }
12073 str++;
12074 }
12075 goto expect_comma;
12076
12077look_for_q:
12078
12079 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012080 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012081 al++;
12082 if (al >= end)
12083 goto process_value;
12084
12085 /* If ',' is found, process the result */
12086 if (*al == ',')
12087 goto process_value;
12088
12089 /* If the character is different from ';', look
12090 * for the end of the header part in best effort.
12091 */
12092 if (*al != ';')
12093 goto expect_comma;
12094
12095 /* Assumes that the char is ';', now expect "q=". */
12096 al++;
12097
12098 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012099 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012100 al++;
12101 if (al >= end)
12102 goto process_value;
12103
12104 /* Expect 'q'. If no 'q', continue in best effort */
12105 if (*al != 'q')
12106 goto process_value;
12107 al++;
12108
12109 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012110 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012111 al++;
12112 if (al >= end)
12113 goto process_value;
12114
12115 /* Expect '='. If no '=', continue in best effort */
12116 if (*al != '=')
12117 goto process_value;
12118 al++;
12119
12120 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012121 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012122 al++;
12123 if (al >= end)
12124 goto process_value;
12125
12126 /* Parse the q value. */
12127 qvalue = parse_qvalue(al, &al);
12128
12129process_value:
12130
12131 /* If the new q value is the best q value, then store the associated
12132 * language in the response. If qvalue is the biggest value (1000),
12133 * break the process.
12134 */
12135 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012136 smp->data.u.str.str = (char *)w;
12137 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012138 if (qvalue >= 1000)
12139 break;
12140 best_q = qvalue;
12141 }
12142
12143expect_comma:
12144
12145 /* Expect comma or end. If the end is detected, quit the loop. */
12146 while (al < end && *al != ',')
12147 al++;
12148 if (al >= end)
12149 break;
12150
12151 /* Comma is found, jump it and restart the analyzer. */
12152 al++;
12153 }
12154
12155 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012156 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12157 smp->data.u.str.str = args[1].data.str.str;
12158 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012159 }
12160
12161 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012162 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012163}
12164
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012165/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012166static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012167{
12168 /* If the constant flag is set or if not size is avalaible at
12169 * the end of the buffer, copy the string in other buffer
12170 * before decoding.
12171 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012172 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012173 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012174 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12175 smp->data.u.str.str = str->str;
12176 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012177 smp->flags &= ~SMP_F_CONST;
12178 }
12179
12180 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012181 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12182 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012183 return 1;
12184}
12185
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012186static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12187{
12188 struct proxy *fe = strm_fe(smp->strm);
12189 int idx, i;
12190 struct cap_hdr *hdr;
12191 int len;
12192
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012193 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012194 return 0;
12195
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012196 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012197
12198 /* Check the availibity of the capture id. */
12199 if (idx > fe->nb_req_cap - 1)
12200 return 0;
12201
12202 /* Look for the original configuration. */
12203 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12204 hdr != NULL && i != idx ;
12205 i--, hdr = hdr->next);
12206 if (!hdr)
12207 return 0;
12208
12209 /* check for the memory allocation */
12210 if (smp->strm->req_cap[hdr->index] == NULL)
12211 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12212 if (smp->strm->req_cap[hdr->index] == NULL)
12213 return 0;
12214
12215 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012216 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012217 if (len > hdr->len)
12218 len = hdr->len;
12219
12220 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012221 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012222 smp->strm->req_cap[idx][len] = '\0';
12223
12224 return 1;
12225}
12226
12227static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12228{
12229 struct proxy *fe = strm_fe(smp->strm);
12230 int idx, i;
12231 struct cap_hdr *hdr;
12232 int len;
12233
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012234 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012235 return 0;
12236
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012237 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012238
12239 /* Check the availibity of the capture id. */
12240 if (idx > fe->nb_rsp_cap - 1)
12241 return 0;
12242
12243 /* Look for the original configuration. */
12244 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12245 hdr != NULL && i != idx ;
12246 i--, hdr = hdr->next);
12247 if (!hdr)
12248 return 0;
12249
12250 /* check for the memory allocation */
12251 if (smp->strm->res_cap[hdr->index] == NULL)
12252 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12253 if (smp->strm->res_cap[hdr->index] == NULL)
12254 return 0;
12255
12256 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012257 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012258 if (len > hdr->len)
12259 len = hdr->len;
12260
12261 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012262 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012263 smp->strm->res_cap[idx][len] = '\0';
12264
12265 return 1;
12266}
12267
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012268/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012269 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012270 * the relevant part of the request line accordingly. Then it updates various
12271 * pointers to the next elements which were moved, and the total buffer length.
12272 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012273 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12274 * error, though this can be revisited when this code is finally exploited.
12275 *
12276 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12277 * query string and 3 to replace uri.
12278 *
12279 * In query string case, the mark question '?' must be set at the start of the
12280 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012281 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012282int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012283 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012284{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012285 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012286 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012287 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012288 int delta;
12289
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012290 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012291 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012292 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012293 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12294
12295 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012296 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012297 txn->req.sl.rq.m_l += delta;
12298 txn->req.sl.rq.u += delta;
12299 txn->req.sl.rq.v += delta;
12300 break;
12301
12302 case 1: // path
12303 cur_ptr = http_get_path(txn);
12304 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012305 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012306
12307 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012308 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 +010012309 cur_end++;
12310
12311 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012312 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012313 txn->req.sl.rq.u_l += delta;
12314 txn->req.sl.rq.v += delta;
12315 break;
12316
12317 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012318 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012319 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012320 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12321 while (cur_ptr < cur_end && *cur_ptr != '?')
12322 cur_ptr++;
12323
12324 /* skip the question mark or indicate that we must insert it
12325 * (but only if the format string is not empty then).
12326 */
12327 if (cur_ptr < cur_end)
12328 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012329 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012330 offset = 0;
12331
12332 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012333 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012334 txn->req.sl.rq.u_l += delta;
12335 txn->req.sl.rq.v += delta;
12336 break;
12337
12338 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012339 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012340 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12341
12342 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012343 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012344 txn->req.sl.rq.u_l += delta;
12345 txn->req.sl.rq.v += delta;
12346 break;
12347
12348 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012349 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012350 }
12351
12352 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012353 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012354 txn->req.sl.rq.l += delta;
12355 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012356 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012357 return 0;
12358}
12359
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012360/* This function replace the HTTP status code and the associated message. The
12361 * variable <status> contains the new status code. This function never fails.
12362 */
12363void http_set_status(unsigned int status, struct stream *s)
12364{
12365 struct http_txn *txn = s->txn;
12366 char *cur_ptr, *cur_end;
12367 int delta;
12368 char *res;
12369 int c_l;
12370 const char *msg;
12371 int msg_len;
12372
12373 chunk_reset(&trash);
12374
12375 res = ultoa_o(status, trash.str, trash.size);
12376 c_l = res - trash.str;
12377
12378 trash.str[c_l] = ' ';
12379 trash.len = c_l + 1;
12380
12381 msg = get_reason(status);
12382 msg_len = strlen(msg);
12383
12384 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12385 trash.len += msg_len;
12386
12387 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12388 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12389
12390 /* commit changes and adjust message */
12391 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12392
12393 /* adjust res line offsets and lengths */
12394 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12395 txn->rsp.sl.st.c_l = c_l;
12396 txn->rsp.sl.st.r_l = msg_len;
12397
12398 delta = trash.len - (cur_end - cur_ptr);
12399 txn->rsp.sl.st.l += delta;
12400 txn->hdr_idx.v[0].len += delta;
12401 http_msg_move_end(&txn->rsp, delta);
12402}
12403
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012404/* This function executes one of the set-{method,path,query,uri} actions. It
12405 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012406 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012407 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012408 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12409 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012410 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012411enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012412 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012413{
12414 chunk_reset(&trash);
12415
12416 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012417 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012418 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012419 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012420
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012421 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012422 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012423}
12424
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012425/* This function is just a compliant action wrapper for "set-status". */
12426enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012427 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012428{
12429 http_set_status(rule->arg.status.code, s);
12430 return ACT_RET_CONT;
12431}
12432
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012433/* parse an http-request action among :
12434 * set-method
12435 * set-path
12436 * set-query
12437 * set-uri
12438 *
12439 * All of them accept a single argument of type string representing a log-format.
12440 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12441 * 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 +020012442 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012443 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012444enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12445 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012446{
12447 int cur_arg = *orig_arg;
12448
Thierry FOURNIER42148732015-09-02 17:17:33 +020012449 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012450
12451 switch (args[0][4]) {
12452 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012453 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012454 rule->action_ptr = http_action_set_req_line;
12455 break;
12456 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012457 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012458 rule->action_ptr = http_action_set_req_line;
12459 break;
12460 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012461 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012462 rule->action_ptr = http_action_set_req_line;
12463 break;
12464 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012465 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012466 rule->action_ptr = http_action_set_req_line;
12467 break;
12468 default:
12469 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012470 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012471 }
12472
12473 if (!*args[cur_arg] ||
12474 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12475 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012476 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012477 }
12478
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012479 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012480 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012481 parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012482 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12483 proxy->conf.args.file, proxy->conf.args.line);
12484
12485 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012486 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012487}
12488
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012489/* parse set-status action:
12490 * This action accepts a single argument of type int representing
12491 * an http status code. It returns ACT_RET_PRS_OK on success,
12492 * ACT_RET_PRS_ERR on error.
12493 */
12494enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12495 struct act_rule *rule, char **err)
12496{
12497 char *error;
12498
Thierry FOURNIER42148732015-09-02 17:17:33 +020012499 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012500 rule->action_ptr = action_http_set_status;
12501
12502 /* Check if an argument is available */
12503 if (!*args[*orig_arg]) {
12504 memprintf(err, "expects exactly 1 argument <status>");
12505 return ACT_RET_PRS_ERR;
12506 }
12507
12508 /* convert status code as integer */
12509 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12510 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12511 memprintf(err, "expects an integer status code between 100 and 999");
12512 return ACT_RET_PRS_ERR;
12513 }
12514
12515 (*orig_arg)++;
12516 return ACT_RET_PRS_OK;
12517}
12518
Willy Tarreaua9083d02015-05-08 15:27:59 +020012519/* This function executes the "capture" action. It executes a fetch expression,
12520 * turns the result into a string and puts it in a capture slot. It always
12521 * returns 1. If an error occurs the action is cancelled, but the rule
12522 * processing continues.
12523 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012524enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012525 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012526{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012527 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012528 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012529 char **cap = s->req_cap;
12530 int len;
12531
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012532 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 +020012533 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012534 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012535
12536 if (cap[h->index] == NULL)
12537 cap[h->index] = pool_alloc2(h->pool);
12538
12539 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012540 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012541
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012542 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012543 if (len > h->len)
12544 len = h->len;
12545
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012546 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012547 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012548 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012549}
12550
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012551/* This function executes the "capture" action and store the result in a
12552 * capture slot if exists. It executes a fetch expression, turns the result
12553 * into a string and puts it in a capture slot. It always returns 1. If an
12554 * error occurs the action is cancelled, but the rule processing continues.
12555 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012556enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012557 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012558{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012559 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012560 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012561 char **cap = s->req_cap;
12562 struct proxy *fe = strm_fe(s);
12563 int len;
12564 int i;
12565
12566 /* Look for the original configuration. */
12567 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012568 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012569 i--, h = h->next);
12570 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012571 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012572
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012573 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 +020012574 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012575 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012576
12577 if (cap[h->index] == NULL)
12578 cap[h->index] = pool_alloc2(h->pool);
12579
12580 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012581 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012582
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012583 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012584 if (len > h->len)
12585 len = h->len;
12586
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012587 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012588 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012589 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012590}
12591
Willy Tarreaua9083d02015-05-08 15:27:59 +020012592/* parse an "http-request capture" action. It takes a single argument which is
12593 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012594 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012595 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012596 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012597enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12598 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012599{
12600 struct sample_expr *expr;
12601 struct cap_hdr *hdr;
12602 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012603 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012604
12605 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12606 if (strcmp(args[cur_arg], "if") == 0 ||
12607 strcmp(args[cur_arg], "unless") == 0)
12608 break;
12609
12610 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012611 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012612 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012613 }
12614
Willy Tarreaua9083d02015-05-08 15:27:59 +020012615 cur_arg = *orig_arg;
12616 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12617 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012618 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012619
12620 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12621 memprintf(err,
12622 "fetch method '%s' extracts information from '%s', none of which is available here",
12623 args[cur_arg-1], sample_src_names(expr->fetch->use));
12624 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012625 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012626 }
12627
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012628 if (!args[cur_arg] || !*args[cur_arg]) {
12629 memprintf(err, "expects 'len or 'id'");
12630 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012631 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012632 }
12633
Willy Tarreaua9083d02015-05-08 15:27:59 +020012634 if (strcmp(args[cur_arg], "len") == 0) {
12635 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012636
12637 if (!(px->cap & PR_CAP_FE)) {
12638 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012639 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012640 }
12641
12642 proxy->conf.args.ctx = ARGC_CAP;
12643
Willy Tarreaua9083d02015-05-08 15:27:59 +020012644 if (!args[cur_arg]) {
12645 memprintf(err, "missing length value");
12646 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012647 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012648 }
12649 /* we copy the table name for now, it will be resolved later */
12650 len = atoi(args[cur_arg]);
12651 if (len <= 0) {
12652 memprintf(err, "length must be > 0");
12653 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012654 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012655 }
12656 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012657
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012658 if (!len) {
12659 memprintf(err, "a positive 'len' argument is mandatory");
12660 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012661 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012662 }
12663
12664 hdr = calloc(sizeof(struct cap_hdr), 1);
12665 hdr->next = px->req_cap;
12666 hdr->name = NULL; /* not a header capture */
12667 hdr->namelen = 0;
12668 hdr->len = len;
12669 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12670 hdr->index = px->nb_req_cap++;
12671
12672 px->req_cap = hdr;
12673 px->to_log |= LW_REQHDR;
12674
Thierry FOURNIER42148732015-09-02 17:17:33 +020012675 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012676 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012677 rule->arg.cap.expr = expr;
12678 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012679 }
12680
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012681 else if (strcmp(args[cur_arg], "id") == 0) {
12682 int id;
12683 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012684
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012685 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012686
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012687 if (!args[cur_arg]) {
12688 memprintf(err, "missing id value");
12689 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012690 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012691 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012692
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012693 id = strtol(args[cur_arg], &error, 10);
12694 if (*error != '\0') {
12695 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12696 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012697 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012698 }
12699 cur_arg++;
12700
12701 proxy->conf.args.ctx = ARGC_CAP;
12702
Thierry FOURNIER42148732015-09-02 17:17:33 +020012703 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012704 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012705 rule->arg.capid.expr = expr;
12706 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012707 }
12708
12709 else {
12710 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12711 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012712 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012713 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012714
12715 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012716 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012717}
12718
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012719/* This function executes the "capture" action and store the result in a
12720 * capture slot if exists. It executes a fetch expression, turns the result
12721 * into a string and puts it in a capture slot. It always returns 1. If an
12722 * error occurs the action is cancelled, but the rule processing continues.
12723 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012724enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012725 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012726{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012727 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012728 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012729 char **cap = s->res_cap;
12730 struct proxy *fe = strm_fe(s);
12731 int len;
12732 int i;
12733
12734 /* Look for the original configuration. */
12735 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012736 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012737 i--, h = h->next);
12738 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012739 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012740
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012741 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 +020012742 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012743 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012744
12745 if (cap[h->index] == NULL)
12746 cap[h->index] = pool_alloc2(h->pool);
12747
12748 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012749 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012750
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012751 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012752 if (len > h->len)
12753 len = h->len;
12754
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012755 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012756 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012757 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012758}
12759
12760/* parse an "http-response capture" action. It takes a single argument which is
12761 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12762 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012763 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012764 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012765enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12766 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012767{
12768 struct sample_expr *expr;
12769 int cur_arg;
12770 int id;
12771 char *error;
12772
12773 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12774 if (strcmp(args[cur_arg], "if") == 0 ||
12775 strcmp(args[cur_arg], "unless") == 0)
12776 break;
12777
12778 if (cur_arg < *orig_arg + 3) {
12779 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012780 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012781 }
12782
12783 cur_arg = *orig_arg;
12784 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12785 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012786 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012787
12788 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12789 memprintf(err,
12790 "fetch method '%s' extracts information from '%s', none of which is available here",
12791 args[cur_arg-1], sample_src_names(expr->fetch->use));
12792 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012793 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012794 }
12795
12796 if (!args[cur_arg] || !*args[cur_arg]) {
12797 memprintf(err, "expects 'len or 'id'");
12798 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012799 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012800 }
12801
12802 if (strcmp(args[cur_arg], "id") != 0) {
12803 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12804 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012805 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012806 }
12807
12808 cur_arg++;
12809
12810 if (!args[cur_arg]) {
12811 memprintf(err, "missing id value");
12812 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012813 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012814 }
12815
12816 id = strtol(args[cur_arg], &error, 10);
12817 if (*error != '\0') {
12818 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12819 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012820 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012821 }
12822 cur_arg++;
12823
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012824 proxy->conf.args.ctx = ARGC_CAP;
12825
Thierry FOURNIER42148732015-09-02 17:17:33 +020012826 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012827 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012828 rule->arg.capid.expr = expr;
12829 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012830
12831 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012832 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012833}
12834
William Lallemand73025dd2014-04-24 14:38:37 +020012835/*
12836 * Return the struct http_req_action_kw associated to a keyword.
12837 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012838struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012839{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012840 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012841}
12842
12843/*
12844 * Return the struct http_res_action_kw associated to a keyword.
12845 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012846struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012847{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012848 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012849}
12850
Willy Tarreau4a568972010-05-12 08:08:50 +020012851/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012852/* All supported ACL keywords must be declared here. */
12853/************************************************************************/
12854
12855/* Note: must not be declared <const> as its list will be overwritten.
12856 * Please take care of keeping this list alphabetically sorted.
12857 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012858static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012859 { "base", "base", PAT_MATCH_STR },
12860 { "base_beg", "base", PAT_MATCH_BEG },
12861 { "base_dir", "base", PAT_MATCH_DIR },
12862 { "base_dom", "base", PAT_MATCH_DOM },
12863 { "base_end", "base", PAT_MATCH_END },
12864 { "base_len", "base", PAT_MATCH_LEN },
12865 { "base_reg", "base", PAT_MATCH_REG },
12866 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012867
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012868 { "cook", "req.cook", PAT_MATCH_STR },
12869 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12870 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12871 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12872 { "cook_end", "req.cook", PAT_MATCH_END },
12873 { "cook_len", "req.cook", PAT_MATCH_LEN },
12874 { "cook_reg", "req.cook", PAT_MATCH_REG },
12875 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012876
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012877 { "hdr", "req.hdr", PAT_MATCH_STR },
12878 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12879 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12880 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12881 { "hdr_end", "req.hdr", PAT_MATCH_END },
12882 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12883 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12884 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012885
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012886 /* these two declarations uses strings with list storage (in place
12887 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12888 * and delete functions are relative to the list management. The parse
12889 * and match method are related to the corresponding fetch methods. This
12890 * is very particular ACL declaration mode.
12891 */
12892 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12893 { "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 +020012894
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012895 { "path", "path", PAT_MATCH_STR },
12896 { "path_beg", "path", PAT_MATCH_BEG },
12897 { "path_dir", "path", PAT_MATCH_DIR },
12898 { "path_dom", "path", PAT_MATCH_DOM },
12899 { "path_end", "path", PAT_MATCH_END },
12900 { "path_len", "path", PAT_MATCH_LEN },
12901 { "path_reg", "path", PAT_MATCH_REG },
12902 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012903
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012904 { "req_ver", "req.ver", PAT_MATCH_STR },
12905 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012906
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012907 { "scook", "res.cook", PAT_MATCH_STR },
12908 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12909 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12910 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12911 { "scook_end", "res.cook", PAT_MATCH_END },
12912 { "scook_len", "res.cook", PAT_MATCH_LEN },
12913 { "scook_reg", "res.cook", PAT_MATCH_REG },
12914 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012915
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012916 { "shdr", "res.hdr", PAT_MATCH_STR },
12917 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12918 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12919 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12920 { "shdr_end", "res.hdr", PAT_MATCH_END },
12921 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12922 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12923 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012924
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012925 { "url", "url", PAT_MATCH_STR },
12926 { "url_beg", "url", PAT_MATCH_BEG },
12927 { "url_dir", "url", PAT_MATCH_DIR },
12928 { "url_dom", "url", PAT_MATCH_DOM },
12929 { "url_end", "url", PAT_MATCH_END },
12930 { "url_len", "url", PAT_MATCH_LEN },
12931 { "url_reg", "url", PAT_MATCH_REG },
12932 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012933
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012934 { "urlp", "urlp", PAT_MATCH_STR },
12935 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12936 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12937 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12938 { "urlp_end", "urlp", PAT_MATCH_END },
12939 { "urlp_len", "urlp", PAT_MATCH_LEN },
12940 { "urlp_reg", "urlp", PAT_MATCH_REG },
12941 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012942
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012943 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012944}};
12945
12946/************************************************************************/
12947/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012948/************************************************************************/
12949/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012950static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012951 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012952 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012953 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12954
Willy Tarreau87b09662015-04-03 00:22:06 +020012955 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012956 { "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 +020012957
12958 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012959 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12960 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12961 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012962
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012963 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12964 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012965
Willy Tarreau409bcde2013-01-08 00:31:00 +010012966 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12967 * are only here to match the ACL's name, are request-only and are used
12968 * for ACL compatibility only.
12969 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012970 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12971 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012972 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12973 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012974
12975 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12976 * only here to match the ACL's name, are request-only and are used for
12977 * ACL compatibility only.
12978 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012979 { "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 +020012980 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012981 { "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 +020012982 { "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 +010012983
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012984 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012985 { "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 +010012986 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012987 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012988 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012989 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012990
12991 /* HTTP protocol on the request path */
12992 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012993 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012994
12995 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012996 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12997 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012998
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012999 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013000 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13001 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013002 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013003
Willy Tarreau18ed2562013-01-14 15:56:36 +010013004 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013005 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13006 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013007
Willy Tarreau18ed2562013-01-14 15:56:36 +010013008 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013009 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013010 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13011 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013012
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013013 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013014 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013015 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013016 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013017 { "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 +010013018 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013019 { "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 +010013020
13021 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013022 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013023 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13024 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013025
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013026 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013027 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013028 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013029 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013030 { "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 +010013031 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013032 { "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 +010013033
Willy Tarreau409bcde2013-01-08 00:31:00 +010013034 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013035 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013036 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13037 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013038 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013039
13040 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013041 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013042 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013043 { "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 +020013044 { "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 +010013045
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013046 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013047 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013048 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013049 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013050 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013051 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013052 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13053 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013054 { "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 +010013055 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013056}};
13057
Willy Tarreau8797c062007-05-07 00:55:35 +020013058
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013059/************************************************************************/
13060/* All supported converter keywords must be declared here. */
13061/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013062/* Note: must not be declared <const> as its list will be overwritten */
13063static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013064 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013065 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013066 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13067 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013068 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013069 { NULL, NULL, 0, 0, 0 },
13070}};
13071
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013072
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013073/************************************************************************/
13074/* All supported http-request action keywords must be declared here. */
13075/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013076struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013077 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013078 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013079 { "set-method", parse_set_req_line },
13080 { "set-path", parse_set_req_line },
13081 { "set-query", parse_set_req_line },
13082 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013083 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013084 }
13085};
13086
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013087struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013088 .kw = {
13089 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013090 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013091 { NULL, NULL }
13092 }
13093};
13094
Willy Tarreau8797c062007-05-07 00:55:35 +020013095__attribute__((constructor))
13096static void __http_protocol_init(void)
13097{
13098 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013099 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013100 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013101 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013102 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020013103}
13104
13105
Willy Tarreau58f10d72006-12-04 02:26:12 +010013106/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013107 * Local variables:
13108 * c-indent-level: 8
13109 * c-basic-offset: 8
13110 * End:
13111 */