blob: 2dcac06745f102c1605ee566c6b00ae3ab2007b0 [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
5275 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005276 channel_auto_read(&s->req);
5277 channel_auto_close(&s->req);
5278 channel_auto_read(&s->res);
5279 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005280
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005281 /* we're in keep-alive with an idle connection, monitor it if not already done */
5282 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005283 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005284 if (!srv)
5285 si_idle_conn(&s->si[1], NULL);
5286 else if ((srv_conn->flags & CO_FL_PRIVATE) ||
5287 ((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR))
5288 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005289 else if (prev_flags & TX_NOT_FIRST)
5290 /* note: we check the request, not the connection, but
5291 * this is valid for strategies SAFE and AGGR, and in
5292 * case of ALWS, we don't care anyway.
5293 */
5294 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005295 else
5296 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005297 }
Willy Tarreau27375622013-12-17 00:00:28 +01005298
Willy Tarreauc29d0cd2015-09-23 12:18:14 +02005299 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005300 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005301}
5302
5303
5304/* This function updates the request state machine according to the response
5305 * state machine and buffer flags. It returns 1 if it changes anything (flag
5306 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5307 * it is only used to find when a request/response couple is complete. Both
5308 * this function and its equivalent should loop until both return zero. It
5309 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5310 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005311int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005312{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005313 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005314 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005315 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005316 unsigned int old_state = txn->req.msg_state;
5317
Willy Tarreau610ecce2010-01-04 21:15:02 +01005318 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5319 return 0;
5320
5321 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005322 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005323 * We can shut the read side unless we want to abort_on_close,
5324 * or we have a POST request. The issue with POST requests is
5325 * that some browsers still send a CRLF after the request, and
5326 * this CRLF must be read so that it does not remain in the kernel
5327 * buffers, otherwise a close could cause an RST on some systems
5328 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005329 * Note that if we're using keep-alive on the client side, we'd
5330 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005331 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005332 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005333 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005334 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5335 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5336 !(s->be->options & PR_O_ABRT_CLOSE) &&
5337 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005338 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005339
Willy Tarreau40f151a2012-12-20 12:10:09 +01005340 /* if the server closes the connection, we want to immediately react
5341 * and close the socket to save packets and syscalls.
5342 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005343 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005344
Willy Tarreau610ecce2010-01-04 21:15:02 +01005345 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5346 goto wait_other_side;
5347
5348 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5349 /* The server has not finished to respond, so we
5350 * don't want to move in order not to upset it.
5351 */
5352 goto wait_other_side;
5353 }
5354
5355 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5356 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005357 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005358 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005359 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005360 goto wait_other_side;
5361 }
5362
5363 /* When we get here, it means that both the request and the
5364 * response have finished receiving. Depending on the connection
5365 * mode, we'll have to wait for the last bytes to leave in either
5366 * direction, and sometimes for a close to be effective.
5367 */
5368
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005369 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5370 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005371 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5372 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005373 }
5374 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5375 /* Option forceclose is set, or either side wants to close,
5376 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005377 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005378 * once both states are CLOSED.
5379 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005380 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5381 channel_shutr_now(chn);
5382 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005383 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005384 }
5385 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005386 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5387 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005388 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005389 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5390 channel_auto_read(chn);
5391 txn->req.msg_state = HTTP_MSG_TUNNEL;
5392 chn->flags |= CF_NEVER_WAIT;
5393 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005394 }
5395
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005396 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005397 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005398 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005399
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005400 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005401 txn->req.msg_state = HTTP_MSG_CLOSING;
5402 goto http_msg_closing;
5403 }
5404 else {
5405 txn->req.msg_state = HTTP_MSG_CLOSED;
5406 goto http_msg_closed;
5407 }
5408 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005409 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005410 }
5411
5412 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5413 http_msg_closing:
5414 /* nothing else to forward, just waiting for the output buffer
5415 * to be empty and for the shutw_now to take effect.
5416 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005417 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005418 txn->req.msg_state = HTTP_MSG_CLOSED;
5419 goto http_msg_closed;
5420 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005421 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005422 txn->req.msg_state = HTTP_MSG_ERROR;
5423 goto wait_other_side;
5424 }
5425 }
5426
5427 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5428 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005429 /* see above in MSG_DONE why we only do this in these states */
5430 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5431 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5432 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005433 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005434 goto wait_other_side;
5435 }
5436
5437 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005438 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005439}
5440
5441
5442/* This function updates the response state machine according to the request
5443 * state machine and buffer flags. It returns 1 if it changes anything (flag
5444 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5445 * it is only used to find when a request/response couple is complete. Both
5446 * this function and its equivalent should loop until both return zero. It
5447 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5448 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005449int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005450{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005451 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005452 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005453 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005454 unsigned int old_state = txn->rsp.msg_state;
5455
Willy Tarreau610ecce2010-01-04 21:15:02 +01005456 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5457 return 0;
5458
5459 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5460 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005461 * still monitor the server connection for a possible close
5462 * while the request is being uploaded, so we don't disable
5463 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005464 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005465 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005466
5467 if (txn->req.msg_state == HTTP_MSG_ERROR)
5468 goto wait_other_side;
5469
5470 if (txn->req.msg_state < HTTP_MSG_DONE) {
5471 /* The client seems to still be sending data, probably
5472 * because we got an error response during an upload.
5473 * We have the choice of either breaking the connection
5474 * or letting it pass through. Let's do the later.
5475 */
5476 goto wait_other_side;
5477 }
5478
5479 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5480 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005481 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005482 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005483 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005484 goto wait_other_side;
5485 }
5486
5487 /* When we get here, it means that both the request and the
5488 * response have finished receiving. Depending on the connection
5489 * mode, we'll have to wait for the last bytes to leave in either
5490 * direction, and sometimes for a close to be effective.
5491 */
5492
5493 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5494 /* Server-close mode : shut read and wait for the request
5495 * side to close its output buffer. The caller will detect
5496 * when we're in DONE and the other is in CLOSED and will
5497 * catch that for the final cleanup.
5498 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005499 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5500 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005501 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005502 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5503 /* Option forceclose is set, or either side wants to close,
5504 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005505 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005506 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005507 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005508 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5509 channel_shutr_now(chn);
5510 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005511 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005512 }
5513 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005514 /* The last possible modes are keep-alive and tunnel. Tunnel will
5515 * need to forward remaining data. Keep-alive will need to monitor
5516 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005517 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005518 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005519 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005520 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5521 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005522 }
5523
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005524 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005525 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005526 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005527 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5528 goto http_msg_closing;
5529 }
5530 else {
5531 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5532 goto http_msg_closed;
5533 }
5534 }
5535 goto wait_other_side;
5536 }
5537
5538 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5539 http_msg_closing:
5540 /* nothing else to forward, just waiting for the output buffer
5541 * to be empty and for the shutw_now to take effect.
5542 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005543 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005544 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5545 goto http_msg_closed;
5546 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005547 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005548 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005549 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005550 if (objt_server(s->target))
5551 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005552 goto wait_other_side;
5553 }
5554 }
5555
5556 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5557 http_msg_closed:
5558 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005559 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005560 channel_auto_close(chn);
5561 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005562 goto wait_other_side;
5563 }
5564
5565 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005566 /* We force the response to leave immediately if we're waiting for the
5567 * other side, since there is no pending shutdown to push it out.
5568 */
5569 if (!channel_is_empty(chn))
5570 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005571 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005572}
5573
5574
5575/* Resync the request and response state machines. Return 1 if either state
5576 * changes.
5577 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005578int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005579{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005580 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005581 int old_req_state = txn->req.msg_state;
5582 int old_res_state = txn->rsp.msg_state;
5583
Willy Tarreau610ecce2010-01-04 21:15:02 +01005584 http_sync_req_state(s);
5585 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005586 if (!http_sync_res_state(s))
5587 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005588 if (!http_sync_req_state(s))
5589 break;
5590 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005591
Willy Tarreau610ecce2010-01-04 21:15:02 +01005592 /* OK, both state machines agree on a compatible state.
5593 * There are a few cases we're interested in :
5594 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5595 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5596 * directions, so let's simply disable both analysers.
5597 * - HTTP_MSG_CLOSED on the response only means we must abort the
5598 * request.
5599 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5600 * with server-close mode means we've completed one request and we
5601 * must re-initialize the server connection.
5602 */
5603
5604 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5605 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5606 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5607 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005608 s->req.analysers = 0;
5609 channel_auto_close(&s->req);
5610 channel_auto_read(&s->req);
5611 s->res.analysers = 0;
5612 channel_auto_close(&s->res);
5613 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005614 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005615 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005616 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005617 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005618 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005619 s->res.analysers = 0;
5620 channel_auto_close(&s->res);
5621 channel_auto_read(&s->res);
5622 s->req.analysers = 0;
5623 channel_abort(&s->req);
5624 channel_auto_close(&s->req);
5625 channel_auto_read(&s->req);
5626 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005627 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005628 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5629 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005630 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005631 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5632 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5633 /* server-close/keep-alive: terminate this transaction,
5634 * possibly killing the server connection and reinitialize
5635 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005636 */
5637 http_end_txn_clean_session(s);
5638 }
5639
Willy Tarreau610ecce2010-01-04 21:15:02 +01005640 return txn->req.msg_state != old_req_state ||
5641 txn->rsp.msg_state != old_res_state;
5642}
5643
Willy Tarreaud98cf932009-12-27 22:54:55 +01005644/* This function is an analyser which forwards request body (including chunk
5645 * sizes if any). It is called as soon as we must forward, even if we forward
5646 * zero byte. The only situation where it must not be called is when we're in
5647 * tunnel mode and we want to forward till the close. It's used both to forward
5648 * remaining data and to resync after end of body. It expects the msg_state to
5649 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005650 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005651 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005652 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005653 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005654int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005655{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005656 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005657 struct http_txn *txn = s->txn;
5658 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005659
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005660 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5661 return 0;
5662
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005663 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005664 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005665 /* Output closed while we were sending data. We must abort and
5666 * wake the other side up.
5667 */
5668 msg->msg_state = HTTP_MSG_ERROR;
5669 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005670 return 1;
5671 }
5672
Willy Tarreaud98cf932009-12-27 22:54:55 +01005673 /* Note that we don't have to send 100-continue back because we don't
5674 * need the data to complete our job, and it's up to the server to
5675 * decide whether to return 100, 417 or anything else in return of
5676 * an "Expect: 100-continue" header.
5677 */
5678
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005679 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005680 /* we have msg->sov which points to the first byte of message
5681 * body, and req->buf.p still points to the beginning of the
5682 * message. We forward the headers now, as we don't need them
5683 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005684 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005685 b_adv(req->buf, msg->sov);
5686 msg->next -= msg->sov;
5687 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005688
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005689 /* The previous analysers guarantee that the state is somewhere
5690 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5691 * msg->next are always correct.
5692 */
5693 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5694 if (msg->flags & HTTP_MSGF_TE_CHNK)
5695 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5696 else
5697 msg->msg_state = HTTP_MSG_DATA;
5698 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005699 }
5700
Willy Tarreau7ba23542014-04-17 21:50:00 +02005701 /* Some post-connect processing might want us to refrain from starting to
5702 * forward data. Currently, the only reason for this is "balance url_param"
5703 * whichs need to parse/process the request after we've enabled forwarding.
5704 */
5705 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005706 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005707 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005708 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005709 goto missing_data;
5710 }
5711 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5712 }
5713
Willy Tarreau80a92c02014-03-12 10:41:13 +01005714 /* in most states, we should abort in case of early close */
5715 channel_auto_close(req);
5716
Willy Tarreauefdf0942014-04-24 20:08:57 +02005717 if (req->to_forward) {
5718 /* We can't process the buffer's contents yet */
5719 req->flags |= CF_WAKE_WRITE;
5720 goto missing_data;
5721 }
5722
Willy Tarreaud98cf932009-12-27 22:54:55 +01005723 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005724 if (msg->msg_state == HTTP_MSG_DATA) {
5725 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005726 /* we may have some pending data starting at req->buf->p */
5727 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005728 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005729 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005730 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005731 msg->next += msg->chunk_len;
5732 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005733
5734 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005735 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005736 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005737 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005738 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005739 }
5740 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005741 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005742 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005743 * TRAILERS state.
5744 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005745 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005746
Willy Tarreau54d23df2012-10-25 19:04:45 +02005747 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005748 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005749 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005750 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005751 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005752 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005753 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005754 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005755 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005756 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005757 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005758 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005759 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005760
5761 if (ret == 0)
5762 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005763 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005764 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005765 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005766 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005767 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005768 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005769 /* we're in MSG_CHUNK_SIZE now */
5770 }
5771 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005772 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005773
5774 if (ret == 0)
5775 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005776 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005777 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005778 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005779 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005780 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005781 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005782 /* we're in HTTP_MSG_DONE now */
5783 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005784 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005785 int old_state = msg->msg_state;
5786
Willy Tarreau610ecce2010-01-04 21:15:02 +01005787 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005788
5789 /* we may have some pending data starting at req->buf->p
5790 * such as last chunk of data or trailers.
5791 */
5792 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005793 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005794 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005795 msg->next = 0;
5796
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005797 /* we don't want to forward closes on DONE except in
5798 * tunnel mode.
5799 */
5800 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005801 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005802 if (http_resync_states(s)) {
5803 /* some state changes occurred, maybe the analyser
5804 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005805 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005806 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005807 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005808 /* request errors are most likely due to
5809 * the server aborting the transfer.
5810 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005811 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005812 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005813 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005814 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005815 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005816 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005817 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005818 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005819
5820 /* If "option abortonclose" is set on the backend, we
5821 * want to monitor the client's connection and forward
5822 * any shutdown notification to the server, which will
5823 * decide whether to close or to go on processing the
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005824 * request. We only do that in tunnel mode, and not in
5825 * other modes since it can be abused to exhaust source
5826 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005827 */
5828 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005829 channel_auto_read(req);
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005830 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5831 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5832 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005833 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005834 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005835 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005836 /* POST requests may require to read extra CRLF
5837 * sent by broken browsers and which could cause
5838 * an RST to be sent upon close on some systems
5839 * (eg: Linux).
5840 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005841 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005842 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005843
Willy Tarreau610ecce2010-01-04 21:15:02 +01005844 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005845 }
5846 }
5847
Willy Tarreaud98cf932009-12-27 22:54:55 +01005848 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005849 /* we may have some pending data starting at req->buf->p */
5850 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005851 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005852 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5853
Willy Tarreaubed410e2014-04-22 08:19:34 +02005854 msg->next = 0;
5855 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5856
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005857 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005858 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005859 if (!(s->flags & SF_ERR_MASK))
5860 s->flags |= SF_ERR_CLICL;
5861 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005862 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005863 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005864 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005865 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005866 }
5867
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005868 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005869 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005870 if (objt_server(s->target))
5871 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005872
5873 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005874 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005875
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005876 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005877 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005878 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005879
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005880 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005881 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005882 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005883 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005884 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005885
Willy Tarreau5c620922011-05-11 19:56:11 +02005886 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005887 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005888 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005889 * modes are already handled by the stream sock layer. We must not do
5890 * this in content-length mode because it could present the MSG_MORE
5891 * flag with the last block of forwarded data, which would cause an
5892 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005893 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005894 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005895 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005896
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005897 return 0;
5898
Willy Tarreaud98cf932009-12-27 22:54:55 +01005899 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005900 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005901 if (sess->listener->counters)
5902 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005903
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005904 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005905 /* we may have some pending data starting at req->buf->p */
5906 b_adv(req->buf, msg->next);
5907 msg->next = 0;
5908
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005909 txn->req.msg_state = HTTP_MSG_ERROR;
5910 if (txn->status) {
5911 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005912 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005913 } else {
5914 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005915 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005916 }
5917 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005918 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005919
Willy Tarreaue7dff022015-04-03 01:14:29 +02005920 if (!(s->flags & SF_ERR_MASK))
5921 s->flags |= SF_ERR_PRXCOND;
5922 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005923 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005924 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005925 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005926 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005927 }
5928 return 0;
5929
5930 aborted_xfer:
5931 txn->req.msg_state = HTTP_MSG_ERROR;
5932 if (txn->status) {
5933 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005934 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005935 } else {
5936 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005937 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005938 }
5939 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005940 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005941
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005942 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005943 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005944 if (objt_server(s->target))
5945 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005946
Willy Tarreaue7dff022015-04-03 01:14:29 +02005947 if (!(s->flags & SF_ERR_MASK))
5948 s->flags |= SF_ERR_SRVCL;
5949 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005950 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005951 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005952 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005953 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005954 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005955 return 0;
5956}
5957
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005958/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5959 * processing can continue on next analysers, or zero if it either needs more
5960 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005961 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005962 * when it has nothing left to do, and may remove any analyser when it wants to
5963 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005964 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005965int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005966{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005967 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005968 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005969 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005970 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005971 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005972 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005973 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005974
Willy Tarreau87b09662015-04-03 00:22:06 +02005975 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 +02005976 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005977 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005978 rep,
5979 rep->rex, rep->wex,
5980 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005981 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005982 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005983
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005984 /*
5985 * Now parse the partial (or complete) lines.
5986 * We will check the response syntax, and also join multi-line
5987 * headers. An index of all the lines will be elaborated while
5988 * parsing.
5989 *
5990 * For the parsing, we use a 28 states FSM.
5991 *
5992 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005993 * rep->buf->p = beginning of response
5994 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5995 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005996 * msg->eol = end of current header or line (LF or CRLF)
5997 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005998 */
5999
Willy Tarreau628c40c2014-04-24 19:11:26 +02006000 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01006001 /* There's a protected area at the end of the buffer for rewriting
6002 * purposes. We don't want to start to parse the request if the
6003 * protected area is affected, because we may have to move processed
6004 * data later, which is much more complicated.
6005 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006006 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01006007 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02006008 /* some data has still not left the buffer, wake us once that's done */
6009 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
6010 goto abort_response;
6011 channel_dont_close(rep);
6012 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01006013 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02006014 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01006015 }
6016
Willy Tarreau379357a2013-06-08 12:55:46 +02006017 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
6018 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
6019 buffer_slow_realign(rep->buf);
6020
Willy Tarreau9b28e032012-10-12 23:49:43 +02006021 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01006022 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01006023 }
6024
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006025 /* 1: we might have to print this header in debug mode */
6026 if (unlikely((global.mode & MODE_DEBUG) &&
6027 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02006028 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006029 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006030
Willy Tarreau9b28e032012-10-12 23:49:43 +02006031 sol = rep->buf->p;
6032 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006033 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006034
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006035 sol += hdr_idx_first_pos(&txn->hdr_idx);
6036 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006037
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006038 while (cur_idx) {
6039 eol = sol + txn->hdr_idx.v[cur_idx].len;
6040 debug_hdr("srvhdr", s, sol, eol);
6041 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
6042 cur_idx = txn->hdr_idx.v[cur_idx].next;
6043 }
6044 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006045
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006046 /*
6047 * Now we quickly check if we have found a full valid response.
6048 * If not so, we check the FD and buffer states before leaving.
6049 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01006050 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006051 * responses are checked first.
6052 *
6053 * Depending on whether the client is still there or not, we
6054 * may send an error response back or not. Note that normally
6055 * we should only check for HTTP status there, and check I/O
6056 * errors somewhere else.
6057 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006058
Willy Tarreau655dce92009-11-08 13:10:58 +01006059 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006060 /* Invalid response */
6061 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6062 /* we detected a parsing error. We want to archive this response
6063 * in the dedicated proxy area for later troubleshooting.
6064 */
6065 hdr_response_bad:
6066 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006067 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006068
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006069 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006070 if (objt_server(s->target)) {
6071 objt_server(s->target)->counters.failed_resp++;
6072 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006073 }
Willy Tarreau64648412010-03-05 10:41:54 +01006074 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006075 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006076 rep->analysers = 0;
6077 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006078 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006079 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006080 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006081
Willy Tarreaue7dff022015-04-03 01:14:29 +02006082 if (!(s->flags & SF_ERR_MASK))
6083 s->flags |= SF_ERR_PRXCOND;
6084 if (!(s->flags & SF_FINST_MASK))
6085 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006086
6087 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006088 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006089
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006090 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006091 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006092 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006093 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006094 goto hdr_response_bad;
6095 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006096
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006097 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006098 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006099 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006100 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006101 else if (txn->flags & TX_NOT_FIRST)
6102 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006103
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006104 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006105 if (objt_server(s->target)) {
6106 objt_server(s->target)->counters.failed_resp++;
6107 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006108 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006109
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006110 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006111 rep->analysers = 0;
6112 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006113 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006114 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006115 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006116
Willy Tarreaue7dff022015-04-03 01:14:29 +02006117 if (!(s->flags & SF_ERR_MASK))
6118 s->flags |= SF_ERR_SRVCL;
6119 if (!(s->flags & SF_FINST_MASK))
6120 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006121 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006122 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006123
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006124 /* read timeout : return a 504 to the client. */
6125 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006126 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006127 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006128 else if (txn->flags & TX_NOT_FIRST)
6129 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006130
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006131 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006132 if (objt_server(s->target)) {
6133 objt_server(s->target)->counters.failed_resp++;
6134 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006135 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006136
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006137 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006138 rep->analysers = 0;
6139 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006140 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006141 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006142 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006143
Willy Tarreaue7dff022015-04-03 01:14:29 +02006144 if (!(s->flags & SF_ERR_MASK))
6145 s->flags |= SF_ERR_SRVTO;
6146 if (!(s->flags & SF_FINST_MASK))
6147 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006148 return 0;
6149 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006150
Willy Tarreauf003d372012-11-26 13:35:37 +01006151 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006152 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006153 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006154 s->be->be_counters.cli_aborts++;
6155 if (objt_server(s->target))
6156 objt_server(s->target)->counters.cli_aborts++;
6157
6158 rep->analysers = 0;
6159 channel_auto_close(rep);
6160
6161 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006162 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006163 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006164
Willy Tarreaue7dff022015-04-03 01:14:29 +02006165 if (!(s->flags & SF_ERR_MASK))
6166 s->flags |= SF_ERR_CLICL;
6167 if (!(s->flags & SF_FINST_MASK))
6168 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006169
Willy Tarreau87b09662015-04-03 00:22:06 +02006170 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006171 return 0;
6172 }
6173
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006174 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006175 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006176 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006177 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006178 else if (txn->flags & TX_NOT_FIRST)
6179 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006180
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006181 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006182 if (objt_server(s->target)) {
6183 objt_server(s->target)->counters.failed_resp++;
6184 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006185 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006186
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006187 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006188 rep->analysers = 0;
6189 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006190 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006191 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006192 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006193
Willy Tarreaue7dff022015-04-03 01:14:29 +02006194 if (!(s->flags & SF_ERR_MASK))
6195 s->flags |= SF_ERR_SRVCL;
6196 if (!(s->flags & SF_FINST_MASK))
6197 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006198 return 0;
6199 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006200
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006201 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006202 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006203 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006204 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006205 else if (txn->flags & TX_NOT_FIRST)
6206 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006207
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006208 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006209 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006210 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006211
Willy Tarreaue7dff022015-04-03 01:14:29 +02006212 if (!(s->flags & SF_ERR_MASK))
6213 s->flags |= SF_ERR_CLICL;
6214 if (!(s->flags & SF_FINST_MASK))
6215 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006216
Willy Tarreau87b09662015-04-03 00:22:06 +02006217 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006218 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006219 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006220
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006221 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006222 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006223 return 0;
6224 }
6225
6226 /* More interesting part now : we know that we have a complete
6227 * response which at least looks like HTTP. We have an indicator
6228 * of each header's length, so we can parse them quickly.
6229 */
6230
6231 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006232 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006233
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006234 /*
6235 * 1: get the status code
6236 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006237 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006238 if (n < 1 || n > 5)
6239 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006240 /* when the client triggers a 4xx from the server, it's most often due
6241 * to a missing object or permission. These events should be tracked
6242 * because if they happen often, it may indicate a brute force or a
6243 * vulnerability scan.
6244 */
6245 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006246 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006247
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006248 if (objt_server(s->target))
6249 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006250
Willy Tarreau91852eb2015-05-01 13:26:00 +02006251 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6252 * exactly one digit "." one digit. This check may be disabled using
6253 * option accept-invalid-http-response.
6254 */
6255 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6256 if (msg->sl.st.v_l != 8) {
6257 msg->err_pos = 0;
6258 goto hdr_response_bad;
6259 }
6260
6261 if (rep->buf->p[4] != '/' ||
6262 !isdigit((unsigned char)rep->buf->p[5]) ||
6263 rep->buf->p[6] != '.' ||
6264 !isdigit((unsigned char)rep->buf->p[7])) {
6265 msg->err_pos = 4;
6266 goto hdr_response_bad;
6267 }
6268 }
6269
Willy Tarreau5b154472009-12-21 20:11:07 +01006270 /* check if the response is HTTP/1.1 or above */
6271 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006272 ((rep->buf->p[5] > '1') ||
6273 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006274 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006275
6276 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006277 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 +01006278
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006279 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006280 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006281
Willy Tarreau9b28e032012-10-12 23:49:43 +02006282 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006283
Willy Tarreau39650402010-03-15 19:44:39 +01006284 /* Adjust server's health based on status code. Note: status codes 501
6285 * and 505 are triggered on demand by client request, so we must not
6286 * count them as server failures.
6287 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006288 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006289 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006290 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006291 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006292 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006293 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006294
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006295 /*
6296 * 2: check for cacheability.
6297 */
6298
6299 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006300 case 100:
6301 /*
6302 * We may be facing a 100-continue response, in which case this
6303 * is not the right response, and we're waiting for the next one.
6304 * Let's allow this response to go to the client and wait for the
6305 * next one.
6306 */
6307 hdr_idx_init(&txn->hdr_idx);
6308 msg->next -= channel_forward(rep, msg->next);
6309 msg->msg_state = HTTP_MSG_RPBEFORE;
6310 txn->status = 0;
6311 s->logs.t_data = -1; /* was not a response yet */
6312 goto next_one;
6313
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006314 case 200:
6315 case 203:
6316 case 206:
6317 case 300:
6318 case 301:
6319 case 410:
6320 /* RFC2616 @13.4:
6321 * "A response received with a status code of
6322 * 200, 203, 206, 300, 301 or 410 MAY be stored
6323 * by a cache (...) unless a cache-control
6324 * directive prohibits caching."
6325 *
6326 * RFC2616 @9.5: POST method :
6327 * "Responses to this method are not cacheable,
6328 * unless the response includes appropriate
6329 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006330 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006331 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006332 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006333 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6334 break;
6335 default:
6336 break;
6337 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006338
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006339 /*
6340 * 3: we may need to capture headers
6341 */
6342 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006343 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006344 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006345 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006346
Willy Tarreau557f1992015-05-01 10:05:17 +02006347 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6348 * by RFC7230#3.3.3 :
6349 *
6350 * The length of a message body is determined by one of the following
6351 * (in order of precedence):
6352 *
6353 * 1. Any response to a HEAD request and any response with a 1xx
6354 * (Informational), 204 (No Content), or 304 (Not Modified) status
6355 * code is always terminated by the first empty line after the
6356 * header fields, regardless of the header fields present in the
6357 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006358 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006359 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6360 * the connection will become a tunnel immediately after the empty
6361 * line that concludes the header fields. A client MUST ignore any
6362 * Content-Length or Transfer-Encoding header fields received in
6363 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006364 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006365 * 3. If a Transfer-Encoding header field is present and the chunked
6366 * transfer coding (Section 4.1) is the final encoding, the message
6367 * body length is determined by reading and decoding the chunked
6368 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006369 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006370 * If a Transfer-Encoding header field is present in a response and
6371 * the chunked transfer coding is not the final encoding, the
6372 * message body length is determined by reading the connection until
6373 * it is closed by the server. If a Transfer-Encoding header field
6374 * is present in a request and the chunked transfer coding is not
6375 * the final encoding, the message body length cannot be determined
6376 * reliably; the server MUST respond with the 400 (Bad Request)
6377 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006378 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006379 * If a message is received with both a Transfer-Encoding and a
6380 * Content-Length header field, the Transfer-Encoding overrides the
6381 * Content-Length. Such a message might indicate an attempt to
6382 * perform request smuggling (Section 9.5) or response splitting
6383 * (Section 9.4) and ought to be handled as an error. A sender MUST
6384 * remove the received Content-Length field prior to forwarding such
6385 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006386 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006387 * 4. If a message is received without Transfer-Encoding and with
6388 * either multiple Content-Length header fields having differing
6389 * field-values or a single Content-Length header field having an
6390 * invalid value, then the message framing is invalid and the
6391 * recipient MUST treat it as an unrecoverable error. If this is a
6392 * request message, the server MUST respond with a 400 (Bad Request)
6393 * status code and then close the connection. If this is a response
6394 * message received by a proxy, the proxy MUST close the connection
6395 * to the server, discard the received response, and send a 502 (Bad
6396 * Gateway) response to the client. If this is a response message
6397 * received by a user agent, the user agent MUST close the
6398 * connection to the server and discard the received response.
6399 *
6400 * 5. If a valid Content-Length header field is present without
6401 * Transfer-Encoding, its decimal value defines the expected message
6402 * body length in octets. If the sender closes the connection or
6403 * the recipient times out before the indicated number of octets are
6404 * received, the recipient MUST consider the message to be
6405 * incomplete and close the connection.
6406 *
6407 * 6. If this is a request message and none of the above are true, then
6408 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006409 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006410 * 7. Otherwise, this is a response message without a declared message
6411 * body length, so the message body length is determined by the
6412 * number of octets received prior to the server closing the
6413 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006414 */
6415
6416 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006417 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006418 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006419 * FIXME: should we parse anyway and return an error on chunked encoding ?
6420 */
6421 if (txn->meth == HTTP_METH_HEAD ||
6422 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006423 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006424 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006425 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006426 goto skip_content_length;
6427 }
6428
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006429 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006430 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006431 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006432 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006433 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6434 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006435 /* bad transfer-encoding (chunked followed by something else) */
6436 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006437 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006438 break;
6439 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006440 }
6441
Willy Tarreau1c913912015-04-30 10:57:51 +02006442 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006443 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006444 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006445 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6446 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6447 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006448 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006449 signed long long cl;
6450
Willy Tarreauad14f752011-09-02 20:33:27 +02006451 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006452 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006453 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006454 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006455
Willy Tarreauad14f752011-09-02 20:33:27 +02006456 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
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; /* parse failure */
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 (cl < 0) {
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;
Willy Tarreauad14f752011-09-02 20:33:27 +02006464 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006465
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006466 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
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; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006469 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006470
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006471 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006472 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006473 }
6474
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006475 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006476 select_compression_response_header(s, rep->buf);
6477
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006478skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006479 /* Now we have to check if we need to modify the Connection header.
6480 * This is more difficult on the response than it is on the request,
6481 * because we can have two different HTTP versions and we don't know
6482 * how the client will interprete a response. For instance, let's say
6483 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6484 * HTTP/1.1 response without any header. Maybe it will bound itself to
6485 * HTTP/1.0 because it only knows about it, and will consider the lack
6486 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6487 * the lack of header as a keep-alive. Thus we will use two flags
6488 * indicating how a request MAY be understood by the client. In case
6489 * of multiple possibilities, we'll fix the header to be explicit. If
6490 * ambiguous cases such as both close and keepalive are seen, then we
6491 * will fall back to explicit close. Note that we won't take risks with
6492 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006493 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006494 */
6495
Willy Tarreaudc008c52010-02-01 16:20:08 +01006496 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6497 txn->status == 101)) {
6498 /* Either we've established an explicit tunnel, or we're
6499 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006500 * to understand the next protocols. We have to switch to tunnel
6501 * mode, so that we transfer the request and responses then let
6502 * this protocol pass unmodified. When we later implement specific
6503 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006504 * header which contains information about that protocol for
6505 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006506 */
6507 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6508 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006509 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6510 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006511 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006512 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006513 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006514
Willy Tarreau70dffda2014-01-30 03:07:23 +01006515 /* this situation happens when combining pretend-keepalive with httpclose. */
6516 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006517 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006518 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006519 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6520
Willy Tarreau60466522010-01-18 19:08:45 +01006521 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006522 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006523 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6524 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006525
Willy Tarreau60466522010-01-18 19:08:45 +01006526 /* now adjust header transformations depending on current state */
6527 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6528 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6529 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006530 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006531 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006532 }
Willy Tarreau60466522010-01-18 19:08:45 +01006533 else { /* SCL / KAL */
6534 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006535 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006536 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006537 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006538
Willy Tarreau60466522010-01-18 19:08:45 +01006539 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006540 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006541
Willy Tarreau60466522010-01-18 19:08:45 +01006542 /* Some keep-alive responses are converted to Server-close if
6543 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006544 */
Willy Tarreau60466522010-01-18 19:08:45 +01006545 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6546 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006547 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006548 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006549 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006550 }
6551
Willy Tarreau7959a552013-09-23 16:44:27 +02006552 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006553 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006554
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006555 /* end of job, return OK */
6556 rep->analysers &= ~an_bit;
6557 rep->analyse_exp = TICK_ETERNITY;
6558 channel_auto_close(rep);
6559 return 1;
6560
6561 abort_keep_alive:
6562 /* A keep-alive request to the server failed on a network error.
6563 * The client is required to retry. We need to close without returning
6564 * any other information so that the client retries.
6565 */
6566 txn->status = 0;
6567 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006568 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006569 channel_auto_close(rep);
6570 s->logs.logwait = 0;
6571 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006572 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006573 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006574 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006575 return 0;
6576}
6577
6578/* This function performs all the processing enabled for the current response.
6579 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006580 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006581 * other functions. It works like process_request (see indications above).
6582 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006583int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006584{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006585 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006586 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006587 struct http_msg *msg = &txn->rsp;
6588 struct proxy *cur_proxy;
6589 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006590 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006591
Willy Tarreau87b09662015-04-03 00:22:06 +02006592 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 +02006593 now_ms, __FUNCTION__,
6594 s,
6595 rep,
6596 rep->rex, rep->wex,
6597 rep->flags,
6598 rep->buf->i,
6599 rep->analysers);
6600
6601 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6602 return 0;
6603
Willy Tarreau70730dd2014-04-24 18:06:27 +02006604 /* The stats applet needs to adjust the Connection header but we don't
6605 * apply any filter there.
6606 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006607 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6608 rep->analysers &= ~an_bit;
6609 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006610 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006611 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006612
Willy Tarreau58975672014-04-24 21:13:57 +02006613 /*
6614 * We will have to evaluate the filters.
6615 * As opposed to version 1.2, now they will be evaluated in the
6616 * filters order and not in the header order. This means that
6617 * each filter has to be validated among all headers.
6618 *
6619 * Filters are tried with ->be first, then with ->fe if it is
6620 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006621 *
6622 * Maybe we are in resume condiion. In this case I choose the
6623 * "struct proxy" which contains the rule list matching the resume
6624 * pointer. If none of theses "struct proxy" match, I initialise
6625 * the process with the first one.
6626 *
6627 * In fact, I check only correspondance betwwen the current list
6628 * pointer and the ->fe rule list. If it doesn't match, I initialize
6629 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006630 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006631 if (s->current_rule_list == &sess->fe->http_res_rules)
6632 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006633 else
6634 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006635 while (1) {
6636 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006637
Willy Tarreau58975672014-04-24 21:13:57 +02006638 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006639 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006640 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006641
Willy Tarreau51d861a2015-05-22 17:30:48 +02006642 if (ret == HTTP_RULE_RES_BADREQ)
6643 goto return_srv_prx_502;
6644
6645 if (ret == HTTP_RULE_RES_DONE) {
6646 rep->analysers &= ~an_bit;
6647 rep->analyse_exp = TICK_ETERNITY;
6648 return 1;
6649 }
6650 }
6651
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006652 /* we need to be called again. */
6653 if (ret == HTTP_RULE_RES_YIELD) {
6654 channel_dont_close(rep);
6655 return 0;
6656 }
6657
Willy Tarreau58975672014-04-24 21:13:57 +02006658 /* try headers filters */
6659 if (rule_set->rsp_exp != NULL) {
6660 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6661 return_bad_resp:
6662 if (objt_server(s->target)) {
6663 objt_server(s->target)->counters.failed_resp++;
6664 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006665 }
Willy Tarreau58975672014-04-24 21:13:57 +02006666 s->be->be_counters.failed_resp++;
6667 return_srv_prx_502:
6668 rep->analysers = 0;
6669 txn->status = 502;
6670 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006671 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006672 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006673 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006674 if (!(s->flags & SF_ERR_MASK))
6675 s->flags |= SF_ERR_PRXCOND;
6676 if (!(s->flags & SF_FINST_MASK))
6677 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006678 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006679 }
Willy Tarreau58975672014-04-24 21:13:57 +02006680 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006681
Willy Tarreau58975672014-04-24 21:13:57 +02006682 /* has the response been denied ? */
6683 if (txn->flags & TX_SVDENY) {
6684 if (objt_server(s->target))
6685 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006686
Willy Tarreau58975672014-04-24 21:13:57 +02006687 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006688 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006689 if (sess->listener->counters)
6690 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006691
Willy Tarreau58975672014-04-24 21:13:57 +02006692 goto return_srv_prx_502;
6693 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006694
Willy Tarreau58975672014-04-24 21:13:57 +02006695 /* add response headers from the rule sets in the same order */
6696 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006697 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006698 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006699 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006700 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006701 ret = acl_pass(ret);
6702 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6703 ret = !ret;
6704 if (!ret)
6705 continue;
6706 }
6707 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6708 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006709 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006710
Willy Tarreau58975672014-04-24 21:13:57 +02006711 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006712 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006713 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006714 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006715 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006716
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006717 /* After this point, this anayzer can't return yield, so we can
6718 * remove the bit corresponding to this analyzer from the list.
6719 *
6720 * Note that the intermediate returns and goto found previously
6721 * reset the analyzers.
6722 */
6723 rep->analysers &= ~an_bit;
6724 rep->analyse_exp = TICK_ETERNITY;
6725
Willy Tarreau58975672014-04-24 21:13:57 +02006726 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006727 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006728 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006729
Willy Tarreau58975672014-04-24 21:13:57 +02006730 /*
6731 * Now check for a server cookie.
6732 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006733 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006734 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006735
Willy Tarreau58975672014-04-24 21:13:57 +02006736 /*
6737 * Check for cache-control or pragma headers if required.
6738 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006739 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 +02006740 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006741
Willy Tarreau58975672014-04-24 21:13:57 +02006742 /*
6743 * Add server cookie in the response if needed
6744 */
6745 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6746 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006747 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006748 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6749 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6750 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6751 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6752 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006753 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006754 /* the server is known, it's not the one the client requested, or the
6755 * cookie's last seen date needs to be refreshed. We have to
6756 * insert a set-cookie here, except if we want to insert only on POST
6757 * requests and this one isn't. Note that servers which don't have cookies
6758 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006759 */
Willy Tarreau58975672014-04-24 21:13:57 +02006760 if (!objt_server(s->target)->cookie) {
6761 chunk_printf(&trash,
6762 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6763 s->be->cookie_name);
6764 }
6765 else {
6766 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006767
Willy Tarreau58975672014-04-24 21:13:57 +02006768 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6769 /* emit last_date, which is mandatory */
6770 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6771 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6772 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006773
Willy Tarreau58975672014-04-24 21:13:57 +02006774 if (s->be->cookie_maxlife) {
6775 /* emit first_date, which is either the original one or
6776 * the current date.
6777 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006778 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006779 s30tob64(txn->cookie_first_date ?
6780 txn->cookie_first_date >> 2 :
6781 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006782 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006783 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006784 }
Willy Tarreau58975672014-04-24 21:13:57 +02006785 chunk_appendf(&trash, "; path=/");
6786 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006787
Willy Tarreau58975672014-04-24 21:13:57 +02006788 if (s->be->cookie_domain)
6789 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006790
Willy Tarreau58975672014-04-24 21:13:57 +02006791 if (s->be->ck_opts & PR_CK_HTTPONLY)
6792 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006793
Willy Tarreau58975672014-04-24 21:13:57 +02006794 if (s->be->ck_opts & PR_CK_SECURE)
6795 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006796
Willy Tarreau58975672014-04-24 21:13:57 +02006797 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6798 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006799
Willy Tarreau58975672014-04-24 21:13:57 +02006800 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006801 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006802 /* the server did not change, only the date was updated */
6803 txn->flags |= TX_SCK_UPDATED;
6804 else
6805 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006806
Willy Tarreau58975672014-04-24 21:13:57 +02006807 /* Here, we will tell an eventual cache on the client side that we don't
6808 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6809 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6810 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006811 */
Willy Tarreau58975672014-04-24 21:13:57 +02006812 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006813
Willy Tarreau58975672014-04-24 21:13:57 +02006814 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006815
Willy Tarreau58975672014-04-24 21:13:57 +02006816 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6817 "Cache-control: private", 22) < 0))
6818 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006819 }
Willy Tarreau58975672014-04-24 21:13:57 +02006820 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006821
Willy Tarreau58975672014-04-24 21:13:57 +02006822 /*
6823 * Check if result will be cacheable with a cookie.
6824 * We'll block the response if security checks have caught
6825 * nasty things such as a cacheable cookie.
6826 */
6827 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6828 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6829 (s->be->options & PR_O_CHK_CACHE)) {
6830 /* we're in presence of a cacheable response containing
6831 * a set-cookie header. We'll block it as requested by
6832 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006833 */
Willy Tarreau58975672014-04-24 21:13:57 +02006834 if (objt_server(s->target))
6835 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006836
Willy Tarreau58975672014-04-24 21:13:57 +02006837 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006838 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006839 if (sess->listener->counters)
6840 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006841
Willy Tarreau58975672014-04-24 21:13:57 +02006842 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6843 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6844 send_log(s->be, LOG_ALERT,
6845 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6846 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6847 goto return_srv_prx_502;
6848 }
Willy Tarreau03945942009-12-22 16:50:27 +01006849
Willy Tarreau70730dd2014-04-24 18:06:27 +02006850 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006851 /*
6852 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6853 * If an "Upgrade" token is found, the header is left untouched in order
6854 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006855 * if anything but "Upgrade" is present in the Connection header. We don't
6856 * want to touch any 101 response either since it's switching to another
6857 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006858 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006859 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006860 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006861 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006862 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6863 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006864
Willy Tarreau58975672014-04-24 21:13:57 +02006865 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6866 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6867 /* we want a keep-alive response here. Keep-alive header
6868 * required if either side is not 1.1.
6869 */
6870 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6871 want_flags |= TX_CON_KAL_SET;
6872 }
6873 else {
6874 /* we want a close response here. Close header required if
6875 * the server is 1.1, regardless of the client.
6876 */
6877 if (msg->flags & HTTP_MSGF_VER_11)
6878 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006879 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006880
Willy Tarreau58975672014-04-24 21:13:57 +02006881 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6882 http_change_connection_header(txn, msg, want_flags);
6883 }
6884
6885 skip_header_mangling:
6886 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6887 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6888 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006889
Willy Tarreau58975672014-04-24 21:13:57 +02006890 /* if the user wants to log as soon as possible, without counting
6891 * bytes from the server, then this is the right moment. We have
6892 * to temporarily assign bytes_out to log what we currently have.
6893 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006894 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006895 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6896 s->logs.bytes_out = txn->rsp.eoh;
6897 s->do_log(s);
6898 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006899 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006900 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006901}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006902
Willy Tarreaud98cf932009-12-27 22:54:55 +01006903/* This function is an analyser which forwards response body (including chunk
6904 * sizes if any). It is called as soon as we must forward, even if we forward
6905 * zero byte. The only situation where it must not be called is when we're in
6906 * tunnel mode and we want to forward till the close. It's used both to forward
6907 * remaining data and to resync after end of body. It expects the msg_state to
6908 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006909 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006910 *
6911 * It is capable of compressing response data both in content-length mode and
6912 * in chunked mode. The state machines follows different flows depending on
6913 * whether content-length and chunked modes are used, since there are no
6914 * trailers in content-length :
6915 *
6916 * chk-mode cl-mode
6917 * ,----- BODY -----.
6918 * / \
6919 * V size > 0 V chk-mode
6920 * .--> SIZE -------------> DATA -------------> CRLF
6921 * | | size == 0 | last byte |
6922 * | v final crlf v inspected |
6923 * | TRAILERS -----------> DONE |
6924 * | |
6925 * `----------------------------------------------'
6926 *
6927 * Compression only happens in the DATA state, and must be flushed in final
6928 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6929 * is performed at once on final states for all bytes parsed, or when leaving
6930 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006931 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006932int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006933{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006934 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006935 struct http_txn *txn = s->txn;
6936 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006937 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006938 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006939 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006940
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006941 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6942 return 0;
6943
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006944 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006945 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006946 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006947 /* Output closed while we were sending data. We must abort and
6948 * wake the other side up.
6949 */
6950 msg->msg_state = HTTP_MSG_ERROR;
6951 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006952 return 1;
6953 }
6954
Willy Tarreau4fe41902010-06-07 22:27:41 +02006955 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006956 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006957
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006958 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006959 /* we have msg->sov which points to the first byte of message
6960 * body, and res->buf.p still points to the beginning of the
6961 * message. We forward the headers now, as we don't need them
6962 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006963 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006964 b_adv(res->buf, msg->sov);
6965 msg->next -= msg->sov;
6966 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006967
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006968 /* The previous analysers guarantee that the state is somewhere
6969 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6970 * msg->next are always correct.
6971 */
6972 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6973 if (msg->flags & HTTP_MSGF_TE_CHNK)
6974 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6975 else
6976 msg->msg_state = HTTP_MSG_DATA;
6977 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006978 }
6979
Willy Tarreauefdf0942014-04-24 20:08:57 +02006980 if (res->to_forward) {
6981 /* We can't process the buffer's contents yet */
6982 res->flags |= CF_WAKE_WRITE;
6983 goto missing_data;
6984 }
6985
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006986 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6987 /* We need a compression buffer in the DATA state to put the
6988 * output of compressed data, and in CRLF state to let the
6989 * TRAILERS state finish the job of removing the trailing CRLF.
6990 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006991 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006992 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006993 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006994 goto aborted_xfer; /* no memory */
6995 }
6996
6997 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006998 if (ret < 0) {
6999 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02007000 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01007001 }
William Lallemand82fe75c2012-10-23 10:25:10 +02007002 compressing = 1;
7003 }
7004
Willy Tarreaud98cf932009-12-27 22:54:55 +01007005 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007006 switch (msg->msg_state - HTTP_MSG_DATA) {
7007 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02007008 /* we may have some pending data starting at res->buf->p */
7009 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02007010 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01007011 if (ret < 0)
7012 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02007013
Willy Tarreaud5a67832014-04-21 10:54:27 +02007014 if (msg->chunk_len) {
7015 /* input empty or output full */
7016 if (res->buf->i > msg->next)
7017 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02007018 goto missing_data;
7019 }
William Lallemandbf3ae612012-11-19 12:35:37 +01007020 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007021 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02007022 if (msg->chunk_len > res->buf->i - msg->next) {
7023 /* output full */
7024 res->flags |= CF_WAKE_WRITE;
7025 goto missing_data;
7026 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007027 msg->next += msg->chunk_len;
7028 msg->chunk_len = 0;
7029 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01007030
7031 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01007032 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02007033 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01007034 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01007035 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007036 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01007037 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007038 /* fall through for HTTP_MSG_CHUNK_CRLF */
7039
7040 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
7041 /* we want the CRLF after the data */
7042
7043 ret = http_skip_chunk_crlf(msg);
7044 if (ret == 0)
7045 goto missing_data;
7046 else if (ret < 0) {
7047 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007048 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007049 goto return_bad_res;
7050 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007051 /* we're in MSG_CHUNK_SIZE now, fall through */
7052
7053 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01007054 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02007055 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01007056 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01007057 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007058
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007059 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02007060 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01007061 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007062 else if (ret < 0) {
7063 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007064 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007065 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007066 }
Willy Tarreau0161d622013-04-02 01:26:55 +02007067 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007068 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01007069
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007070 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02007071 if (unlikely(compressing)) {
7072 /* we need to flush output contents before syncing FSMs */
7073 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7074 compressing = 0;
7075 }
7076
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007077 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007078 if (ret == 0)
7079 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007080 else if (ret < 0) {
7081 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007082 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007083 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007084 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02007085 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007086
7087 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01007088 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007089 if (unlikely(compressing)) {
7090 /* we need to flush output contents before syncing FSMs */
7091 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7092 compressing = 0;
7093 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007094
Willy Tarreauc623c172014-04-18 09:53:50 +02007095 /* we may have some pending data starting at res->buf->p
7096 * such as a last chunk of data or trailers.
7097 */
7098 b_adv(res->buf, msg->next);
7099 msg->next = 0;
7100
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007101 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02007102 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007103 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7104 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007105 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02007106
Willy Tarreau610ecce2010-01-04 21:15:02 +01007107 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01007108 /* some state changes occurred, maybe the analyser
7109 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01007110 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007111 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007112 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007113 /* response errors are most likely due to
7114 * the client aborting the transfer.
7115 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007116 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007117 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007118 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007119 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01007120 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007121 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007122 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01007123 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007124 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007125 }
7126 }
7127
Willy Tarreaud98cf932009-12-27 22:54:55 +01007128 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02007129 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007130 if (unlikely(compressing)) {
7131 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02007132 compressing = 0;
7133 }
Willy Tarreauf003d372012-11-26 13:35:37 +01007134
Willy Tarreauc623c172014-04-18 09:53:50 +02007135 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
7136 b_adv(res->buf, msg->next);
7137 msg->next = 0;
7138 msg->chunk_len -= channel_forward(res, msg->chunk_len);
7139 }
7140
Willy Tarreauf003d372012-11-26 13:35:37 +01007141 if (res->flags & CF_SHUTW)
7142 goto aborted_xfer;
7143
7144 /* stop waiting for data if the input is closed before the end. If the
7145 * client side was already closed, it means that the client has aborted,
7146 * so we don't want to count this as a server abort. Otherwise it's a
7147 * server abort.
7148 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007149 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007150 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01007151 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007152 if (!(s->flags & SF_ERR_MASK))
7153 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007154 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007155 if (objt_server(s->target))
7156 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007157 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01007158 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01007159
Willy Tarreau40dba092010-03-04 18:14:51 +01007160 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007161 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01007162 goto return_bad_res;
7163
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007164 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007165 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007166 * Similarly, with keep-alive on the client side, we don't want to forward a
7167 * close.
7168 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007169 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007170 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7171 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007172 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007173
Willy Tarreau5c620922011-05-11 19:56:11 +02007174 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007175 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007176 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007177 * modes are already handled by the stream sock layer. We must not do
7178 * this in content-length mode because it could present the MSG_MORE
7179 * flag with the last block of forwarded data, which would cause an
7180 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007181 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007182 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007183 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007184
Willy Tarreau87b09662015-04-03 00:22:06 +02007185 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007186 return 0;
7187
Willy Tarreau40dba092010-03-04 18:14:51 +01007188 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007189 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007190 if (objt_server(s->target))
7191 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007192
7193 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02007194 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02007195 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02007196 compressing = 0;
7197 }
7198
Willy Tarreauc623c172014-04-18 09:53:50 +02007199 /* we may have some pending data starting at res->buf->p */
7200 if (s->comp_algo == NULL) {
7201 b_adv(res->buf, msg->next);
7202 msg->next = 0;
7203 }
7204
Willy Tarreaud98cf932009-12-27 22:54:55 +01007205 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007206 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007207 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007208 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007209 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007210 if (objt_server(s->target))
7211 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007212
Willy Tarreaue7dff022015-04-03 01:14:29 +02007213 if (!(s->flags & SF_ERR_MASK))
7214 s->flags |= SF_ERR_PRXCOND;
7215 if (!(s->flags & SF_FINST_MASK))
7216 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007217 return 0;
7218
7219 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02007220 if (unlikely(compressing)) {
7221 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
7222 compressing = 0;
7223 }
7224
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007225 txn->rsp.msg_state = HTTP_MSG_ERROR;
7226 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007227 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007228 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007229 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007230
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007231 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007232 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007233 if (objt_server(s->target))
7234 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007235
Willy Tarreaue7dff022015-04-03 01:14:29 +02007236 if (!(s->flags & SF_ERR_MASK))
7237 s->flags |= SF_ERR_CLICL;
7238 if (!(s->flags & SF_FINST_MASK))
7239 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007240 return 0;
7241}
7242
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007243/* Iterate the same filter through all request headers.
7244 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007245 * Since it can manage the switch to another backend, it updates the per-proxy
7246 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007247 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007248int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007249{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007250 char *cur_ptr, *cur_end, *cur_next;
7251 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007252 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007253 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007254 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007255
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007256 last_hdr = 0;
7257
Willy Tarreau9b28e032012-10-12 23:49:43 +02007258 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007259 old_idx = 0;
7260
7261 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007262 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007263 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007264 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007265 (exp->action == ACT_ALLOW ||
7266 exp->action == ACT_DENY ||
7267 exp->action == ACT_TARPIT))
7268 return 0;
7269
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007270 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007271 if (!cur_idx)
7272 break;
7273
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007274 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007275 cur_ptr = cur_next;
7276 cur_end = cur_ptr + cur_hdr->len;
7277 cur_next = cur_end + cur_hdr->cr + 1;
7278
7279 /* Now we have one header between cur_ptr and cur_end,
7280 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007281 */
7282
Willy Tarreau15a53a42015-01-21 13:39:42 +01007283 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007284 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007285 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007286 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007287 last_hdr = 1;
7288 break;
7289
7290 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007291 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007292 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007293 break;
7294
7295 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007296 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007297 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007298 break;
7299
7300 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007301 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7302 if (trash.len < 0)
7303 return -1;
7304
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007305 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007306 /* FIXME: if the user adds a newline in the replacement, the
7307 * index will not be recalculated for now, and the new line
7308 * will not be counted as a new header.
7309 */
7310
7311 cur_end += delta;
7312 cur_next += delta;
7313 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007314 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007315 break;
7316
7317 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007318 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007319 cur_next += delta;
7320
Willy Tarreaufa355d42009-11-29 18:12:29 +01007321 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007322 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7323 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007324 cur_hdr->len = 0;
7325 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007326 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007327 break;
7328
7329 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007330 }
7331
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007332 /* keep the link from this header to next one in case of later
7333 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007334 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007335 old_idx = cur_idx;
7336 }
7337 return 0;
7338}
7339
7340
7341/* Apply the filter to the request line.
7342 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7343 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007344 * Since it can manage the switch to another backend, it updates the per-proxy
7345 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007346 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007347int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007348{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007349 char *cur_ptr, *cur_end;
7350 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007351 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007352 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007353
Willy Tarreau3d300592007-03-18 18:34:41 +01007354 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007355 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007356 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007357 (exp->action == ACT_ALLOW ||
7358 exp->action == ACT_DENY ||
7359 exp->action == ACT_TARPIT))
7360 return 0;
7361 else if (exp->action == ACT_REMOVE)
7362 return 0;
7363
7364 done = 0;
7365
Willy Tarreau9b28e032012-10-12 23:49:43 +02007366 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007367 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007368
7369 /* Now we have the request line between cur_ptr and cur_end */
7370
Willy Tarreau15a53a42015-01-21 13:39:42 +01007371 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007372 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007373 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007374 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007375 done = 1;
7376 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007377
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007378 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007379 txn->flags |= TX_CLDENY;
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_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007384 txn->flags |= TX_CLTARPIT;
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_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007389 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7390 if (trash.len < 0)
7391 return -1;
7392
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007393 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007394 /* FIXME: if the user adds a newline in the replacement, the
7395 * index will not be recalculated for now, and the new line
7396 * will not be counted as a new header.
7397 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007398
Willy Tarreaufa355d42009-11-29 18:12:29 +01007399 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007400 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007401 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007402 HTTP_MSG_RQMETH,
7403 cur_ptr, cur_end + 1,
7404 NULL, NULL);
7405 if (unlikely(!cur_end))
7406 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007407
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007408 /* we have a full request and we know that we have either a CR
7409 * or an LF at <ptr>.
7410 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007411 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7412 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007413 /* there is no point trying this regex on headers */
7414 return 1;
7415 }
7416 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007417 return done;
7418}
Willy Tarreau97de6242006-12-27 17:18:38 +01007419
Willy Tarreau58f10d72006-12-04 02:26:12 +01007420
Willy Tarreau58f10d72006-12-04 02:26:12 +01007421
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007422/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007423 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007424 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007425 * unparsable request. Since it can manage the switch to another backend, it
7426 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007427 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007428int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007429{
Willy Tarreau192252e2015-04-04 01:47:55 +02007430 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007431 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007432 struct hdr_exp *exp;
7433
7434 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007435 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007436
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007437 /*
7438 * The interleaving of transformations and verdicts
7439 * makes it difficult to decide to continue or stop
7440 * the evaluation.
7441 */
7442
Willy Tarreau6c123b12010-01-28 20:22:06 +01007443 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7444 break;
7445
Willy Tarreau3d300592007-03-18 18:34:41 +01007446 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007447 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007448 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007449 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007450
7451 /* if this filter had a condition, evaluate it now and skip to
7452 * next filter if the condition does not match.
7453 */
7454 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007455 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007456 ret = acl_pass(ret);
7457 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7458 ret = !ret;
7459
7460 if (!ret)
7461 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007462 }
7463
7464 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007465 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007466 if (unlikely(ret < 0))
7467 return -1;
7468
7469 if (likely(ret == 0)) {
7470 /* The filter did not match the request, it can be
7471 * iterated through all headers.
7472 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007473 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7474 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007475 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007476 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007477 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007478}
7479
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007480
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007481/* Find the end of a cookie value contained between <s> and <e>. It works the
7482 * same way as with headers above except that the semi-colon also ends a token.
7483 * See RFC2965 for more information. Note that it requires a valid header to
7484 * return a valid result.
7485 */
7486char *find_cookie_value_end(char *s, const char *e)
7487{
7488 int quoted, qdpair;
7489
7490 quoted = qdpair = 0;
7491 for (; s < e; s++) {
7492 if (qdpair) qdpair = 0;
7493 else if (quoted) {
7494 if (*s == '\\') qdpair = 1;
7495 else if (*s == '"') quoted = 0;
7496 }
7497 else if (*s == '"') quoted = 1;
7498 else if (*s == ',' || *s == ';') return s;
7499 }
7500 return s;
7501}
7502
7503/* Delete a value in a header between delimiters <from> and <next> in buffer
7504 * <buf>. The number of characters displaced is returned, and the pointer to
7505 * the first delimiter is updated if required. The function tries as much as
7506 * possible to respect the following principles :
7507 * - replace <from> delimiter by the <next> one unless <from> points to a
7508 * colon, in which case <next> is simply removed
7509 * - set exactly one space character after the new first delimiter, unless
7510 * there are not enough characters in the block being moved to do so.
7511 * - remove unneeded spaces before the previous delimiter and after the new
7512 * one.
7513 *
7514 * It is the caller's responsibility to ensure that :
7515 * - <from> points to a valid delimiter or the colon ;
7516 * - <next> points to a valid delimiter or the final CR/LF ;
7517 * - there are non-space chars before <from> ;
7518 * - there is a CR/LF at or after <next>.
7519 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007520int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007521{
7522 char *prev = *from;
7523
7524 if (*prev == ':') {
7525 /* We're removing the first value, preserve the colon and add a
7526 * space if possible.
7527 */
7528 if (!http_is_crlf[(unsigned char)*next])
7529 next++;
7530 prev++;
7531 if (prev < next)
7532 *prev++ = ' ';
7533
7534 while (http_is_spht[(unsigned char)*next])
7535 next++;
7536 } else {
7537 /* Remove useless spaces before the old delimiter. */
7538 while (http_is_spht[(unsigned char)*(prev-1)])
7539 prev--;
7540 *from = prev;
7541
7542 /* copy the delimiter and if possible a space if we're
7543 * not at the end of the line.
7544 */
7545 if (!http_is_crlf[(unsigned char)*next]) {
7546 *prev++ = *next++;
7547 if (prev + 1 < next)
7548 *prev++ = ' ';
7549 while (http_is_spht[(unsigned char)*next])
7550 next++;
7551 }
7552 }
7553 return buffer_replace2(buf, prev, next, NULL, 0);
7554}
7555
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007556/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007557 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007558 * desirable to call it only when needed. This code is quite complex because
7559 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7560 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007561 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007562void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007563{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007564 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007565 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007566 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007567 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007568 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7569 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007570
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007571 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007572 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007573 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007574
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007575 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007576 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007577 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007578
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007579 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007580 hdr_beg = hdr_next;
7581 hdr_end = hdr_beg + cur_hdr->len;
7582 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007583
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007584 /* We have one full header between hdr_beg and hdr_end, and the
7585 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007586 * "Cookie:" headers.
7587 */
7588
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007589 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007590 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007591 old_idx = cur_idx;
7592 continue;
7593 }
7594
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007595 del_from = NULL; /* nothing to be deleted */
7596 preserve_hdr = 0; /* assume we may kill the whole header */
7597
Willy Tarreau58f10d72006-12-04 02:26:12 +01007598 /* Now look for cookies. Conforming to RFC2109, we have to support
7599 * attributes whose name begin with a '$', and associate them with
7600 * the right cookie, if we want to delete this cookie.
7601 * So there are 3 cases for each cookie read :
7602 * 1) it's a special attribute, beginning with a '$' : ignore it.
7603 * 2) it's a server id cookie that we *MAY* want to delete : save
7604 * some pointers on it (last semi-colon, beginning of cookie...)
7605 * 3) it's an application cookie : we *MAY* have to delete a previous
7606 * "special" cookie.
7607 * At the end of loop, if a "special" cookie remains, we may have to
7608 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007609 * *MUST* delete it.
7610 *
7611 * Note: RFC2965 is unclear about the processing of spaces around
7612 * the equal sign in the ATTR=VALUE form. A careful inspection of
7613 * the RFC explicitly allows spaces before it, and not within the
7614 * tokens (attrs or values). An inspection of RFC2109 allows that
7615 * too but section 10.1.3 lets one think that spaces may be allowed
7616 * after the equal sign too, resulting in some (rare) buggy
7617 * implementations trying to do that. So let's do what servers do.
7618 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7619 * allowed quoted strings in values, with any possible character
7620 * after a backslash, including control chars and delimitors, which
7621 * causes parsing to become ambiguous. Browsers also allow spaces
7622 * within values even without quotes.
7623 *
7624 * We have to keep multiple pointers in order to support cookie
7625 * removal at the beginning, middle or end of header without
7626 * corrupting the header. All of these headers are valid :
7627 *
7628 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7629 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7630 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7631 * | | | | | | | | |
7632 * | | | | | | | | hdr_end <--+
7633 * | | | | | | | +--> next
7634 * | | | | | | +----> val_end
7635 * | | | | | +-----------> val_beg
7636 * | | | | +--------------> equal
7637 * | | | +----------------> att_end
7638 * | | +---------------------> att_beg
7639 * | +--------------------------> prev
7640 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007641 */
7642
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007643 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7644 /* Iterate through all cookies on this line */
7645
7646 /* find att_beg */
7647 att_beg = prev + 1;
7648 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7649 att_beg++;
7650
7651 /* find att_end : this is the first character after the last non
7652 * space before the equal. It may be equal to hdr_end.
7653 */
7654 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007655
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007656 while (equal < hdr_end) {
7657 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007658 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007659 if (http_is_spht[(unsigned char)*equal++])
7660 continue;
7661 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007662 }
7663
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007664 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7665 * is between <att_beg> and <equal>, both may be identical.
7666 */
7667
7668 /* look for end of cookie if there is an equal sign */
7669 if (equal < hdr_end && *equal == '=') {
7670 /* look for the beginning of the value */
7671 val_beg = equal + 1;
7672 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7673 val_beg++;
7674
7675 /* find the end of the value, respecting quotes */
7676 next = find_cookie_value_end(val_beg, hdr_end);
7677
7678 /* make val_end point to the first white space or delimitor after the value */
7679 val_end = next;
7680 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7681 val_end--;
7682 } else {
7683 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007684 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007685
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007686 /* We have nothing to do with attributes beginning with '$'. However,
7687 * they will automatically be removed if a header before them is removed,
7688 * since they're supposed to be linked together.
7689 */
7690 if (*att_beg == '$')
7691 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007692
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007693 /* Ignore cookies with no equal sign */
7694 if (equal == next) {
7695 /* This is not our cookie, so we must preserve it. But if we already
7696 * scheduled another cookie for removal, we cannot remove the
7697 * complete header, but we can remove the previous block itself.
7698 */
7699 preserve_hdr = 1;
7700 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007701 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007702 val_end += delta;
7703 next += delta;
7704 hdr_end += delta;
7705 hdr_next += delta;
7706 cur_hdr->len += delta;
7707 http_msg_move_end(&txn->req, delta);
7708 prev = del_from;
7709 del_from = NULL;
7710 }
7711 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007712 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007713
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007714 /* if there are spaces around the equal sign, we need to
7715 * strip them otherwise we'll get trouble for cookie captures,
7716 * or even for rewrites. Since this happens extremely rarely,
7717 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007718 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007719 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7720 int stripped_before = 0;
7721 int stripped_after = 0;
7722
7723 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007724 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007725 equal += stripped_before;
7726 val_beg += stripped_before;
7727 }
7728
7729 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007730 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007731 val_beg += stripped_after;
7732 stripped_before += stripped_after;
7733 }
7734
7735 val_end += stripped_before;
7736 next += stripped_before;
7737 hdr_end += stripped_before;
7738 hdr_next += stripped_before;
7739 cur_hdr->len += stripped_before;
7740 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007741 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007742 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007743
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007744 /* First, let's see if we want to capture this cookie. We check
7745 * that we don't already have a client side cookie, because we
7746 * can only capture one. Also as an optimisation, we ignore
7747 * cookies shorter than the declared name.
7748 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007749 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7750 (val_end - att_beg >= sess->fe->capture_namelen) &&
7751 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007752 int log_len = val_end - att_beg;
7753
7754 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7755 Alert("HTTP logging : out of memory.\n");
7756 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007757 if (log_len > sess->fe->capture_len)
7758 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007759 memcpy(txn->cli_cookie, att_beg, log_len);
7760 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007761 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007762 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007763
Willy Tarreaubca99692010-10-06 19:25:55 +02007764 /* Persistence cookies in passive, rewrite or insert mode have the
7765 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007766 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007767 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007768 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007769 * For cookies in prefix mode, the form is :
7770 *
7771 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007772 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007773 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7774 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7775 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007776 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007777
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007778 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7779 * have the server ID between val_beg and delim, and the original cookie between
7780 * delim+1 and val_end. Otherwise, delim==val_end :
7781 *
7782 * Cookie: NAME=SRV; # in all but prefix modes
7783 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7784 * | || || | |+-> next
7785 * | || || | +--> val_end
7786 * | || || +---------> delim
7787 * | || |+------------> val_beg
7788 * | || +-------------> att_end = equal
7789 * | |+-----------------> att_beg
7790 * | +------------------> prev
7791 * +-------------------------> hdr_beg
7792 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007793
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007794 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007795 for (delim = val_beg; delim < val_end; delim++)
7796 if (*delim == COOKIE_DELIM)
7797 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007798 } else {
7799 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007800 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007801 /* Now check if the cookie contains a date field, which would
7802 * appear after a vertical bar ('|') just after the server name
7803 * and before the delimiter.
7804 */
7805 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7806 if (vbar1) {
7807 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007808 * right is the last seen date. It is a base64 encoded
7809 * 30-bit value representing the UNIX date since the
7810 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007811 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007812 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007813 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007814 if (val_end - vbar1 >= 5) {
7815 val = b64tos30(vbar1);
7816 if (val > 0)
7817 txn->cookie_last_date = val << 2;
7818 }
7819 /* look for a second vertical bar */
7820 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7821 if (vbar1 && (val_end - vbar1 > 5)) {
7822 val = b64tos30(vbar1 + 1);
7823 if (val > 0)
7824 txn->cookie_first_date = val << 2;
7825 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007826 }
7827 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007828
Willy Tarreauf64d1412010-10-07 20:06:11 +02007829 /* if the cookie has an expiration date and the proxy wants to check
7830 * it, then we do that now. We first check if the cookie is too old,
7831 * then only if it has expired. We detect strict overflow because the
7832 * time resolution here is not great (4 seconds). Cookies with dates
7833 * in the future are ignored if their offset is beyond one day. This
7834 * allows an admin to fix timezone issues without expiring everyone
7835 * and at the same time avoids keeping unwanted side effects for too
7836 * long.
7837 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007838 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7839 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007840 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007841 txn->flags &= ~TX_CK_MASK;
7842 txn->flags |= TX_CK_OLD;
7843 delim = val_beg; // let's pretend we have not found the cookie
7844 txn->cookie_first_date = 0;
7845 txn->cookie_last_date = 0;
7846 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007847 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7848 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007849 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007850 txn->flags &= ~TX_CK_MASK;
7851 txn->flags |= TX_CK_EXPIRED;
7852 delim = val_beg; // let's pretend we have not found the cookie
7853 txn->cookie_first_date = 0;
7854 txn->cookie_last_date = 0;
7855 }
7856
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007857 /* Here, we'll look for the first running server which supports the cookie.
7858 * This allows to share a same cookie between several servers, for example
7859 * to dedicate backup servers to specific servers only.
7860 * However, to prevent clients from sticking to cookie-less backup server
7861 * when they have incidentely learned an empty cookie, we simply ignore
7862 * empty cookies and mark them as invalid.
7863 * The same behaviour is applied when persistence must be ignored.
7864 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007865 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007866 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007867
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007868 while (srv) {
7869 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7870 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007871 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007872 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007873 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007874 /* we found the server and we can use it */
7875 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007876 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007877 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007878 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007879 break;
7880 } else {
7881 /* we found a server, but it's down,
7882 * mark it as such and go on in case
7883 * another one is available.
7884 */
7885 txn->flags &= ~TX_CK_MASK;
7886 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007887 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007888 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007889 srv = srv->next;
7890 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007891
Willy Tarreauf64d1412010-10-07 20:06:11 +02007892 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007893 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007894 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007895 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007896 txn->flags |= TX_CK_UNUSED;
7897 else
7898 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007899 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007900
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007901 /* depending on the cookie mode, we may have to either :
7902 * - delete the complete cookie if we're in insert+indirect mode, so that
7903 * the server never sees it ;
7904 * - remove the server id from the cookie value, and tag the cookie as an
7905 * application cookie so that it does not get accidentely removed later,
7906 * if we're in cookie prefix mode
7907 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007908 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007909 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007910
Willy Tarreau9b28e032012-10-12 23:49:43 +02007911 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007912 val_end += delta;
7913 next += delta;
7914 hdr_end += delta;
7915 hdr_next += delta;
7916 cur_hdr->len += delta;
7917 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007918
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007919 del_from = NULL;
7920 preserve_hdr = 1; /* we want to keep this cookie */
7921 }
7922 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007923 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007924 del_from = prev;
7925 }
7926 } else {
7927 /* This is not our cookie, so we must preserve it. But if we already
7928 * scheduled another cookie for removal, we cannot remove the
7929 * complete header, but we can remove the previous block itself.
7930 */
7931 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007932
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007933 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007934 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007935 if (att_beg >= del_from)
7936 att_beg += delta;
7937 if (att_end >= del_from)
7938 att_end += delta;
7939 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007940 val_end += delta;
7941 next += delta;
7942 hdr_end += delta;
7943 hdr_next += delta;
7944 cur_hdr->len += delta;
7945 http_msg_move_end(&txn->req, delta);
7946 prev = del_from;
7947 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007948 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007949 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007950
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007951 /* continue with next cookie on this header line */
7952 att_beg = next;
7953 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007954
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007955 /* There are no more cookies on this line.
7956 * We may still have one (or several) marked for deletion at the
7957 * end of the line. We must do this now in two ways :
7958 * - if some cookies must be preserved, we only delete from the
7959 * mark to the end of line ;
7960 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007961 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007962 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007963 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007964 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007965 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007966 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007967 cur_hdr->len += delta;
7968 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007969 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007970
7971 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007972 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7973 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007974 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007975 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007976 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007977 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007978 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007979 }
7980
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007981 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007982 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007983 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007984}
7985
7986
Willy Tarreaua15645d2007-03-18 16:22:39 +01007987/* Iterate the same filter through all response headers contained in <rtr>.
7988 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7989 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007990int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007991{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007992 char *cur_ptr, *cur_end, *cur_next;
7993 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007994 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007995 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007996 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007997
7998 last_hdr = 0;
7999
Willy Tarreau9b28e032012-10-12 23:49:43 +02008000 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008001 old_idx = 0;
8002
8003 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008004 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008005 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008006 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008007 (exp->action == ACT_ALLOW ||
8008 exp->action == ACT_DENY))
8009 return 0;
8010
8011 cur_idx = txn->hdr_idx.v[old_idx].next;
8012 if (!cur_idx)
8013 break;
8014
8015 cur_hdr = &txn->hdr_idx.v[cur_idx];
8016 cur_ptr = cur_next;
8017 cur_end = cur_ptr + cur_hdr->len;
8018 cur_next = cur_end + cur_hdr->cr + 1;
8019
8020 /* Now we have one header between cur_ptr and cur_end,
8021 * and the next header starts at cur_next.
8022 */
8023
Willy Tarreau15a53a42015-01-21 13:39:42 +01008024 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008025 switch (exp->action) {
8026 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008027 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008028 last_hdr = 1;
8029 break;
8030
8031 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008032 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008033 last_hdr = 1;
8034 break;
8035
8036 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008037 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8038 if (trash.len < 0)
8039 return -1;
8040
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008041 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008042 /* FIXME: if the user adds a newline in the replacement, the
8043 * index will not be recalculated for now, and the new line
8044 * will not be counted as a new header.
8045 */
8046
8047 cur_end += delta;
8048 cur_next += delta;
8049 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008050 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008051 break;
8052
8053 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008054 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008055 cur_next += delta;
8056
Willy Tarreaufa355d42009-11-29 18:12:29 +01008057 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008058 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8059 txn->hdr_idx.used--;
8060 cur_hdr->len = 0;
8061 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008062 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008063 break;
8064
8065 }
8066 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008067
8068 /* keep the link from this header to next one in case of later
8069 * removal of next header.
8070 */
8071 old_idx = cur_idx;
8072 }
8073 return 0;
8074}
8075
8076
8077/* Apply the filter to the status line in the response buffer <rtr>.
8078 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8079 * or -1 if a replacement resulted in an invalid status line.
8080 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008081int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008082{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008083 char *cur_ptr, *cur_end;
8084 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008085 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008086 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008087
8088
Willy Tarreau3d300592007-03-18 18:34:41 +01008089 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008090 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008091 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008092 (exp->action == ACT_ALLOW ||
8093 exp->action == ACT_DENY))
8094 return 0;
8095 else if (exp->action == ACT_REMOVE)
8096 return 0;
8097
8098 done = 0;
8099
Willy Tarreau9b28e032012-10-12 23:49:43 +02008100 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008101 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008102
8103 /* Now we have the status line between cur_ptr and cur_end */
8104
Willy Tarreau15a53a42015-01-21 13:39:42 +01008105 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008106 switch (exp->action) {
8107 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008108 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008109 done = 1;
8110 break;
8111
8112 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008113 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008114 done = 1;
8115 break;
8116
8117 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008118 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8119 if (trash.len < 0)
8120 return -1;
8121
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008122 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008123 /* FIXME: if the user adds a newline in the replacement, the
8124 * index will not be recalculated for now, and the new line
8125 * will not be counted as a new header.
8126 */
8127
Willy Tarreaufa355d42009-11-29 18:12:29 +01008128 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008129 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008130 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008131 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008132 cur_ptr, cur_end + 1,
8133 NULL, NULL);
8134 if (unlikely(!cur_end))
8135 return -1;
8136
8137 /* we have a full respnse and we know that we have either a CR
8138 * or an LF at <ptr>.
8139 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008140 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008141 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008142 /* there is no point trying this regex on headers */
8143 return 1;
8144 }
8145 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008146 return done;
8147}
8148
8149
8150
8151/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008152 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008153 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8154 * unparsable response.
8155 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008156int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008157{
Willy Tarreau192252e2015-04-04 01:47:55 +02008158 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008159 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008160 struct hdr_exp *exp;
8161
8162 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008163 int ret;
8164
8165 /*
8166 * The interleaving of transformations and verdicts
8167 * makes it difficult to decide to continue or stop
8168 * the evaluation.
8169 */
8170
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008171 if (txn->flags & TX_SVDENY)
8172 break;
8173
Willy Tarreau3d300592007-03-18 18:34:41 +01008174 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008175 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8176 exp->action == ACT_PASS)) {
8177 exp = exp->next;
8178 continue;
8179 }
8180
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008181 /* if this filter had a condition, evaluate it now and skip to
8182 * next filter if the condition does not match.
8183 */
8184 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008185 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008186 ret = acl_pass(ret);
8187 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8188 ret = !ret;
8189 if (!ret)
8190 continue;
8191 }
8192
Willy Tarreaua15645d2007-03-18 16:22:39 +01008193 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008194 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008195 if (unlikely(ret < 0))
8196 return -1;
8197
8198 if (likely(ret == 0)) {
8199 /* The filter did not match the response, it can be
8200 * iterated through all headers.
8201 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008202 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8203 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008204 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008205 }
8206 return 0;
8207}
8208
8209
Willy Tarreaua15645d2007-03-18 16:22:39 +01008210/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008211 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008212 * desirable to call it only when needed. This function is also used when we
8213 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008214 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008215void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008216{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008217 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008218 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008219 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008220 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008221 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008222 char *hdr_beg, *hdr_end, *hdr_next;
8223 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008224
Willy Tarreaua15645d2007-03-18 16:22:39 +01008225 /* Iterate through the headers.
8226 * we start with the start line.
8227 */
8228 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008229 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008230
8231 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8232 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008233 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008234
8235 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008236 hdr_beg = hdr_next;
8237 hdr_end = hdr_beg + cur_hdr->len;
8238 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008239
Willy Tarreau24581ba2010-08-31 22:39:35 +02008240 /* We have one full header between hdr_beg and hdr_end, and the
8241 * next header starts at hdr_next. We're only interested in
8242 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008243 */
8244
Willy Tarreau24581ba2010-08-31 22:39:35 +02008245 is_cookie2 = 0;
8246 prev = hdr_beg + 10;
8247 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008248 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008249 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8250 if (!val) {
8251 old_idx = cur_idx;
8252 continue;
8253 }
8254 is_cookie2 = 1;
8255 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008256 }
8257
Willy Tarreau24581ba2010-08-31 22:39:35 +02008258 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8259 * <prev> points to the colon.
8260 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008261 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008262
Willy Tarreau24581ba2010-08-31 22:39:35 +02008263 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8264 * check-cache is enabled) and we are not interested in checking
8265 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008266 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008267 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008268 return;
8269
Willy Tarreau24581ba2010-08-31 22:39:35 +02008270 /* OK so now we know we have to process this response cookie.
8271 * The format of the Set-Cookie header is slightly different
8272 * from the format of the Cookie header in that it does not
8273 * support the comma as a cookie delimiter (thus the header
8274 * cannot be folded) because the Expires attribute described in
8275 * the original Netscape's spec may contain an unquoted date
8276 * with a comma inside. We have to live with this because
8277 * many browsers don't support Max-Age and some browsers don't
8278 * support quoted strings. However the Set-Cookie2 header is
8279 * clean.
8280 *
8281 * We have to keep multiple pointers in order to support cookie
8282 * removal at the beginning, middle or end of header without
8283 * corrupting the header (in case of set-cookie2). A special
8284 * pointer, <scav> points to the beginning of the set-cookie-av
8285 * fields after the first semi-colon. The <next> pointer points
8286 * either to the end of line (set-cookie) or next unquoted comma
8287 * (set-cookie2). All of these headers are valid :
8288 *
8289 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8290 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8291 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8292 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8293 * | | | | | | | | | |
8294 * | | | | | | | | +-> next hdr_end <--+
8295 * | | | | | | | +------------> scav
8296 * | | | | | | +--------------> val_end
8297 * | | | | | +--------------------> val_beg
8298 * | | | | +----------------------> equal
8299 * | | | +------------------------> att_end
8300 * | | +----------------------------> att_beg
8301 * | +------------------------------> prev
8302 * +-----------------------------------------> hdr_beg
8303 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008304
Willy Tarreau24581ba2010-08-31 22:39:35 +02008305 for (; prev < hdr_end; prev = next) {
8306 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008307
Willy Tarreau24581ba2010-08-31 22:39:35 +02008308 /* find att_beg */
8309 att_beg = prev + 1;
8310 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8311 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008312
Willy Tarreau24581ba2010-08-31 22:39:35 +02008313 /* find att_end : this is the first character after the last non
8314 * space before the equal. It may be equal to hdr_end.
8315 */
8316 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008317
Willy Tarreau24581ba2010-08-31 22:39:35 +02008318 while (equal < hdr_end) {
8319 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8320 break;
8321 if (http_is_spht[(unsigned char)*equal++])
8322 continue;
8323 att_end = equal;
8324 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008325
Willy Tarreau24581ba2010-08-31 22:39:35 +02008326 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8327 * is between <att_beg> and <equal>, both may be identical.
8328 */
8329
8330 /* look for end of cookie if there is an equal sign */
8331 if (equal < hdr_end && *equal == '=') {
8332 /* look for the beginning of the value */
8333 val_beg = equal + 1;
8334 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8335 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008336
Willy Tarreau24581ba2010-08-31 22:39:35 +02008337 /* find the end of the value, respecting quotes */
8338 next = find_cookie_value_end(val_beg, hdr_end);
8339
8340 /* make val_end point to the first white space or delimitor after the value */
8341 val_end = next;
8342 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8343 val_end--;
8344 } else {
8345 /* <equal> points to next comma, semi-colon or EOL */
8346 val_beg = val_end = next = equal;
8347 }
8348
8349 if (next < hdr_end) {
8350 /* Set-Cookie2 supports multiple cookies, and <next> points to
8351 * a colon or semi-colon before the end. So skip all attr-value
8352 * pairs and look for the next comma. For Set-Cookie, since
8353 * commas are permitted in values, skip to the end.
8354 */
8355 if (is_cookie2)
8356 next = find_hdr_value_end(next, hdr_end);
8357 else
8358 next = hdr_end;
8359 }
8360
8361 /* Now everything is as on the diagram above */
8362
8363 /* Ignore cookies with no equal sign */
8364 if (equal == val_end)
8365 continue;
8366
8367 /* If there are spaces around the equal sign, we need to
8368 * strip them otherwise we'll get trouble for cookie captures,
8369 * or even for rewrites. Since this happens extremely rarely,
8370 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008371 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008372 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8373 int stripped_before = 0;
8374 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008375
Willy Tarreau24581ba2010-08-31 22:39:35 +02008376 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008377 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008378 equal += stripped_before;
8379 val_beg += stripped_before;
8380 }
8381
8382 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008383 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008384 val_beg += stripped_after;
8385 stripped_before += stripped_after;
8386 }
8387
8388 val_end += stripped_before;
8389 next += stripped_before;
8390 hdr_end += stripped_before;
8391 hdr_next += stripped_before;
8392 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008393 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008394 }
8395
8396 /* First, let's see if we want to capture this cookie. We check
8397 * that we don't already have a server side cookie, because we
8398 * can only capture one. Also as an optimisation, we ignore
8399 * cookies shorter than the declared name.
8400 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008401 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008402 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008403 (val_end - att_beg >= sess->fe->capture_namelen) &&
8404 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008405 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008406 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008407 Alert("HTTP logging : out of memory.\n");
8408 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008409 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008410 if (log_len > sess->fe->capture_len)
8411 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008412 memcpy(txn->srv_cookie, att_beg, log_len);
8413 txn->srv_cookie[log_len] = 0;
8414 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008415 }
8416
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008417 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008418 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008419 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008420 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8421 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008422 /* assume passive cookie by default */
8423 txn->flags &= ~TX_SCK_MASK;
8424 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008425
8426 /* If the cookie is in insert mode on a known server, we'll delete
8427 * this occurrence because we'll insert another one later.
8428 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008429 * a direct access.
8430 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008431 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008432 /* The "preserve" flag was set, we don't want to touch the
8433 * server's cookie.
8434 */
8435 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008436 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008437 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008438 /* this cookie must be deleted */
8439 if (*prev == ':' && next == hdr_end) {
8440 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008441 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008442 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8443 txn->hdr_idx.used--;
8444 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008445 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008446 hdr_next += delta;
8447 http_msg_move_end(&txn->rsp, delta);
8448 /* note: while both invalid now, <next> and <hdr_end>
8449 * are still equal, so the for() will stop as expected.
8450 */
8451 } else {
8452 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008453 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008454 next = prev;
8455 hdr_end += delta;
8456 hdr_next += delta;
8457 cur_hdr->len += delta;
8458 http_msg_move_end(&txn->rsp, delta);
8459 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008460 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008461 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008462 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008463 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008464 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008465 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008466 * with this server since we know it.
8467 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008468 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008469 next += delta;
8470 hdr_end += delta;
8471 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008472 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008473 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008474
Willy Tarreauf1348312010-10-07 15:54:11 +02008475 txn->flags &= ~TX_SCK_MASK;
8476 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008477 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008478 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008479 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008480 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008481 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008482 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008483 next += delta;
8484 hdr_end += delta;
8485 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008486 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008487 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008488
Willy Tarreau827aee92011-03-10 16:55:02 +01008489 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008490 txn->flags &= ~TX_SCK_MASK;
8491 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008492 }
8493 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008494 /* that's done for this cookie, check the next one on the same
8495 * line when next != hdr_end (only if is_cookie2).
8496 */
8497 }
8498 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008499 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008500 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008501}
8502
8503
Willy Tarreaua15645d2007-03-18 16:22:39 +01008504/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008505 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008506 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008507void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008508{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008509 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008510 char *p1, *p2;
8511
8512 char *cur_ptr, *cur_end, *cur_next;
8513 int cur_idx;
8514
Willy Tarreau5df51872007-11-25 16:20:08 +01008515 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008516 return;
8517
8518 /* Iterate through the headers.
8519 * we start with the start line.
8520 */
8521 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008522 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008523
8524 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8525 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008526 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008527
8528 cur_hdr = &txn->hdr_idx.v[cur_idx];
8529 cur_ptr = cur_next;
8530 cur_end = cur_ptr + cur_hdr->len;
8531 cur_next = cur_end + cur_hdr->cr + 1;
8532
8533 /* We have one full header between cur_ptr and cur_end, and the
8534 * next header starts at cur_next. We're only interested in
8535 * "Cookie:" headers.
8536 */
8537
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008538 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8539 if (val) {
8540 if ((cur_end - (cur_ptr + val) >= 8) &&
8541 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8542 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8543 return;
8544 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008545 }
8546
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008547 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8548 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008549 continue;
8550
8551 /* OK, right now we know we have a cache-control header at cur_ptr */
8552
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008553 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008554
8555 if (p1 >= cur_end) /* no more info */
8556 continue;
8557
8558 /* p1 is at the beginning of the value */
8559 p2 = p1;
8560
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008561 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008562 p2++;
8563
8564 /* we have a complete value between p1 and p2 */
8565 if (p2 < cur_end && *p2 == '=') {
8566 /* we have something of the form no-cache="set-cookie" */
8567 if ((cur_end - p1 >= 21) &&
8568 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8569 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008570 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008571 continue;
8572 }
8573
8574 /* OK, so we know that either p2 points to the end of string or to a comma */
8575 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008576 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008577 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8578 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8579 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008580 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008581 return;
8582 }
8583
8584 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008585 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008586 continue;
8587 }
8588 }
8589}
8590
Willy Tarreau58f10d72006-12-04 02:26:12 +01008591
Willy Tarreaub2513902006-12-17 14:52:38 +01008592/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008593 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008594 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008595 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008596 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008597 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008598 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008599 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008600 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008601int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008602{
8603 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008604 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008605 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008606
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008607 if (!uri_auth)
8608 return 0;
8609
Cyril Bonté70be45d2010-10-12 00:14:35 +02008610 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008611 return 0;
8612
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008613 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008614 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008615 return 0;
8616
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008617 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008618 return 0;
8619
Willy Tarreaub2513902006-12-17 14:52:38 +01008620 return 1;
8621}
8622
Willy Tarreau4076a152009-04-02 15:18:36 +02008623/*
8624 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008625 * By default it tries to report the error position as msg->err_pos. However if
8626 * this one is not set, it will then report msg->next, which is the last known
8627 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008628 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008629 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008630void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008631 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008632 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008633{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008634 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008635 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008636 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008637
Willy Tarreau9b28e032012-10-12 23:49:43 +02008638 es->len = MIN(chn->buf->i, sizeof(es->buf));
8639 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008640 len1 = MIN(len1, es->len);
8641 len2 = es->len - len1; /* remaining data if buffer wraps */
8642
Willy Tarreau9b28e032012-10-12 23:49:43 +02008643 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008644 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008645 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008646
Willy Tarreau4076a152009-04-02 15:18:36 +02008647 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008648 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008649 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008650 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008651
Willy Tarreau4076a152009-04-02 15:18:36 +02008652 es->when = date; // user-visible date
8653 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008654 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008655 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008656 if (objt_conn(sess->origin))
8657 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008658 else
8659 memset(&es->src, 0, sizeof(es->src));
8660
Willy Tarreau078272e2010-12-12 12:46:33 +01008661 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008662 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008663 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008664 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008665 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008666 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008667 es->b_out = chn->buf->o;
8668 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008669 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008670 es->m_clen = msg->chunk_len;
8671 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008672}
Willy Tarreaub2513902006-12-17 14:52:38 +01008673
Willy Tarreau294c4732011-12-16 21:35:50 +01008674/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8675 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8676 * performed over the whole headers. Otherwise it must contain a valid header
8677 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8678 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8679 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8680 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008681 * -1. The value fetch stops at commas, so this function is suited for use with
8682 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008683 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008684 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008685unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008686 struct hdr_idx *idx, int occ,
8687 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008688{
Willy Tarreau294c4732011-12-16 21:35:50 +01008689 struct hdr_ctx local_ctx;
8690 char *ptr_hist[MAX_HDR_HISTORY];
8691 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008692 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008693 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008694
Willy Tarreau294c4732011-12-16 21:35:50 +01008695 if (!ctx) {
8696 local_ctx.idx = 0;
8697 ctx = &local_ctx;
8698 }
8699
Willy Tarreaubce70882009-09-07 11:51:47 +02008700 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008701 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008702 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008703 occ--;
8704 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008705 *vptr = ctx->line + ctx->val;
8706 *vlen = ctx->vlen;
8707 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008708 }
8709 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008710 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008711 }
8712
8713 /* negative occurrence, we scan all the list then walk back */
8714 if (-occ > MAX_HDR_HISTORY)
8715 return 0;
8716
Willy Tarreau294c4732011-12-16 21:35:50 +01008717 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008718 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008719 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8720 len_hist[hist_ptr] = ctx->vlen;
8721 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008722 hist_ptr = 0;
8723 found++;
8724 }
8725 if (-occ > found)
8726 return 0;
8727 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008728 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8729 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8730 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008731 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008732 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008733 if (hist_ptr >= MAX_HDR_HISTORY)
8734 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008735 *vptr = ptr_hist[hist_ptr];
8736 *vlen = len_hist[hist_ptr];
8737 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008738}
8739
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008740/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8741 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8742 * performed over the whole headers. Otherwise it must contain a valid header
8743 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8744 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8745 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8746 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8747 * -1. This function differs from http_get_hdr() in that it only returns full
8748 * line header values and does not stop at commas.
8749 * The return value is 0 if nothing was found, or non-zero otherwise.
8750 */
8751unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8752 struct hdr_idx *idx, int occ,
8753 struct hdr_ctx *ctx, char **vptr, int *vlen)
8754{
8755 struct hdr_ctx local_ctx;
8756 char *ptr_hist[MAX_HDR_HISTORY];
8757 int len_hist[MAX_HDR_HISTORY];
8758 unsigned int hist_ptr;
8759 int found;
8760
8761 if (!ctx) {
8762 local_ctx.idx = 0;
8763 ctx = &local_ctx;
8764 }
8765
8766 if (occ >= 0) {
8767 /* search from the beginning */
8768 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8769 occ--;
8770 if (occ <= 0) {
8771 *vptr = ctx->line + ctx->val;
8772 *vlen = ctx->vlen;
8773 return 1;
8774 }
8775 }
8776 return 0;
8777 }
8778
8779 /* negative occurrence, we scan all the list then walk back */
8780 if (-occ > MAX_HDR_HISTORY)
8781 return 0;
8782
8783 found = hist_ptr = 0;
8784 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8785 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8786 len_hist[hist_ptr] = ctx->vlen;
8787 if (++hist_ptr >= MAX_HDR_HISTORY)
8788 hist_ptr = 0;
8789 found++;
8790 }
8791 if (-occ > found)
8792 return 0;
8793 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8794 * find occurrence -occ, so we have to check [hist_ptr+occ].
8795 */
8796 hist_ptr += occ;
8797 if (hist_ptr >= MAX_HDR_HISTORY)
8798 hist_ptr -= MAX_HDR_HISTORY;
8799 *vptr = ptr_hist[hist_ptr];
8800 *vlen = len_hist[hist_ptr];
8801 return 1;
8802}
8803
Willy Tarreaubaaee002006-06-26 02:48:02 +02008804/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008805 * Print a debug line with a header. Always stop at the first CR or LF char,
8806 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8807 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008808 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008809void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008810{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008811 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008812 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008813
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008814 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008815 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008816 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008817 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 +02008818
8819 for (max = 0; start + max < end; max++)
8820 if (start[max] == '\r' || start[max] == '\n')
8821 break;
8822
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008823 UBOUND(max, trash.size - trash.len - 3);
8824 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8825 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008826 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008827}
8828
Willy Tarreaueee5b512015-04-03 23:46:31 +02008829
8830/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8831 * The hdr_idx is allocated as well. In case of allocation failure, everything
8832 * allocated is freed and NULL is returned. Otherwise the new transaction is
8833 * assigned to the stream and returned.
8834 */
8835struct http_txn *http_alloc_txn(struct stream *s)
8836{
8837 struct http_txn *txn = s->txn;
8838
8839 if (txn)
8840 return txn;
8841
8842 txn = pool_alloc2(pool2_http_txn);
8843 if (!txn)
8844 return txn;
8845
8846 txn->hdr_idx.size = global.tune.max_http_hdr;
8847 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8848 if (!txn->hdr_idx.v) {
8849 pool_free2(pool2_http_txn, txn);
8850 return NULL;
8851 }
8852
8853 s->txn = txn;
8854 return txn;
8855}
8856
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008857void http_txn_reset_req(struct http_txn *txn)
8858{
8859 txn->req.flags = 0;
8860 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8861 txn->req.next = 0;
8862 txn->req.chunk_len = 0LL;
8863 txn->req.body_len = 0LL;
8864 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8865}
8866
8867void http_txn_reset_res(struct http_txn *txn)
8868{
8869 txn->rsp.flags = 0;
8870 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8871 txn->rsp.next = 0;
8872 txn->rsp.chunk_len = 0LL;
8873 txn->rsp.body_len = 0LL;
8874 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8875}
8876
Willy Tarreau0937bc42009-12-22 15:03:09 +01008877/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008878 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008879 * the required fields are properly allocated and that we only need to (re)init
8880 * them. This should be used before processing any new request.
8881 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008882void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008883{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008884 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008885 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008886
8887 txn->flags = 0;
8888 txn->status = -1;
8889
Willy Tarreauf64d1412010-10-07 20:06:11 +02008890 txn->cookie_first_date = 0;
8891 txn->cookie_last_date = 0;
8892
Willy Tarreaueee5b512015-04-03 23:46:31 +02008893 txn->srv_cookie = NULL;
8894 txn->cli_cookie = NULL;
8895 txn->uri = NULL;
8896
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008897 http_txn_reset_req(txn);
8898 http_txn_reset_res(txn);
8899
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008900 txn->req.chn = &s->req;
8901 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008902
8903 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008904
8905 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8906 if (fe->options2 & PR_O2_REQBUG_OK)
8907 txn->req.err_pos = -1; /* let buggy requests pass */
8908
Willy Tarreau0937bc42009-12-22 15:03:09 +01008909 if (txn->hdr_idx.v)
8910 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008911
8912 vars_init(&s->vars_txn, SCOPE_TXN);
8913 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008914}
8915
8916/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008917void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008918{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008919 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008920 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008921
Willy Tarreau75195602014-03-11 15:48:55 +01008922 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008923 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008924 s->comp_algo->end(&s->comp_ctx);
8925 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008926 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008927
Willy Tarreau0937bc42009-12-22 15:03:09 +01008928 /* these ones will have been dynamically allocated */
8929 pool_free2(pool2_requri, txn->uri);
8930 pool_free2(pool2_capture, txn->cli_cookie);
8931 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008932 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008933
William Lallemanda73203e2012-03-12 12:48:57 +01008934 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008935 txn->uri = NULL;
8936 txn->srv_cookie = NULL;
8937 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008938
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008939 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008940 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008941 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008942 pool_free2(h->pool, s->req_cap[h->index]);
8943 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008944 }
8945
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008946 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008947 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008948 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008949 pool_free2(h->pool, s->res_cap[h->index]);
8950 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008951 }
8952
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008953 vars_prune(&s->vars_txn, s);
8954 vars_prune(&s->vars_reqres, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008955}
8956
8957/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008958void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008959{
8960 http_end_txn(s);
8961 http_init_txn(s);
8962
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008963 /* reinitialise the current rule list pointer to NULL. We are sure that
8964 * any rulelist match the NULL pointer.
8965 */
8966 s->current_rule_list = NULL;
8967
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008968 s->be = strm_fe(s);
8969 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008970 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008971 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008972 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008973 /* re-init store persistence */
8974 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008975 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008976
Willy Tarreau0937bc42009-12-22 15:03:09 +01008977 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008978
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008979 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008980
Willy Tarreau739cfba2010-01-25 23:11:14 +01008981 /* We must trim any excess data from the response buffer, because we
8982 * may have blocked an invalid response from a server that we don't
8983 * want to accidentely forward once we disable the analysers, nor do
8984 * we want those data to come along with next response. A typical
8985 * example of such data would be from a buggy server responding to
8986 * a HEAD with some data, or sending more than the advertised
8987 * content-length.
8988 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008989 if (unlikely(s->res.buf->i))
8990 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008991
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008992 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008993 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008994
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008995 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008996 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008997
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008998 s->req.rex = TICK_ETERNITY;
8999 s->req.wex = TICK_ETERNITY;
9000 s->req.analyse_exp = TICK_ETERNITY;
9001 s->res.rex = TICK_ETERNITY;
9002 s->res.wex = TICK_ETERNITY;
9003 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009004}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009005
Sasha Pachev218f0642014-06-16 12:05:59 -06009006void free_http_res_rules(struct list *r)
9007{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009008 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06009009
9010 list_for_each_entry_safe(pr, tr, r, list) {
9011 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009012 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009013 free(pr);
9014 }
9015}
9016
9017void free_http_req_rules(struct list *r)
9018{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009019 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009020
9021 list_for_each_entry_safe(pr, tr, r, list) {
9022 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009023 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009024 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009025
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009026 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009027 free(pr);
9028 }
9029}
9030
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009031/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009032struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009033{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009034 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009035 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009036 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009037 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009038
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009039 rule = (struct act_rule*)calloc(1, sizeof(struct act_rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009040 if (!rule) {
9041 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009042 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009043 }
9044
CJ Ess108b1dd2015-04-07 12:03:37 -04009045 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009046 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009047 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009048 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009049 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009050 int code;
9051 int hc;
9052
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009053 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01009054 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009055 if (strcmp(args[cur_arg], "deny_status") == 0) {
9056 cur_arg++;
9057 if (!args[cur_arg]) {
9058 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9059 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9060 goto out_err;
9061 }
9062
9063 code = atol(args[cur_arg]);
9064 cur_arg++;
9065 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9066 if (http_err_codes[hc] == code) {
9067 rule->deny_status = hc;
9068 break;
9069 }
9070 }
9071
9072 if (hc >= HTTP_ERR_SIZE) {
9073 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9074 file, linenum, code);
9075 }
9076 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009077 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009078 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009079 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009080 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009081 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009082 cur_arg = 1;
9083
9084 while(*args[cur_arg]) {
9085 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009086 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009087 cur_arg+=2;
9088 continue;
9089 } else
9090 break;
9091 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009092 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009093 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009094 cur_arg = 1;
9095
9096 if (!*args[cur_arg] ||
9097 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9098 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9099 file, linenum, args[0]);
9100 goto out_err;
9101 }
9102 rule->arg.nice = atoi(args[cur_arg]);
9103 if (rule->arg.nice < -1024)
9104 rule->arg.nice = -1024;
9105 else if (rule->arg.nice > 1024)
9106 rule->arg.nice = 1024;
9107 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009108 } else if (!strcmp(args[0], "set-tos")) {
9109#ifdef IP_TOS
9110 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009111 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009112 cur_arg = 1;
9113
9114 if (!*args[cur_arg] ||
9115 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9116 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9117 file, linenum, args[0]);
9118 goto out_err;
9119 }
9120
9121 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9122 if (err && *err != '\0') {
9123 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9124 file, linenum, err, args[0]);
9125 goto out_err;
9126 }
9127 cur_arg++;
9128#else
9129 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9130 goto out_err;
9131#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009132 } else if (!strcmp(args[0], "set-mark")) {
9133#ifdef SO_MARK
9134 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009135 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009136 cur_arg = 1;
9137
9138 if (!*args[cur_arg] ||
9139 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9140 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9141 file, linenum, args[0]);
9142 goto out_err;
9143 }
9144
9145 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9146 if (err && *err != '\0') {
9147 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9148 file, linenum, err, args[0]);
9149 goto out_err;
9150 }
9151 cur_arg++;
9152 global.last_checks |= LSTCHK_NETADM;
9153#else
9154 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9155 goto out_err;
9156#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009157 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009158 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009159 cur_arg = 1;
9160
9161 if (!*args[cur_arg] ||
9162 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9163 bad_log_level:
9164 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9165 file, linenum, args[0]);
9166 goto out_err;
9167 }
9168 if (strcmp(args[cur_arg], "silent") == 0)
9169 rule->arg.loglevel = -1;
9170 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9171 goto bad_log_level;
9172 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009173 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009174 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009175 cur_arg = 1;
9176
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009177 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9178 (*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 +01009179 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9180 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009181 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009182 }
9183
9184 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9185 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9186 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009187
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009188 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009189 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009190 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9191 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009192 free(proxy->conf.lfs_file);
9193 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9194 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009195 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009196 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009197 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009198 cur_arg = 1;
9199
9200 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009201 (*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 -06009202 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9203 file, linenum, args[0]);
9204 goto out_err;
9205 }
9206
9207 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9208 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9209 LIST_INIT(&rule->arg.hdr_add.fmt);
9210
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009211 error = NULL;
9212 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9213 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9214 args[cur_arg + 1], error);
9215 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009216 goto out_err;
9217 }
9218
9219 proxy->conf.args.ctx = ARGC_HRQ;
9220 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9221 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9222 file, linenum);
9223
9224 free(proxy->conf.lfs_file);
9225 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9226 proxy->conf.lfs_line = proxy->conf.args.line;
9227 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009228 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009229 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009230 cur_arg = 1;
9231
9232 if (!*args[cur_arg] ||
9233 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9234 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9235 file, linenum, args[0]);
9236 goto out_err;
9237 }
9238
9239 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9240 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9241
9242 proxy->conf.args.ctx = ARGC_HRQ;
9243 free(proxy->conf.lfs_file);
9244 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9245 proxy->conf.lfs_line = proxy->conf.args.line;
9246 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009247 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9248 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009249 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009250 struct sample_expr *expr;
9251 unsigned int where;
9252 char *err = NULL;
9253
9254 cur_arg = 1;
9255 proxy->conf.args.ctx = ARGC_TRK;
9256
9257 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9258 if (!expr) {
9259 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9260 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9261 free(err);
9262 goto out_err;
9263 }
9264
9265 where = 0;
9266 if (proxy->cap & PR_CAP_FE)
9267 where |= SMP_VAL_FE_HRQ_HDR;
9268 if (proxy->cap & PR_CAP_BE)
9269 where |= SMP_VAL_BE_HRQ_HDR;
9270
9271 if (!(expr->fetch->val & where)) {
9272 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9273 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9274 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9275 args[cur_arg-1], sample_src_names(expr->fetch->use));
9276 free(expr);
9277 goto out_err;
9278 }
9279
9280 if (strcmp(args[cur_arg], "table") == 0) {
9281 cur_arg++;
9282 if (!args[cur_arg]) {
9283 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9284 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9285 free(expr);
9286 goto out_err;
9287 }
9288 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009289 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009290 cur_arg++;
9291 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009292 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009293 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009294 } else if (strcmp(args[0], "redirect") == 0) {
9295 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009296 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009297
Willy Tarreaube4653b2015-05-28 15:26:58 +02009298 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009299 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9300 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9301 goto out_err;
9302 }
9303
9304 /* this redirect rule might already contain a parsed condition which
9305 * we'll pass to the http-request rule.
9306 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009307 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009308 rule->arg.redir = redir;
9309 rule->cond = redir->cond;
9310 redir->cond = NULL;
9311 cur_arg = 2;
9312 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009313 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9314 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009315 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009316 /*
9317 * '+ 8' for 'add-acl('
9318 * '- 9' for 'add-acl(' + trailing ')'
9319 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009320 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009321
9322 cur_arg = 1;
9323
9324 if (!*args[cur_arg] ||
9325 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9326 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9327 file, linenum, args[0]);
9328 goto out_err;
9329 }
9330
9331 LIST_INIT(&rule->arg.map.key);
9332 proxy->conf.args.ctx = ARGC_HRQ;
9333 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9334 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9335 file, linenum);
9336 free(proxy->conf.lfs_file);
9337 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9338 proxy->conf.lfs_line = proxy->conf.args.line;
9339 cur_arg += 1;
9340 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9341 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009342 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009343 /*
9344 * '+ 8' for 'del-acl('
9345 * '- 9' for 'del-acl(' + trailing ')'
9346 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009347 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009348
9349 cur_arg = 1;
9350
9351 if (!*args[cur_arg] ||
9352 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9353 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9354 file, linenum, args[0]);
9355 goto out_err;
9356 }
9357
9358 LIST_INIT(&rule->arg.map.key);
9359 proxy->conf.args.ctx = ARGC_HRQ;
9360 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9361 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9362 file, linenum);
9363 free(proxy->conf.lfs_file);
9364 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9365 proxy->conf.lfs_line = proxy->conf.args.line;
9366 cur_arg += 1;
9367 } else if (strncmp(args[0], "del-map", 7) == 0) {
9368 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009369 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009370 /*
9371 * '+ 8' for 'del-map('
9372 * '- 9' for 'del-map(' + trailing ')'
9373 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009374 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009375
9376 cur_arg = 1;
9377
9378 if (!*args[cur_arg] ||
9379 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9380 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9381 file, linenum, args[0]);
9382 goto out_err;
9383 }
9384
9385 LIST_INIT(&rule->arg.map.key);
9386 proxy->conf.args.ctx = ARGC_HRQ;
9387 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9388 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9389 file, linenum);
9390 free(proxy->conf.lfs_file);
9391 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9392 proxy->conf.lfs_line = proxy->conf.args.line;
9393 cur_arg += 1;
9394 } else if (strncmp(args[0], "set-map", 7) == 0) {
9395 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009396 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009397 /*
9398 * '+ 8' for 'set-map('
9399 * '- 9' for 'set-map(' + trailing ')'
9400 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009401 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009402
9403 cur_arg = 1;
9404
9405 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9406 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9407 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9408 file, linenum, args[0]);
9409 goto out_err;
9410 }
9411
9412 LIST_INIT(&rule->arg.map.key);
9413 LIST_INIT(&rule->arg.map.value);
9414 proxy->conf.args.ctx = ARGC_HRQ;
9415
9416 /* key pattern */
9417 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9418 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9419 file, linenum);
9420
9421 /* value pattern */
9422 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9423 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9424 file, linenum);
9425 free(proxy->conf.lfs_file);
9426 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9427 proxy->conf.lfs_line = proxy->conf.args.line;
9428
9429 cur_arg += 2;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009430 } else if (strncmp(args[0], "set-src", 7) == 0) {
9431 struct sample_expr *expr;
9432 unsigned int where;
9433 char *err = NULL;
9434
9435 cur_arg = 1;
9436 proxy->conf.args.ctx = ARGC_HRQ;
9437
9438 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9439 if (!expr) {
9440 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9441 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9442 free(err);
9443 goto out_err;
9444 }
9445
9446 where = 0;
9447 if (proxy->cap & PR_CAP_FE)
9448 where |= SMP_VAL_FE_HRQ_HDR;
9449 if (proxy->cap & PR_CAP_BE)
9450 where |= SMP_VAL_BE_HRQ_HDR;
9451
9452 if (!(expr->fetch->val & where)) {
9453 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9454 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9455 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9456 args[cur_arg-1], sample_src_names(expr->fetch->use));
9457 free(expr);
9458 goto out_err;
9459 }
9460
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02009461 rule->arg.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009462 rule->action = ACT_HTTP_REQ_SET_SRC;
William Lallemand73025dd2014-04-24 14:38:37 +02009463 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9464 char *errmsg = NULL;
9465 cur_arg = 1;
9466 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009467 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009468 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009469 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009470 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9471 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9472 free(errmsg);
9473 goto out_err;
9474 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009475 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009476 action_build_list(&http_req_keywords.list, &trash);
9477 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9478 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9479 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', "
9480 "'set-src'%s%s, but got '%s'%s.\n",
9481 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009482 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009483 }
9484
9485 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9486 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009487 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009488
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009489 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9490 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9491 file, linenum, args[0], errmsg);
9492 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009493 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009494 }
9495 rule->cond = cond;
9496 }
9497 else if (*args[cur_arg]) {
9498 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9499 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9500 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009501 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009502 }
9503
9504 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009505 out_err:
9506 free(rule);
9507 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009508}
9509
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009510/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009511struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009512{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009513 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009514 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009515 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009516 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009517
9518 rule = calloc(1, sizeof(*rule));
9519 if (!rule) {
9520 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9521 goto out_err;
9522 }
9523
9524 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009525 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009526 cur_arg = 1;
9527 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009528 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009529 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009530 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009531 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009532 cur_arg = 1;
9533
9534 if (!*args[cur_arg] ||
9535 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9536 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9537 file, linenum, args[0]);
9538 goto out_err;
9539 }
9540 rule->arg.nice = atoi(args[cur_arg]);
9541 if (rule->arg.nice < -1024)
9542 rule->arg.nice = -1024;
9543 else if (rule->arg.nice > 1024)
9544 rule->arg.nice = 1024;
9545 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009546 } else if (!strcmp(args[0], "set-tos")) {
9547#ifdef IP_TOS
9548 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009549 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009550 cur_arg = 1;
9551
9552 if (!*args[cur_arg] ||
9553 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9554 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9555 file, linenum, args[0]);
9556 goto out_err;
9557 }
9558
9559 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9560 if (err && *err != '\0') {
9561 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9562 file, linenum, err, args[0]);
9563 goto out_err;
9564 }
9565 cur_arg++;
9566#else
9567 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9568 goto out_err;
9569#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009570 } else if (!strcmp(args[0], "set-mark")) {
9571#ifdef SO_MARK
9572 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009573 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009574 cur_arg = 1;
9575
9576 if (!*args[cur_arg] ||
9577 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9578 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9579 file, linenum, args[0]);
9580 goto out_err;
9581 }
9582
9583 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9584 if (err && *err != '\0') {
9585 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9586 file, linenum, err, args[0]);
9587 goto out_err;
9588 }
9589 cur_arg++;
9590 global.last_checks |= LSTCHK_NETADM;
9591#else
9592 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9593 goto out_err;
9594#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009595 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009596 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009597 cur_arg = 1;
9598
9599 if (!*args[cur_arg] ||
9600 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9601 bad_log_level:
9602 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9603 file, linenum, args[0]);
9604 goto out_err;
9605 }
9606 if (strcmp(args[cur_arg], "silent") == 0)
9607 rule->arg.loglevel = -1;
9608 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9609 goto bad_log_level;
9610 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009611 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009612 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009613 cur_arg = 1;
9614
9615 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9616 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9617 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9618 file, linenum, args[0]);
9619 goto out_err;
9620 }
9621
9622 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9623 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9624 LIST_INIT(&rule->arg.hdr_add.fmt);
9625
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009626 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009627 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009628 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9629 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009630 free(proxy->conf.lfs_file);
9631 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9632 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009633 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009634 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009635 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009636 cur_arg = 1;
9637
9638 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009639 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9640 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009641 file, linenum, args[0]);
9642 goto out_err;
9643 }
9644
9645 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9646 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9647 LIST_INIT(&rule->arg.hdr_add.fmt);
9648
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009649 error = NULL;
9650 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9651 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9652 args[cur_arg + 1], error);
9653 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009654 goto out_err;
9655 }
9656
9657 proxy->conf.args.ctx = ARGC_HRQ;
9658 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9659 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9660 file, linenum);
9661
9662 free(proxy->conf.lfs_file);
9663 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9664 proxy->conf.lfs_line = proxy->conf.args.line;
9665 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009666 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009667 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009668 cur_arg = 1;
9669
9670 if (!*args[cur_arg] ||
9671 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9672 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9673 file, linenum, args[0]);
9674 goto out_err;
9675 }
9676
9677 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9678 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9679
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009680 proxy->conf.args.ctx = ARGC_HRS;
9681 free(proxy->conf.lfs_file);
9682 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9683 proxy->conf.lfs_line = proxy->conf.args.line;
9684 cur_arg += 1;
9685 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9686 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009687 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009688 /*
9689 * '+ 8' for 'add-acl('
9690 * '- 9' for 'add-acl(' + trailing ')'
9691 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009692 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009693
9694 cur_arg = 1;
9695
9696 if (!*args[cur_arg] ||
9697 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9698 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9699 file, linenum, args[0]);
9700 goto out_err;
9701 }
9702
9703 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009704 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009705 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9706 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9707 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009708 free(proxy->conf.lfs_file);
9709 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9710 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009711
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009712 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009713 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9714 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009715 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009716 /*
9717 * '+ 8' for 'del-acl('
9718 * '- 9' for 'del-acl(' + trailing ')'
9719 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009720 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009721
9722 cur_arg = 1;
9723
9724 if (!*args[cur_arg] ||
9725 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9726 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9727 file, linenum, args[0]);
9728 goto out_err;
9729 }
9730
9731 LIST_INIT(&rule->arg.map.key);
9732 proxy->conf.args.ctx = ARGC_HRS;
9733 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9734 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9735 file, linenum);
9736 free(proxy->conf.lfs_file);
9737 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9738 proxy->conf.lfs_line = proxy->conf.args.line;
9739 cur_arg += 1;
9740 } else if (strncmp(args[0], "del-map", 7) == 0) {
9741 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009742 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009743 /*
9744 * '+ 8' for 'del-map('
9745 * '- 9' for 'del-map(' + trailing ')'
9746 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009747 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009748
9749 cur_arg = 1;
9750
9751 if (!*args[cur_arg] ||
9752 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9753 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9754 file, linenum, args[0]);
9755 goto out_err;
9756 }
9757
9758 LIST_INIT(&rule->arg.map.key);
9759 proxy->conf.args.ctx = ARGC_HRS;
9760 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9761 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9762 file, linenum);
9763 free(proxy->conf.lfs_file);
9764 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9765 proxy->conf.lfs_line = proxy->conf.args.line;
9766 cur_arg += 1;
9767 } else if (strncmp(args[0], "set-map", 7) == 0) {
9768 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009769 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009770 /*
9771 * '+ 8' for 'set-map('
9772 * '- 9' for 'set-map(' + trailing ')'
9773 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009774 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009775
9776 cur_arg = 1;
9777
9778 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9779 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9780 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9781 file, linenum, args[0]);
9782 goto out_err;
9783 }
9784
9785 LIST_INIT(&rule->arg.map.key);
9786 LIST_INIT(&rule->arg.map.value);
9787
9788 proxy->conf.args.ctx = ARGC_HRS;
9789
9790 /* key pattern */
9791 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9792 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9793 file, linenum);
9794
9795 /* value pattern */
9796 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9797 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9798 file, linenum);
9799
9800 free(proxy->conf.lfs_file);
9801 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9802 proxy->conf.lfs_line = proxy->conf.args.line;
9803
9804 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009805 } else if (strcmp(args[0], "redirect") == 0) {
9806 struct redirect_rule *redir;
9807 char *errmsg = NULL;
9808
9809 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9810 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9811 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9812 goto out_err;
9813 }
9814
9815 /* this redirect rule might already contain a parsed condition which
9816 * we'll pass to the http-request rule.
9817 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009818 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009819 rule->arg.redir = redir;
9820 rule->cond = redir->cond;
9821 redir->cond = NULL;
9822 cur_arg = 2;
9823 return rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009824 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9825 char *errmsg = NULL;
9826 cur_arg = 1;
9827 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009828 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009829 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009830 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009831 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9832 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9833 free(errmsg);
9834 goto out_err;
9835 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009836 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009837 action_build_list(&http_res_keywords.list, &trash);
9838 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9839 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9840 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', "
9841 "'set-src'%s%s, but got '%s'%s.\n",
9842 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009843 goto out_err;
9844 }
9845
9846 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9847 struct acl_cond *cond;
9848 char *errmsg = NULL;
9849
9850 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9851 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9852 file, linenum, args[0], errmsg);
9853 free(errmsg);
9854 goto out_err;
9855 }
9856 rule->cond = cond;
9857 }
9858 else if (*args[cur_arg]) {
9859 Alert("parsing [%s:%d]: 'http-response %s' expects"
9860 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9861 file, linenum, args[0], args[cur_arg]);
9862 goto out_err;
9863 }
9864
9865 return rule;
9866 out_err:
9867 free(rule);
9868 return NULL;
9869}
9870
Willy Tarreau4baae242012-12-27 12:00:31 +01009871/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009872 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009873 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9874 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009875 */
9876struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009877 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009878{
9879 struct redirect_rule *rule;
9880 int cur_arg;
9881 int type = REDIRECT_TYPE_NONE;
9882 int code = 302;
9883 const char *destination = NULL;
9884 const char *cookie = NULL;
9885 int cookie_set = 0;
9886 unsigned int flags = REDIRECT_FLAG_NONE;
9887 struct acl_cond *cond = NULL;
9888
9889 cur_arg = 0;
9890 while (*(args[cur_arg])) {
9891 if (strcmp(args[cur_arg], "location") == 0) {
9892 if (!*args[cur_arg + 1])
9893 goto missing_arg;
9894
9895 type = REDIRECT_TYPE_LOCATION;
9896 cur_arg++;
9897 destination = args[cur_arg];
9898 }
9899 else if (strcmp(args[cur_arg], "prefix") == 0) {
9900 if (!*args[cur_arg + 1])
9901 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009902 type = REDIRECT_TYPE_PREFIX;
9903 cur_arg++;
9904 destination = args[cur_arg];
9905 }
9906 else if (strcmp(args[cur_arg], "scheme") == 0) {
9907 if (!*args[cur_arg + 1])
9908 goto missing_arg;
9909
9910 type = REDIRECT_TYPE_SCHEME;
9911 cur_arg++;
9912 destination = args[cur_arg];
9913 }
9914 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9915 if (!*args[cur_arg + 1])
9916 goto missing_arg;
9917
9918 cur_arg++;
9919 cookie = args[cur_arg];
9920 cookie_set = 1;
9921 }
9922 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9923 if (!*args[cur_arg + 1])
9924 goto missing_arg;
9925
9926 cur_arg++;
9927 cookie = args[cur_arg];
9928 cookie_set = 0;
9929 }
9930 else if (strcmp(args[cur_arg], "code") == 0) {
9931 if (!*args[cur_arg + 1])
9932 goto missing_arg;
9933
9934 cur_arg++;
9935 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009936 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009937 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009938 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009939 args[cur_arg - 1], args[cur_arg]);
9940 return NULL;
9941 }
9942 }
9943 else if (!strcmp(args[cur_arg],"drop-query")) {
9944 flags |= REDIRECT_FLAG_DROP_QS;
9945 }
9946 else if (!strcmp(args[cur_arg],"append-slash")) {
9947 flags |= REDIRECT_FLAG_APPEND_SLASH;
9948 }
9949 else if (strcmp(args[cur_arg], "if") == 0 ||
9950 strcmp(args[cur_arg], "unless") == 0) {
9951 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9952 if (!cond) {
9953 memprintf(errmsg, "error in condition: %s", *errmsg);
9954 return NULL;
9955 }
9956 break;
9957 }
9958 else {
9959 memprintf(errmsg,
9960 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9961 args[cur_arg]);
9962 return NULL;
9963 }
9964 cur_arg++;
9965 }
9966
9967 if (type == REDIRECT_TYPE_NONE) {
9968 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9969 return NULL;
9970 }
9971
Willy Tarreaube4653b2015-05-28 15:26:58 +02009972 if (dir && type != REDIRECT_TYPE_LOCATION) {
9973 memprintf(errmsg, "response only supports redirect type 'location'");
9974 return NULL;
9975 }
9976
Willy Tarreau4baae242012-12-27 12:00:31 +01009977 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9978 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009979 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009980
9981 if (!use_fmt) {
9982 /* old-style static redirect rule */
9983 rule->rdr_str = strdup(destination);
9984 rule->rdr_len = strlen(destination);
9985 }
9986 else {
9987 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009988
9989 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9990 * if prefix == "/", we don't want to add anything, otherwise it
9991 * makes it hard for the user to configure a self-redirection.
9992 */
Godbachd9722032014-12-18 15:44:58 +08009993 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009994 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009995 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009996 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
9997 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009998 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009999 free(curproxy->conf.lfs_file);
10000 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10001 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010002 }
10003 }
10004
Willy Tarreau4baae242012-12-27 12:00:31 +010010005 if (cookie) {
10006 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10007 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10008 */
10009 rule->cookie_len = strlen(cookie);
10010 if (cookie_set) {
10011 rule->cookie_str = malloc(rule->cookie_len + 10);
10012 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10013 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10014 rule->cookie_len += 9;
10015 } else {
10016 rule->cookie_str = malloc(rule->cookie_len + 21);
10017 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10018 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10019 rule->cookie_len += 20;
10020 }
10021 }
10022 rule->type = type;
10023 rule->code = code;
10024 rule->flags = flags;
10025 LIST_INIT(&rule->list);
10026 return rule;
10027
10028 missing_arg:
10029 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10030 return NULL;
10031}
10032
Willy Tarreau8797c062007-05-07 00:55:35 +020010033/************************************************************************/
10034/* The code below is dedicated to ACL parsing and matching */
10035/************************************************************************/
10036
10037
Willy Tarreau14174bc2012-04-16 14:34:04 +020010038/* This function ensures that the prerequisites for an L7 fetch are ready,
10039 * which means that a request or response is ready. If some data is missing,
10040 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010041 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10042 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010043 *
10044 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010045 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10046 * decide whether or not an HTTP message is present ;
10047 * 0 if the requested data cannot be fetched or if it is certain that
10048 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010049 * 1 if an HTTP message is ready
10050 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010051int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010052 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010053{
Willy Tarreau192252e2015-04-04 01:47:55 +020010054 struct http_txn *txn;
10055 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010056
Willy Tarreaueee5b512015-04-03 23:46:31 +020010057 /* Note: this function may only be used from places where
10058 * http_init_txn() has already been done, and implies that <s>,
10059 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
10060 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010061 */
10062
Willy Tarreau192252e2015-04-04 01:47:55 +020010063 if (!s)
10064 return 0;
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010065 if (!s->txn) {
10066 if (unlikely(!http_alloc_txn(s)))
10067 return 0; /* not enough memory */
10068 http_init_txn(s);
10069 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010070 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010071 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010072
10073 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010074 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010075
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010076 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010077 /* If the buffer does not leave enough free space at the end,
10078 * we must first realign it.
10079 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010080 if (s->req.buf->p > s->req.buf->data &&
10081 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10082 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010083
Willy Tarreau14174bc2012-04-16 14:34:04 +020010084 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010085 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010086 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010087
10088 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010089 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010090 http_msg_analyzer(msg, &txn->hdr_idx);
10091
10092 /* Still no valid request ? */
10093 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010094 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010095 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010096 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010097 }
10098 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010099 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010100 return 0;
10101 }
10102
10103 /* OK we just got a valid HTTP request. We have some minor
10104 * preparation to perform so that further checks can rely
10105 * on HTTP tests.
10106 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010107
10108 /* If the request was parsed but was too large, we must absolutely
10109 * return an error so that it is not processed. At the moment this
10110 * cannot happen, but if the parsers are to change in the future,
10111 * we want this check to be maintained.
10112 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010113 if (unlikely(s->req.buf->i + s->req.buf->p >
10114 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010115 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010116 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010117 return 1;
10118 }
10119
Willy Tarreau9b28e032012-10-12 23:49:43 +020010120 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010121 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010122 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010123
Willy Tarreau506d0502013-07-06 13:29:24 +020010124 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10125 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010126 }
10127
Willy Tarreau506d0502013-07-06 13:29:24 +020010128 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010129 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010130 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010131
10132 /* otherwise everything's ready for the request */
10133 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010134 else {
10135 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010136 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10137 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010138 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010139 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010140 }
10141
10142 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010143 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010144 return 1;
10145}
Willy Tarreau8797c062007-05-07 00:55:35 +020010146
Willy Tarreau8797c062007-05-07 00:55:35 +020010147/* 1. Check on METHOD
10148 * We use the pre-parsed method if it is known, and store its number as an
10149 * integer. If it is unknown, we use the pointer and the length.
10150 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010151static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010152{
10153 int len, meth;
10154
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010155 len = strlen(text);
10156 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010157
10158 pattern->val.i = meth;
10159 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010160 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010161 pattern->len = len;
10162 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010163 else {
10164 pattern->ptr.str = NULL;
10165 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010166 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010167 return 1;
10168}
10169
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010170/* This function fetches the method of current HTTP request and stores
10171 * it in the global pattern struct as a chunk. There are two possibilities :
10172 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10173 * in <len> and <ptr> is NULL ;
10174 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10175 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010176 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010177 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010178static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010179smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010180{
10181 int meth;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010182 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010183
Willy Tarreau24e32d82012-04-23 23:55:44 +020010184 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010185
Willy Tarreau8797c062007-05-07 00:55:35 +020010186 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010187 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010188 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010189 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010190 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10191 /* ensure the indexes are not affected */
10192 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010193 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010194 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10195 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010196 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010197 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010198 return 1;
10199}
10200
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010201/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010202static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010203{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010204 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010205 struct pattern_list *lst;
10206 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010207
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010208 list_for_each_entry(lst, &expr->patterns, list) {
10209 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010210
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010211 /* well-known method */
10212 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010213 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010214 return pattern;
10215 else
10216 continue;
10217 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010218
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010219 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010220 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010221 continue;
10222
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010223 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010224 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10225 (!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 +010010226 return pattern;
10227 }
10228 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010229}
10230
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010231static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010232smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010233{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010234 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010235 char *ptr;
10236 int len;
10237
Willy Tarreauc0239e02012-04-16 14:42:55 +020010238 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010239
Willy Tarreau8797c062007-05-07 00:55:35 +020010240 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010241 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010242
10243 while ((len-- > 0) && (*ptr++ != '/'));
10244 if (len <= 0)
10245 return 0;
10246
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010247 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010248 smp->data.u.str.str = ptr;
10249 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010250
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010251 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010252 return 1;
10253}
10254
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010255static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010256smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010257{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010258 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010259 char *ptr;
10260 int len;
10261
Willy Tarreauc0239e02012-04-16 14:42:55 +020010262 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010263
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010264 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010265 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10266 return 0;
10267
Willy Tarreau8797c062007-05-07 00:55:35 +020010268 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010269 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010270
10271 while ((len-- > 0) && (*ptr++ != '/'));
10272 if (len <= 0)
10273 return 0;
10274
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010275 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010276 smp->data.u.str.str = ptr;
10277 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010278
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010279 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010280 return 1;
10281}
10282
10283/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010284static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010285smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010286{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010287 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010288 char *ptr;
10289 int len;
10290
Willy Tarreauc0239e02012-04-16 14:42:55 +020010291 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010292
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010293 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010294 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10295 return 0;
10296
Willy Tarreau8797c062007-05-07 00:55:35 +020010297 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010298 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010299
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010300 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010301 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010302 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010303 return 1;
10304}
10305
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010306/* returns the longest available part of the body. This requires that the body
10307 * has been waited for using http-buffer-request.
10308 */
10309static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010310smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010311{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010312 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010313 struct http_msg *msg;
10314 unsigned long len;
10315 unsigned long block1;
10316 char *body;
10317 struct chunk *temp;
10318
10319 CHECK_HTTP_MESSAGE_FIRST();
10320
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010321 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010322 msg = &txn->req;
10323 else
10324 msg = &txn->rsp;
10325
10326 len = http_body_bytes(msg);
10327 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10328
10329 block1 = len;
10330 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10331 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10332
10333 if (block1 == len) {
10334 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010335 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010336 smp->data.u.str.str = body;
10337 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010338 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10339 }
10340 else {
10341 /* buffer is wrapped, we need to defragment it */
10342 temp = get_trash_chunk();
10343 memcpy(temp->str, body, block1);
10344 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010345 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010346 smp->data.u.str.str = temp->str;
10347 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010348 smp->flags = SMP_F_VOL_TEST;
10349 }
10350 return 1;
10351}
10352
10353
10354/* returns the available length of the body. This requires that the body
10355 * has been waited for using http-buffer-request.
10356 */
10357static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010358smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010359{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010360 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010361 struct http_msg *msg;
10362
10363 CHECK_HTTP_MESSAGE_FIRST();
10364
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010365 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010366 msg = &txn->req;
10367 else
10368 msg = &txn->rsp;
10369
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010370 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010371 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010372
10373 smp->flags = SMP_F_VOL_TEST;
10374 return 1;
10375}
10376
10377
10378/* returns the advertised length of the body, or the advertised size of the
10379 * chunks available in the buffer. This requires that the body has been waited
10380 * for using http-buffer-request.
10381 */
10382static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010383smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010384{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010385 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010386 struct http_msg *msg;
10387
10388 CHECK_HTTP_MESSAGE_FIRST();
10389
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010390 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010391 msg = &txn->req;
10392 else
10393 msg = &txn->rsp;
10394
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010395 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010396 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010397
10398 smp->flags = SMP_F_VOL_TEST;
10399 return 1;
10400}
10401
10402
Willy Tarreau8797c062007-05-07 00:55:35 +020010403/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010404static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010405smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010406{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010407 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010408
Willy Tarreauc0239e02012-04-16 14:42:55 +020010409 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010410
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010411 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010412 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010413 smp->data.u.str.len = txn->req.sl.rq.u_l;
10414 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010415 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010416 return 1;
10417}
10418
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010419static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010420smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010421{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010422 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010423 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010424
Willy Tarreauc0239e02012-04-16 14:42:55 +020010425 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010426
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010427 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010428 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 +020010429 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010430 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010431
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010432 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010433 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010434 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010435 return 1;
10436}
10437
10438static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010439smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010440{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010441 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010442 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010443
Willy Tarreauc0239e02012-04-16 14:42:55 +020010444 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010445
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010446 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010447 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 +020010448 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10449 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010450
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010451 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010452 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010453 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010454 return 1;
10455}
10456
Willy Tarreau185b5c42012-04-26 15:11:51 +020010457/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10458 * Accepts an optional argument of type string containing the header field name,
10459 * and an optional argument of type signed or unsigned integer to request an
10460 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010461 * headers are considered from the first one. It does not stop on commas and
10462 * returns full lines instead (useful for User-Agent or Date for example).
10463 */
10464static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010465smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010466{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010467 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010468 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010469 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010470 int occ = 0;
10471 const char *name_str = NULL;
10472 int name_len = 0;
10473
10474 if (!ctx) {
10475 /* first call */
10476 ctx = &static_hdr_ctx;
10477 ctx->idx = 0;
10478 smp->ctx.a[0] = ctx;
10479 }
10480
10481 if (args) {
10482 if (args[0].type != ARGT_STR)
10483 return 0;
10484 name_str = args[0].data.str.str;
10485 name_len = args[0].data.str.len;
10486
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010487 if (args[1].type == ARGT_SINT)
10488 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010489 }
10490
10491 CHECK_HTTP_MESSAGE_FIRST();
10492
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010493 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010494 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 +020010495
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010496 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10497 /* search for header from the beginning */
10498 ctx->idx = 0;
10499
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010500 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010501 /* no explicit occurrence and single fetch => last header by default */
10502 occ = -1;
10503
10504 if (!occ)
10505 /* prepare to report multiple occurrences for ACL fetches */
10506 smp->flags |= SMP_F_NOT_LAST;
10507
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010508 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010509 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010510 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 +020010511 return 1;
10512
10513 smp->flags &= ~SMP_F_NOT_LAST;
10514 return 0;
10515}
10516
10517/* 6. Check on HTTP header count. The number of occurrences is returned.
10518 * Accepts exactly 1 argument of type string. It does not stop on commas and
10519 * returns full lines instead (useful for User-Agent or Date for example).
10520 */
10521static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010522smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010523{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010524 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010525 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010526 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010527 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010528 const char *name = NULL;
10529 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010530
Willy Tarreau601a4d12015-04-01 19:16:09 +020010531 if (args && args->type == ARGT_STR) {
10532 name = args->data.str.str;
10533 len = args->data.str.len;
10534 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010535
10536 CHECK_HTTP_MESSAGE_FIRST();
10537
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010538 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010539 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 +020010540
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010541 ctx.idx = 0;
10542 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010543 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010544 cnt++;
10545
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010546 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010547 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010548 smp->flags = SMP_F_VOL_HDR;
10549 return 1;
10550}
10551
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010552static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010553smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010554{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010555 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010556 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010557 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010558 struct chunk *temp;
10559 char del = ',';
10560
10561 if (args && args->type == ARGT_STR)
10562 del = *args[0].data.str.str;
10563
10564 CHECK_HTTP_MESSAGE_FIRST();
10565
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010566 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010567 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 +020010568
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010569 temp = get_trash_chunk();
10570
10571 ctx.idx = 0;
10572 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10573 if (temp->len)
10574 temp->str[temp->len++] = del;
10575 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10576 temp->len += ctx.del;
10577 }
10578
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010579 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010580 smp->data.u.str.str = temp->str;
10581 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010582 smp->flags = SMP_F_VOL_HDR;
10583 return 1;
10584}
10585
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010586/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10587 * Accepts an optional argument of type string containing the header field name,
10588 * and an optional argument of type signed or unsigned integer to request an
10589 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010590 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010591 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010592static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010593smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010594{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010595 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010596 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010597 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010598 int occ = 0;
10599 const char *name_str = NULL;
10600 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010601
Willy Tarreaua890d072013-04-02 12:01:06 +020010602 if (!ctx) {
10603 /* first call */
10604 ctx = &static_hdr_ctx;
10605 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010606 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010607 }
10608
Willy Tarreau185b5c42012-04-26 15:11:51 +020010609 if (args) {
10610 if (args[0].type != ARGT_STR)
10611 return 0;
10612 name_str = args[0].data.str.str;
10613 name_len = args[0].data.str.len;
10614
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010615 if (args[1].type == ARGT_SINT)
10616 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010617 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010618
Willy Tarreaue333ec92012-04-16 16:26:40 +020010619 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010620
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010621 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010622 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 +020010623
Willy Tarreau185b5c42012-04-26 15:11:51 +020010624 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010625 /* search for header from the beginning */
10626 ctx->idx = 0;
10627
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010628 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010629 /* no explicit occurrence and single fetch => last header by default */
10630 occ = -1;
10631
10632 if (!occ)
10633 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010634 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010635
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010636 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010637 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010638 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 +020010639 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010640
Willy Tarreau37406352012-04-23 16:16:37 +020010641 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010642 return 0;
10643}
10644
Willy Tarreauc11416f2007-06-17 16:58:38 +020010645/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010646 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010647 */
10648static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010649smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010650{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010651 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010652 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010653 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010654 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010655 const char *name = NULL;
10656 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010657
Willy Tarreau601a4d12015-04-01 19:16:09 +020010658 if (args && args->type == ARGT_STR) {
10659 name = args->data.str.str;
10660 len = args->data.str.len;
10661 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010662
Willy Tarreaue333ec92012-04-16 16:26:40 +020010663 CHECK_HTTP_MESSAGE_FIRST();
10664
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010665 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010666 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 +020010667
Willy Tarreau33a7e692007-06-10 19:45:56 +020010668 ctx.idx = 0;
10669 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010670 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010671 cnt++;
10672
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010673 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010674 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010675 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010676 return 1;
10677}
10678
Willy Tarreau185b5c42012-04-26 15:11:51 +020010679/* Fetch an HTTP header's integer value. The integer value is returned. It
10680 * takes a mandatory argument of type string and an optional one of type int
10681 * to designate a specific occurrence. It returns an unsigned integer, which
10682 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010683 */
10684static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010685smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010686{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010687 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010688
Willy Tarreauf853c462012-04-23 18:53:56 +020010689 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010690 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010691 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010692 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010693
Willy Tarreaud53e2422012-04-16 17:21:11 +020010694 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010695}
10696
Cyril Bonté69fa9922012-10-25 00:01:06 +020010697/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10698 * and an optional one of type int to designate a specific occurrence.
10699 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010700 */
10701static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010702smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010703{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010704 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010705
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010706 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010707 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010708 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010709 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010710 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010711 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010712 if (smp->data.u.str.len < temp->size - 1) {
10713 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10714 temp->str[smp->data.u.str.len] = '\0';
10715 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010716 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010717 break;
10718 }
10719 }
10720 }
10721
Willy Tarreaud53e2422012-04-16 17:21:11 +020010722 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010723 if (!(smp->flags & SMP_F_NOT_LAST))
10724 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010725 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010726 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010727}
10728
Willy Tarreau737b0c12007-06-10 21:28:46 +020010729/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10730 * the first '/' after the possible hostname, and ends before the possible '?'.
10731 */
10732static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010733smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010734{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010735 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010736 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010737
Willy Tarreauc0239e02012-04-16 14:42:55 +020010738 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010739
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010740 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010741 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010742 ptr = http_get_path(txn);
10743 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010744 return 0;
10745
10746 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010747 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010748 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010749
10750 while (ptr < end && *ptr != '?')
10751 ptr++;
10752
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010753 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010754 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010755 return 1;
10756}
10757
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010758/* This produces a concatenation of the first occurrence of the Host header
10759 * followed by the path component if it begins with a slash ('/'). This means
10760 * that '*' will not be added, resulting in exactly the first Host entry.
10761 * If no Host header is found, then the path is returned as-is. The returned
10762 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010763 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010764 */
10765static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010766smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010767{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010768 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010769 char *ptr, *end, *beg;
10770 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010771 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010772
10773 CHECK_HTTP_MESSAGE_FIRST();
10774
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010775 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010776 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010777 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010778 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010779
10780 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010781 temp = get_trash_chunk();
10782 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010783 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010784 smp->data.u.str.str = temp->str;
10785 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010786
10787 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010788 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010789 beg = http_get_path(txn);
10790 if (!beg)
10791 beg = end;
10792
10793 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10794
10795 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010796 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10797 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010798 }
10799
10800 smp->flags = SMP_F_VOL_1ST;
10801 return 1;
10802}
10803
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010804/* This produces a 32-bit hash of the concatenation of the first occurrence of
10805 * the Host header followed by the path component if it begins with a slash ('/').
10806 * This means that '*' will not be added, resulting in exactly the first Host
10807 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010808 * is hashed using the path hash followed by a full avalanche hash and provides a
10809 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010810 * high-traffic sites without having to store whole paths.
10811 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010812int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010813smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010814{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010815 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010816 struct hdr_ctx ctx;
10817 unsigned int hash = 0;
10818 char *ptr, *beg, *end;
10819 int len;
10820
10821 CHECK_HTTP_MESSAGE_FIRST();
10822
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010823 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010824 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010825 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010826 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10827 ptr = ctx.line + ctx.val;
10828 len = ctx.vlen;
10829 while (len--)
10830 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10831 }
10832
10833 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010834 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010835 beg = http_get_path(txn);
10836 if (!beg)
10837 beg = end;
10838
10839 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10840
10841 if (beg < ptr && *beg == '/') {
10842 while (beg < ptr)
10843 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10844 }
10845 hash = full_hash(hash);
10846
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010847 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010848 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010849 smp->flags = SMP_F_VOL_1ST;
10850 return 1;
10851}
10852
Willy Tarreau4a550602012-12-09 14:53:32 +010010853/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010854 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10855 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10856 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010857 * that in environments where IPv6 is insignificant, truncating the output to
10858 * 8 bytes would still work.
10859 */
10860static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010861smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010862{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010863 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010864 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010865
10866 if (!cli_conn)
10867 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010868
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010869 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010870 return 0;
10871
Willy Tarreau47ca5452012-12-23 20:22:19 +010010872 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010873 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010874 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010875
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010876 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010877 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010878 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010879 temp->len += 4;
10880 break;
10881 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010882 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010883 temp->len += 16;
10884 break;
10885 default:
10886 return 0;
10887 }
10888
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010889 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010890 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010891 return 1;
10892}
10893
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010894/* Extracts the query string, which comes after the question mark '?'. If no
10895 * question mark is found, nothing is returned. Otherwise it returns a sample
10896 * of type string carrying the whole query string.
10897 */
10898static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010899smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010900{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010901 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010902 char *ptr, *end;
10903
10904 CHECK_HTTP_MESSAGE_FIRST();
10905
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010906 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010907 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10908 end = ptr + txn->req.sl.rq.u_l;
10909
10910 /* look up the '?' */
10911 do {
10912 if (ptr == end)
10913 return 0;
10914 } while (*ptr++ != '?');
10915
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010916 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010917 smp->data.u.str.str = ptr;
10918 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010919 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10920 return 1;
10921}
10922
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010923static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010924smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010925{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010926 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10927 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10928 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010929
Willy Tarreau24e32d82012-04-23 23:55:44 +020010930 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010931
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010932 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010933 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010934 return 1;
10935}
10936
Willy Tarreau7f18e522010-10-22 20:04:13 +020010937/* return a valid test if the current request is the first one on the connection */
10938static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010939smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010940{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010941 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010942 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010943 return 1;
10944}
10945
Willy Tarreau34db1082012-04-19 17:16:54 +020010946/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010947static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010948smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010949{
10950
Willy Tarreau24e32d82012-04-23 23:55:44 +020010951 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010952 return 0;
10953
Willy Tarreauc0239e02012-04-16 14:42:55 +020010954 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010955
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010956 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010957 return 0;
10958
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010959 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010960 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010961 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010962 return 1;
10963}
Willy Tarreau8797c062007-05-07 00:55:35 +020010964
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010965/* Accepts exactly 1 argument of type userlist */
10966static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010967smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010968{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010969 if (!args || args->type != ARGT_USR)
10970 return 0;
10971
10972 CHECK_HTTP_MESSAGE_FIRST();
10973
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010974 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010975 return 0;
10976
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010977 /* if the user does not belong to the userlist or has a wrong password,
10978 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010979 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010980 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010981 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10982 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010983 return 0;
10984
10985 /* pat_match_auth() will need the user list */
10986 smp->ctx.a[0] = args->data.usr;
10987
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010988 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010989 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010990 smp->data.u.str.str = smp->strm->txn->auth.user;
10991 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010992
10993 return 1;
10994}
10995
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010996/* Try to find the next occurrence of a cookie name in a cookie header value.
10997 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10998 * the cookie value is returned into *value and *value_l, and the function
10999 * returns a pointer to the next pointer to search from if the value was found.
11000 * Otherwise if the cookie was not found, NULL is returned and neither value
11001 * nor value_l are touched. The input <hdr> string should first point to the
11002 * header's value, and the <hdr_end> pointer must point to the first character
11003 * not part of the value. <list> must be non-zero if value may represent a list
11004 * of values (cookie headers). This makes it faster to abort parsing when no
11005 * list is expected.
11006 */
David Carlier4686f792015-09-25 14:10:50 +010011007char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011008extract_cookie_value(char *hdr, const char *hdr_end,
11009 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011010 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011011{
11012 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11013 char *next;
11014
11015 /* we search at least a cookie name followed by an equal, and more
11016 * generally something like this :
11017 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11018 */
11019 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11020 /* Iterate through all cookies on this line */
11021
11022 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
11023 att_beg++;
11024
11025 /* find att_end : this is the first character after the last non
11026 * space before the equal. It may be equal to hdr_end.
11027 */
11028 equal = att_end = att_beg;
11029
11030 while (equal < hdr_end) {
11031 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11032 break;
11033 if (http_is_spht[(unsigned char)*equal++])
11034 continue;
11035 att_end = equal;
11036 }
11037
11038 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11039 * is between <att_beg> and <equal>, both may be identical.
11040 */
11041
11042 /* look for end of cookie if there is an equal sign */
11043 if (equal < hdr_end && *equal == '=') {
11044 /* look for the beginning of the value */
11045 val_beg = equal + 1;
11046 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
11047 val_beg++;
11048
11049 /* find the end of the value, respecting quotes */
11050 next = find_cookie_value_end(val_beg, hdr_end);
11051
11052 /* make val_end point to the first white space or delimitor after the value */
11053 val_end = next;
11054 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
11055 val_end--;
11056 } else {
11057 val_beg = val_end = next = equal;
11058 }
11059
11060 /* We have nothing to do with attributes beginning with '$'. However,
11061 * they will automatically be removed if a header before them is removed,
11062 * since they're supposed to be linked together.
11063 */
11064 if (*att_beg == '$')
11065 continue;
11066
11067 /* Ignore cookies with no equal sign */
11068 if (equal == next)
11069 continue;
11070
11071 /* Now we have the cookie name between att_beg and att_end, and
11072 * its value between val_beg and val_end.
11073 */
11074
11075 if (att_end - att_beg == cookie_name_l &&
11076 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11077 /* let's return this value and indicate where to go on from */
11078 *value = val_beg;
11079 *value_l = val_end - val_beg;
11080 return next + 1;
11081 }
11082
11083 /* Set-Cookie headers only have the name in the first attr=value part */
11084 if (!list)
11085 break;
11086 }
11087
11088 return NULL;
11089}
11090
William Lallemanda43ba4e2014-01-28 18:14:25 +010011091/* Fetch a captured HTTP request header. The index is the position of
11092 * the "capture" option in the configuration file
11093 */
11094static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011095smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011096{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011097 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011098 int idx;
11099
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011100 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011101 return 0;
11102
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011103 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011104
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011105 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 +010011106 return 0;
11107
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011108 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011109 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011110 smp->data.u.str.str = smp->strm->req_cap[idx];
11111 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011112
11113 return 1;
11114}
11115
11116/* Fetch a captured HTTP response header. The index is the position of
11117 * the "capture" option in the configuration file
11118 */
11119static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011120smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011121{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011122 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011123 int idx;
11124
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011125 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011126 return 0;
11127
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011128 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011129
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011130 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 +010011131 return 0;
11132
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011133 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011134 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011135 smp->data.u.str.str = smp->strm->res_cap[idx];
11136 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011137
11138 return 1;
11139}
11140
William Lallemand65ad6e12014-01-31 15:08:02 +010011141/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11142static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011143smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011144{
11145 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011146 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011147 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011148
Willy Tarreau15e91e12015-04-04 00:52:09 +020011149 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011150 return 0;
11151
William Lallemand96a77852014-02-05 00:30:02 +010011152 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011153
William Lallemand96a77852014-02-05 00:30:02 +010011154 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11155 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011156
William Lallemand96a77852014-02-05 00:30:02 +010011157 temp = get_trash_chunk();
11158 temp->str = txn->uri;
11159 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011160 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011161 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011162 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011163
11164 return 1;
11165
11166}
11167
11168/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11169static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011170smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011171{
11172 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011173 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011174 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011175
Willy Tarreau15e91e12015-04-04 00:52:09 +020011176 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011177 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011178
William Lallemand65ad6e12014-01-31 15:08:02 +010011179 ptr = txn->uri;
11180
11181 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11182 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011183
William Lallemand65ad6e12014-01-31 15:08:02 +010011184 if (!*ptr)
11185 return 0;
11186
11187 ptr++; /* skip the space */
11188
11189 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011190 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011191 if (!ptr)
11192 return 0;
11193 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11194 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011195
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011196 smp->data.u.str = *temp;
11197 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011198 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011199 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011200
11201 return 1;
11202}
11203
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011204/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11205 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11206 */
11207static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011208smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011209{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011210 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011211
Willy Tarreau15e91e12015-04-04 00:52:09 +020011212 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011213 return 0;
11214
11215 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011216 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011217 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011218 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011219
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011220 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011221 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011222 smp->flags = SMP_F_CONST;
11223 return 1;
11224
11225}
11226
11227/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11228 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11229 */
11230static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011231smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011232{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011233 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011234
Willy Tarreau15e91e12015-04-04 00:52:09 +020011235 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011236 return 0;
11237
11238 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011239 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011240 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011241 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011242
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011243 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011244 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011245 smp->flags = SMP_F_CONST;
11246 return 1;
11247
11248}
11249
William Lallemand65ad6e12014-01-31 15:08:02 +010011250
Willy Tarreaue333ec92012-04-16 16:26:40 +020011251/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011252 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011253 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011254 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011255 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011256 * Accepts exactly 1 argument of type string. If the input options indicate
11257 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011258 * The returned sample is of type CSTR. Can be used to parse cookies in other
11259 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011260 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011261int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011262{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011263 struct http_txn *txn;
11264 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011265 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011266 const struct http_msg *msg;
11267 const char *hdr_name;
11268 int hdr_name_len;
11269 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011270 int occ = 0;
11271 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011272
Willy Tarreau24e32d82012-04-23 23:55:44 +020011273 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011274 return 0;
11275
Willy Tarreaua890d072013-04-02 12:01:06 +020011276 if (!ctx) {
11277 /* first call */
11278 ctx = &static_hdr_ctx;
11279 ctx->idx = 0;
11280 smp->ctx.a[2] = ctx;
11281 }
11282
Willy Tarreaue333ec92012-04-16 16:26:40 +020011283 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011284
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011285 txn = smp->strm->txn;
11286 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011287
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011288 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011289 msg = &txn->req;
11290 hdr_name = "Cookie";
11291 hdr_name_len = 6;
11292 } else {
11293 msg = &txn->rsp;
11294 hdr_name = "Set-Cookie";
11295 hdr_name_len = 10;
11296 }
11297
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011298 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011299 /* no explicit occurrence and single fetch => last cookie by default */
11300 occ = -1;
11301
11302 /* OK so basically here, either we want only one value and it's the
11303 * last one, or we want to iterate over all of them and we fetch the
11304 * next one.
11305 */
11306
Willy Tarreau9b28e032012-10-12 23:49:43 +020011307 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011308 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011309 /* search for the header from the beginning, we must first initialize
11310 * the search parameters.
11311 */
Willy Tarreau37406352012-04-23 16:16:37 +020011312 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011313 ctx->idx = 0;
11314 }
11315
Willy Tarreau28376d62012-04-26 21:26:10 +020011316 smp->flags |= SMP_F_VOL_HDR;
11317
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011318 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011319 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11320 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011321 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11322 goto out;
11323
Willy Tarreau24e32d82012-04-23 23:55:44 +020011324 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011325 continue;
11326
Willy Tarreau37406352012-04-23 16:16:37 +020011327 smp->ctx.a[0] = ctx->line + ctx->val;
11328 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011329 }
11330
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011331 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011332 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011333 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011334 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011335 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011336 &smp->data.u.str.str,
11337 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011338 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011339 found = 1;
11340 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011341 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011342 smp->flags |= SMP_F_NOT_LAST;
11343 return 1;
11344 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011345 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011346 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011347 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011348 /* all cookie headers and values were scanned. If we're looking for the
11349 * last occurrence, we may return it now.
11350 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011351 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011352 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011353 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011354}
11355
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011356/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011357 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011358 * multiple cookies may be parsed on the same line. The returned sample is of
11359 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011360 */
11361static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011362smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011363{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011364 struct http_txn *txn;
11365 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011366 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011367 const struct http_msg *msg;
11368 const char *hdr_name;
11369 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011370 int cnt;
11371 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011372 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011373
Willy Tarreau24e32d82012-04-23 23:55:44 +020011374 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011375 return 0;
11376
Willy Tarreaue333ec92012-04-16 16:26:40 +020011377 CHECK_HTTP_MESSAGE_FIRST();
11378
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011379 txn = smp->strm->txn;
11380 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011381
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011382 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011383 msg = &txn->req;
11384 hdr_name = "Cookie";
11385 hdr_name_len = 6;
11386 } else {
11387 msg = &txn->rsp;
11388 hdr_name = "Set-Cookie";
11389 hdr_name_len = 10;
11390 }
11391
Willy Tarreau9b28e032012-10-12 23:49:43 +020011392 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011393 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011394 ctx.idx = 0;
11395 cnt = 0;
11396
11397 while (1) {
11398 /* Note: val_beg == NULL every time we need to fetch a new header */
11399 if (!val_beg) {
11400 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11401 break;
11402
Willy Tarreau24e32d82012-04-23 23:55:44 +020011403 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011404 continue;
11405
11406 val_beg = ctx.line + ctx.val;
11407 val_end = val_beg + ctx.vlen;
11408 }
11409
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011410 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011411 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011412 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011413 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011414 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011415 &smp->data.u.str.str,
11416 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011417 cnt++;
11418 }
11419 }
11420
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011421 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011422 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011423 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011424 return 1;
11425}
11426
Willy Tarreau51539362012-05-08 12:46:28 +020011427/* Fetch an cookie's integer value. The integer value is returned. It
11428 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11429 */
11430static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011431smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011432{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011433 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011434
11435 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011436 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011437 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011438 }
11439
11440 return ret;
11441}
11442
Willy Tarreau8797c062007-05-07 00:55:35 +020011443/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011444/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011445/************************************************************************/
11446
David Cournapeau16023ee2010-12-23 20:55:41 +090011447/*
11448 * Given a path string and its length, find the position of beginning of the
11449 * query string. Returns NULL if no query string is found in the path.
11450 *
11451 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11452 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011453 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011454 */
bedis4c75cca2012-10-05 08:38:24 +020011455static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011456{
11457 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011458
bedis4c75cca2012-10-05 08:38:24 +020011459 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011460 return p ? p + 1 : NULL;
11461}
11462
bedis4c75cca2012-10-05 08:38:24 +020011463static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011464{
bedis4c75cca2012-10-05 08:38:24 +020011465 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011466}
11467
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011468/* after increasing a pointer value, it can exceed the first buffer
11469 * size. This function transform the value of <ptr> according with
11470 * the expected position. <chunks> is an array of the one or two
11471 * avalaible chunks. The first value is the start of the first chunk,
11472 * the second value if the end+1 of the first chunks. The third value
11473 * is NULL or the start of the second chunk and the fourth value is
11474 * the end+1 of the second chunk. The function returns 1 if does a
11475 * wrap, else returns 0.
11476 */
11477static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11478{
11479 if (*ptr < chunks[1])
11480 return 0;
11481 if (!chunks[2])
11482 return 0;
11483 *ptr = chunks[2] + ( *ptr - chunks[1] );
11484 return 1;
11485}
11486
David Cournapeau16023ee2010-12-23 20:55:41 +090011487/*
11488 * Given a url parameter, find the starting position of the first occurence,
11489 * or NULL if the parameter is not found.
11490 *
11491 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11492 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011493 *
11494 * Warning:this function returns a pointer that can be point to the first chunk
11495 * or the second chunk. The caller must be check the position before using the
11496 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011497 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011498static const char *
11499find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011500 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011501 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011502{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011503 const char *pos, *last, *equal;
11504 const char **bufs = chunks;
11505 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011506
David Cournapeau16023ee2010-12-23 20:55:41 +090011507
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011508 pos = bufs[0];
11509 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011510 while (pos <= last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011511 /* Check the equal. */
11512 equal = pos + url_param_name_l;
11513 if (fix_pointer_if_wrap(chunks, &equal)) {
11514 if (equal >= chunks[3])
11515 return NULL;
11516 } else {
11517 if (equal >= chunks[1])
11518 return NULL;
11519 }
11520 if (*equal == '=') {
11521 if (pos + url_param_name_l > last) {
11522 /* process wrap case, we detect a wrap. In this case, the
11523 * comparison is performed in two parts.
11524 */
11525
11526 /* This is the end, we dont have any other chunk. */
11527 if (bufs != chunks || !bufs[2])
11528 return NULL;
11529
11530 /* Compute the length of each part of the comparison. */
11531 l1 = last - pos;
11532 l2 = url_param_name_l - l1;
11533
11534 /* The second buffer is too short to contain the compared string. */
11535 if (bufs[2] + l2 > bufs[3])
11536 return NULL;
11537
11538 if (memcmp(pos, url_param_name, l1) == 0 &&
11539 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11540 return pos;
11541
11542 /* Perform wrapping and jump the string who fail the comparison. */
11543 bufs += 2;
11544 pos = bufs[0] + l2;
11545 last = bufs[1];
11546
11547 } else {
11548 /* process a simple comparison. */
11549 if (memcmp(pos, url_param_name, url_param_name_l) == 0) {
11550 return pos; }
11551 pos += url_param_name_l + 1;
11552 if (fix_pointer_if_wrap(chunks, &pos))
11553 last = bufs[2];
11554 }
11555 }
11556
11557 while (1) {
11558 /* Look for the next delimiter. */
11559 while (pos <= last && !is_param_delimiter(*pos, delim))
11560 pos++;
11561 if (pos < last)
11562 break;
11563 /* process buffer wrapping. */
11564 if (bufs != chunks || !bufs[2])
11565 return NULL;
11566 bufs += 2;
11567 pos = bufs[0];
11568 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011569 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011570 pos++;
11571 }
11572 return NULL;
11573}
11574
11575/*
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011576 * Given a url parameter name and a query string, returns its value and size
11577 * into *value and *value_l respectively, and returns non-zero. An empty
11578 * url_param_name matches the first available parameter. If the parameter is
11579 * not found, zero is returned and value/value_l are not touched.
David Cournapeau16023ee2010-12-23 20:55:41 +090011580 */
11581static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011582find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011583 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011584 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011585{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011586 const char *arg_start, *qs_end;
11587 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011588
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011589 arg_start = chunks[0];
11590 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011591 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011592 /* Looks for an argument name. */
11593 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011594 url_param_name, url_param_name_l,
11595 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011596 /* Check for wrapping. */
11597 if (arg_start > qs_end)
11598 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011599 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011600 if (!arg_start)
11601 return 0;
11602
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011603 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011604 while (1) {
11605 /* looks for the first argument. */
11606 value_start = memchr(arg_start, '=', qs_end - arg_start);
11607 if (!value_start) {
11608
11609 /* Check for wrapping. */
11610 if (arg_start >= chunks[0] &&
11611 arg_start <= chunks[1] &&
11612 chunks[2]) {
11613 arg_start = chunks[2];
11614 qs_end = chunks[3];
11615 continue;
11616 }
11617 return 0;
11618 }
11619 break;
11620 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011621 value_start++;
11622 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011623 else {
11624 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011625 value_start = arg_start + url_param_name_l + 1;
11626
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011627 /* Check for pointer wrapping. */
11628 if (fix_pointer_if_wrap(chunks, &value_start)) {
11629 /* Update the end pointer. */
11630 qs_end = chunks[3];
11631
11632 /* Check for overflow. */
11633 if (value_start > qs_end)
11634 return 0;
11635 }
11636 }
11637
David Cournapeau16023ee2010-12-23 20:55:41 +090011638 value_end = value_start;
11639
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011640 while (1) {
11641 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11642 value_end++;
11643 if (value_end < qs_end)
11644 break;
11645 /* process buffer wrapping. */
11646 if (value_end >= chunks[0] &&
11647 value_end <= chunks[1] &&
11648 chunks[2]) {
11649 value_end = chunks[2];
11650 qs_end = chunks[3];
11651 continue;
11652 }
11653 break;
11654 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011655
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011656 *vstart = value_start;
11657 *vend = value_end;
Willy Tarreau00134332011-01-04 14:57:34 +010011658 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011659}
11660
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011661/* This scans a URL-encoded query string. It takes an optionally wrapping
11662 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11663 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11664 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011665 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011666static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011667smp_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 +090011668{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011669 const char *vstart, *vend;
11670 struct chunk *temp;
11671 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011672
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011673 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011674 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011675 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011676 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011677 return 0;
11678
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011679 /* Create sample. If the value is contiguous, return the pointer as CONST,
11680 * if the value is wrapped, copy-it in a buffer.
11681 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011682 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011683 if (chunks[2] &&
11684 vstart >= chunks[0] && vstart <= chunks[1] &&
11685 vend >= chunks[2] && vend <= chunks[3]) {
11686 /* Wrapped case. */
11687 temp = get_trash_chunk();
11688 memcpy(temp->str, vstart, chunks[1] - vstart);
11689 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011690 smp->data.u.str.str = temp->str;
11691 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011692 } else {
11693 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011694 smp->data.u.str.str = (char *)vstart;
11695 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011696 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11697 }
11698
11699 /* Update context, check wrapping. */
11700 chunks[0] = vend;
11701 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11702 chunks[1] = chunks[3];
11703 chunks[2] = NULL;
11704 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011705
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011706 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011707 smp->flags |= SMP_F_NOT_LAST;
11708
David Cournapeau16023ee2010-12-23 20:55:41 +090011709 return 1;
11710}
11711
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011712/* This function iterates over each parameter of the query string. It uses
11713 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011714 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11715 * An optional parameter name is passed in args[0], otherwise any parameter is
11716 * considered. It supports an optional delimiter argument for the beginning of
11717 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011718 */
11719static int
11720smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11721{
11722 struct http_msg *msg;
11723 char delim = '?';
11724 const char *name;
11725 int name_len;
11726
Dragan Dosen26f77e52015-05-25 10:02:11 +020011727 if (!args ||
11728 (args[0].type && args[0].type != ARGT_STR) ||
11729 (args[1].type && args[1].type != ARGT_STR))
11730 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011731
Dragan Dosen26f77e52015-05-25 10:02:11 +020011732 name = "";
11733 name_len = 0;
11734 if (args->type == ARGT_STR) {
11735 name = args->data.str.str;
11736 name_len = args->data.str.len;
11737 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011738
Dragan Dosen26f77e52015-05-25 10:02:11 +020011739 if (args[1].type)
11740 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011741
Dragan Dosen26f77e52015-05-25 10:02:11 +020011742 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011743 CHECK_HTTP_MESSAGE_FIRST();
11744
11745 msg = &smp->strm->txn->req;
11746
11747 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11748 msg->sl.rq.u_l, delim);
11749 if (!smp->ctx.a[0])
11750 return 0;
11751
11752 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011753
11754 /* Assume that the context is filled with NULL pointer
11755 * before the first call.
11756 * smp->ctx.a[2] = NULL;
11757 * smp->ctx.a[3] = NULL;
11758 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011759 }
11760
11761 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11762}
11763
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011764/* This function iterates over each parameter of the body. This requires
11765 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011766 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11767 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11768 * optional second part if the body wraps at the end of the buffer. An optional
11769 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011770 */
11771static int
11772smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11773{
11774 struct http_txn *txn = smp->strm->txn;
11775 struct http_msg *msg;
11776 unsigned long len;
11777 unsigned long block1;
11778 char *body;
11779 const char *name;
11780 int name_len;
11781
11782 if (!args || (args[0].type && args[0].type != ARGT_STR))
11783 return 0;
11784
11785 name = "";
11786 name_len = 0;
11787 if (args[0].type == ARGT_STR) {
11788 name = args[0].data.str.str;
11789 name_len = args[0].data.str.len;
11790 }
11791
11792 if (!smp->ctx.a[0]) { // first call, find the query string
11793 CHECK_HTTP_MESSAGE_FIRST();
11794
11795 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
11796 msg = &txn->req;
11797 else
11798 msg = &txn->rsp;
11799
11800 len = http_body_bytes(msg);
11801 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11802
11803 block1 = len;
11804 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11805 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11806
11807 if (block1 == len) {
11808 /* buffer is not wrapped (or empty) */
11809 smp->ctx.a[0] = body;
11810 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011811
11812 /* Assume that the context is filled with NULL pointer
11813 * before the first call.
11814 * smp->ctx.a[2] = NULL;
11815 * smp->ctx.a[3] = NULL;
11816 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011817 }
11818 else {
11819 /* buffer is wrapped, we need to defragment it */
11820 smp->ctx.a[0] = body;
11821 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011822 smp->ctx.a[2] = msg->chn->buf->data;
11823 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011824 }
11825 }
11826 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11827}
11828
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011829/* Return the signed integer value for the specified url parameter (see url_param
11830 * above).
11831 */
11832static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011833smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011834{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011835 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011836
11837 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011838 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011839 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011840 }
11841
11842 return ret;
11843}
11844
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011845/* This produces a 32-bit hash of the concatenation of the first occurrence of
11846 * the Host header followed by the path component if it begins with a slash ('/').
11847 * This means that '*' will not be added, resulting in exactly the first Host
11848 * entry. If no Host header is found, then the path is used. The resulting value
11849 * is hashed using the url hash followed by a full avalanche hash and provides a
11850 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11851 * high-traffic sites without having to store whole paths.
11852 * this differs from the base32 functions in that it includes the url parameters
11853 * as well as the path
11854 */
11855static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011856smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011857{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011858 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011859 struct hdr_ctx ctx;
11860 unsigned int hash = 0;
11861 char *ptr, *beg, *end;
11862 int len;
11863
11864 CHECK_HTTP_MESSAGE_FIRST();
11865
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011866 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011867 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011868 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011869 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11870 ptr = ctx.line + ctx.val;
11871 len = ctx.vlen;
11872 while (len--)
11873 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11874 }
11875
11876 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011877 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 +000011878 beg = http_get_path(txn);
11879 if (!beg)
11880 beg = end;
11881
11882 for (ptr = beg; ptr < end ; ptr++);
11883
11884 if (beg < ptr && *beg == '/') {
11885 while (beg < ptr)
11886 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11887 }
11888 hash = full_hash(hash);
11889
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011890 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011891 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011892 smp->flags = SMP_F_VOL_1ST;
11893 return 1;
11894}
11895
11896/* This concatenates the source address with the 32-bit hash of the Host and
11897 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11898 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11899 * on the source address length. The URL hash is stored before the address so
11900 * that in environments where IPv6 is insignificant, truncating the output to
11901 * 8 bytes would still work.
11902 */
11903static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011904smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011905{
11906 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011907 struct connection *cli_conn = objt_conn(smp->sess->origin);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011908 unsigned int hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011909
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011910 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011911 return 0;
11912
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011913 /* The returned hash is a 32 bytes integer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011914 hash = smp->data.u.sint;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011915
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011916 temp = get_trash_chunk();
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011917 memcpy(temp->str + temp->len, &hash, sizeof(hash));
11918 temp->len += sizeof(hash);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011919
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011920 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011921 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011922 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011923 temp->len += 4;
11924 break;
11925 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011926 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011927 temp->len += 16;
11928 break;
11929 default:
11930 return 0;
11931 }
11932
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011933 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011934 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011935 return 1;
11936}
11937
Willy Tarreau185b5c42012-04-26 15:11:51 +020011938/* This function is used to validate the arguments passed to any "hdr" fetch
11939 * keyword. These keywords support an optional positive or negative occurrence
11940 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11941 * is assumed that the types are already the correct ones. Returns 0 on error,
11942 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11943 * error message in case of error, that the caller is responsible for freeing.
11944 * The initial location must either be freeable or NULL.
11945 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011946int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011947{
11948 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011949 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011950 return 0;
11951 }
11952 return 1;
11953}
11954
Willy Tarreau276fae92013-07-25 14:36:01 +020011955/* takes an UINT value on input supposed to represent the time since EPOCH,
11956 * adds an optional offset found in args[0] and emits a string representing
11957 * the date in RFC-1123/5322 format.
11958 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011959static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011960{
11961 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11962 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11963 struct chunk *temp;
11964 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011965 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011966 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011967
11968 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011969 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011970 curr_date += args[0].data.sint;
11971
11972 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011973 if (!tm)
11974 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011975
11976 temp = get_trash_chunk();
11977 temp->len = snprintf(temp->str, temp->size - temp->len,
11978 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11979 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11980 tm->tm_hour, tm->tm_min, tm->tm_sec);
11981
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011982 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011983 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011984 return 1;
11985}
11986
Thierry FOURNIERad903512014-04-11 17:51:01 +020011987/* Match language range with language tag. RFC2616 14.4:
11988 *
11989 * A language-range matches a language-tag if it exactly equals
11990 * the tag, or if it exactly equals a prefix of the tag such
11991 * that the first tag character following the prefix is "-".
11992 *
11993 * Return 1 if the strings match, else return 0.
11994 */
11995static inline int language_range_match(const char *range, int range_len,
11996 const char *tag, int tag_len)
11997{
11998 const char *end = range + range_len;
11999 const char *tend = tag + tag_len;
12000 while (range < end) {
12001 if (*range == '-' && tag == tend)
12002 return 1;
12003 if (*range != *tag || tag == tend)
12004 return 0;
12005 range++;
12006 tag++;
12007 }
12008 /* Return true only if the last char of the tag is matched. */
12009 return tag == tend;
12010}
12011
12012/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012013static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012014{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012015 const char *al = smp->data.u.str.str;
12016 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012017 const char *token;
12018 int toklen;
12019 int qvalue;
12020 const char *str;
12021 const char *w;
12022 int best_q = 0;
12023
12024 /* Set the constant to the sample, because the output of the
12025 * function will be peek in the constant configuration string.
12026 */
12027 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012028 smp->data.u.str.size = 0;
12029 smp->data.u.str.str = "";
12030 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012031
12032 /* Parse the accept language */
12033 while (1) {
12034
12035 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012036 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012037 al++;
12038 if (al >= end)
12039 break;
12040
12041 /* Start of the fisrt word. */
12042 token = al;
12043
12044 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012045 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012046 al++;
12047 if (al == token)
12048 goto expect_comma;
12049
12050 /* Length of the token. */
12051 toklen = al - token;
12052 qvalue = 1000;
12053
12054 /* Check if the token exists in the list. If the token not exists,
12055 * jump to the next token.
12056 */
12057 str = args[0].data.str.str;
12058 w = str;
12059 while (1) {
12060 if (*str == ';' || *str == '\0') {
12061 if (language_range_match(token, toklen, w, str-w))
12062 goto look_for_q;
12063 if (*str == '\0')
12064 goto expect_comma;
12065 w = str + 1;
12066 }
12067 str++;
12068 }
12069 goto expect_comma;
12070
12071look_for_q:
12072
12073 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012074 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012075 al++;
12076 if (al >= end)
12077 goto process_value;
12078
12079 /* If ',' is found, process the result */
12080 if (*al == ',')
12081 goto process_value;
12082
12083 /* If the character is different from ';', look
12084 * for the end of the header part in best effort.
12085 */
12086 if (*al != ';')
12087 goto expect_comma;
12088
12089 /* Assumes that the char is ';', now expect "q=". */
12090 al++;
12091
12092 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012093 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012094 al++;
12095 if (al >= end)
12096 goto process_value;
12097
12098 /* Expect 'q'. If no 'q', continue in best effort */
12099 if (*al != 'q')
12100 goto process_value;
12101 al++;
12102
12103 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012104 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012105 al++;
12106 if (al >= end)
12107 goto process_value;
12108
12109 /* Expect '='. If no '=', continue in best effort */
12110 if (*al != '=')
12111 goto process_value;
12112 al++;
12113
12114 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012115 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012116 al++;
12117 if (al >= end)
12118 goto process_value;
12119
12120 /* Parse the q value. */
12121 qvalue = parse_qvalue(al, &al);
12122
12123process_value:
12124
12125 /* If the new q value is the best q value, then store the associated
12126 * language in the response. If qvalue is the biggest value (1000),
12127 * break the process.
12128 */
12129 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012130 smp->data.u.str.str = (char *)w;
12131 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012132 if (qvalue >= 1000)
12133 break;
12134 best_q = qvalue;
12135 }
12136
12137expect_comma:
12138
12139 /* Expect comma or end. If the end is detected, quit the loop. */
12140 while (al < end && *al != ',')
12141 al++;
12142 if (al >= end)
12143 break;
12144
12145 /* Comma is found, jump it and restart the analyzer. */
12146 al++;
12147 }
12148
12149 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012150 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12151 smp->data.u.str.str = args[1].data.str.str;
12152 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012153 }
12154
12155 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012156 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012157}
12158
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012159/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012160static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012161{
12162 /* If the constant flag is set or if not size is avalaible at
12163 * the end of the buffer, copy the string in other buffer
12164 * before decoding.
12165 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012166 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012167 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012168 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12169 smp->data.u.str.str = str->str;
12170 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012171 smp->flags &= ~SMP_F_CONST;
12172 }
12173
12174 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012175 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12176 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012177 return 1;
12178}
12179
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012180static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12181{
12182 struct proxy *fe = strm_fe(smp->strm);
12183 int idx, i;
12184 struct cap_hdr *hdr;
12185 int len;
12186
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012187 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012188 return 0;
12189
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012190 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012191
12192 /* Check the availibity of the capture id. */
12193 if (idx > fe->nb_req_cap - 1)
12194 return 0;
12195
12196 /* Look for the original configuration. */
12197 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12198 hdr != NULL && i != idx ;
12199 i--, hdr = hdr->next);
12200 if (!hdr)
12201 return 0;
12202
12203 /* check for the memory allocation */
12204 if (smp->strm->req_cap[hdr->index] == NULL)
12205 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12206 if (smp->strm->req_cap[hdr->index] == NULL)
12207 return 0;
12208
12209 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012210 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012211 if (len > hdr->len)
12212 len = hdr->len;
12213
12214 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012215 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012216 smp->strm->req_cap[idx][len] = '\0';
12217
12218 return 1;
12219}
12220
12221static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12222{
12223 struct proxy *fe = strm_fe(smp->strm);
12224 int idx, i;
12225 struct cap_hdr *hdr;
12226 int len;
12227
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012228 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012229 return 0;
12230
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012231 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012232
12233 /* Check the availibity of the capture id. */
12234 if (idx > fe->nb_rsp_cap - 1)
12235 return 0;
12236
12237 /* Look for the original configuration. */
12238 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12239 hdr != NULL && i != idx ;
12240 i--, hdr = hdr->next);
12241 if (!hdr)
12242 return 0;
12243
12244 /* check for the memory allocation */
12245 if (smp->strm->res_cap[hdr->index] == NULL)
12246 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12247 if (smp->strm->res_cap[hdr->index] == NULL)
12248 return 0;
12249
12250 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012251 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012252 if (len > hdr->len)
12253 len = hdr->len;
12254
12255 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012256 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012257 smp->strm->res_cap[idx][len] = '\0';
12258
12259 return 1;
12260}
12261
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012262/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012263 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012264 * the relevant part of the request line accordingly. Then it updates various
12265 * pointers to the next elements which were moved, and the total buffer length.
12266 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012267 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12268 * error, though this can be revisited when this code is finally exploited.
12269 *
12270 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12271 * query string and 3 to replace uri.
12272 *
12273 * In query string case, the mark question '?' must be set at the start of the
12274 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012275 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012276int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012277 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012278{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012279 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012280 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012281 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012282 int delta;
12283
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012284 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012285 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012286 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012287 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12288
12289 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012290 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012291 txn->req.sl.rq.m_l += delta;
12292 txn->req.sl.rq.u += delta;
12293 txn->req.sl.rq.v += delta;
12294 break;
12295
12296 case 1: // path
12297 cur_ptr = http_get_path(txn);
12298 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012299 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012300
12301 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012302 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 +010012303 cur_end++;
12304
12305 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012306 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012307 txn->req.sl.rq.u_l += delta;
12308 txn->req.sl.rq.v += delta;
12309 break;
12310
12311 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012312 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012313 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012314 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12315 while (cur_ptr < cur_end && *cur_ptr != '?')
12316 cur_ptr++;
12317
12318 /* skip the question mark or indicate that we must insert it
12319 * (but only if the format string is not empty then).
12320 */
12321 if (cur_ptr < cur_end)
12322 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012323 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012324 offset = 0;
12325
12326 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012327 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012328 txn->req.sl.rq.u_l += delta;
12329 txn->req.sl.rq.v += delta;
12330 break;
12331
12332 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012333 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012334 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12335
12336 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012337 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012338 txn->req.sl.rq.u_l += delta;
12339 txn->req.sl.rq.v += delta;
12340 break;
12341
12342 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012343 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012344 }
12345
12346 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012347 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012348 txn->req.sl.rq.l += delta;
12349 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012350 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012351 return 0;
12352}
12353
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012354/* This function replace the HTTP status code and the associated message. The
12355 * variable <status> contains the new status code. This function never fails.
12356 */
12357void http_set_status(unsigned int status, struct stream *s)
12358{
12359 struct http_txn *txn = s->txn;
12360 char *cur_ptr, *cur_end;
12361 int delta;
12362 char *res;
12363 int c_l;
12364 const char *msg;
12365 int msg_len;
12366
12367 chunk_reset(&trash);
12368
12369 res = ultoa_o(status, trash.str, trash.size);
12370 c_l = res - trash.str;
12371
12372 trash.str[c_l] = ' ';
12373 trash.len = c_l + 1;
12374
12375 msg = get_reason(status);
12376 msg_len = strlen(msg);
12377
12378 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12379 trash.len += msg_len;
12380
12381 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12382 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12383
12384 /* commit changes and adjust message */
12385 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12386
12387 /* adjust res line offsets and lengths */
12388 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12389 txn->rsp.sl.st.c_l = c_l;
12390 txn->rsp.sl.st.r_l = msg_len;
12391
12392 delta = trash.len - (cur_end - cur_ptr);
12393 txn->rsp.sl.st.l += delta;
12394 txn->hdr_idx.v[0].len += delta;
12395 http_msg_move_end(&txn->rsp, delta);
12396}
12397
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012398/* This function executes one of the set-{method,path,query,uri} actions. It
12399 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012400 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012401 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012402 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12403 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012404 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012405enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012406 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012407{
12408 chunk_reset(&trash);
12409
12410 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012411 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012412 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012413 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012414
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012415 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012416 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012417}
12418
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012419/* This function is just a compliant action wrapper for "set-status". */
12420enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012421 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012422{
12423 http_set_status(rule->arg.status.code, s);
12424 return ACT_RET_CONT;
12425}
12426
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012427/* parse an http-request action among :
12428 * set-method
12429 * set-path
12430 * set-query
12431 * set-uri
12432 *
12433 * All of them accept a single argument of type string representing a log-format.
12434 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12435 * 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 +020012436 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012437 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012438enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12439 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012440{
12441 int cur_arg = *orig_arg;
12442
Thierry FOURNIER42148732015-09-02 17:17:33 +020012443 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012444
12445 switch (args[0][4]) {
12446 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012447 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012448 rule->action_ptr = http_action_set_req_line;
12449 break;
12450 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012451 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012452 rule->action_ptr = http_action_set_req_line;
12453 break;
12454 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012455 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012456 rule->action_ptr = http_action_set_req_line;
12457 break;
12458 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012459 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012460 rule->action_ptr = http_action_set_req_line;
12461 break;
12462 default:
12463 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012464 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012465 }
12466
12467 if (!*args[cur_arg] ||
12468 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12469 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012470 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012471 }
12472
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012473 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012474 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012475 parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012476 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12477 proxy->conf.args.file, proxy->conf.args.line);
12478
12479 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012480 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012481}
12482
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012483/* parse set-status action:
12484 * This action accepts a single argument of type int representing
12485 * an http status code. It returns ACT_RET_PRS_OK on success,
12486 * ACT_RET_PRS_ERR on error.
12487 */
12488enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12489 struct act_rule *rule, char **err)
12490{
12491 char *error;
12492
Thierry FOURNIER42148732015-09-02 17:17:33 +020012493 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012494 rule->action_ptr = action_http_set_status;
12495
12496 /* Check if an argument is available */
12497 if (!*args[*orig_arg]) {
12498 memprintf(err, "expects exactly 1 argument <status>");
12499 return ACT_RET_PRS_ERR;
12500 }
12501
12502 /* convert status code as integer */
12503 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12504 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12505 memprintf(err, "expects an integer status code between 100 and 999");
12506 return ACT_RET_PRS_ERR;
12507 }
12508
12509 (*orig_arg)++;
12510 return ACT_RET_PRS_OK;
12511}
12512
Willy Tarreaua9083d02015-05-08 15:27:59 +020012513/* This function executes the "capture" action. It executes a fetch expression,
12514 * turns the result into a string and puts it in a capture slot. It always
12515 * returns 1. If an error occurs the action is cancelled, but the rule
12516 * processing continues.
12517 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012518enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012519 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012520{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012521 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012522 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012523 char **cap = s->req_cap;
12524 int len;
12525
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012526 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 +020012527 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012528 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012529
12530 if (cap[h->index] == NULL)
12531 cap[h->index] = pool_alloc2(h->pool);
12532
12533 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012534 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012535
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012536 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012537 if (len > h->len)
12538 len = h->len;
12539
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012540 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012541 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012542 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012543}
12544
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012545/* This function executes the "capture" action and store the result in a
12546 * capture slot if exists. It executes a fetch expression, turns the result
12547 * into a string and puts it in a capture slot. It always returns 1. If an
12548 * error occurs the action is cancelled, but the rule processing continues.
12549 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012550enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012551 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012552{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012553 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012554 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012555 char **cap = s->req_cap;
12556 struct proxy *fe = strm_fe(s);
12557 int len;
12558 int i;
12559
12560 /* Look for the original configuration. */
12561 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012562 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012563 i--, h = h->next);
12564 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012565 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012566
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012567 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 +020012568 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012569 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012570
12571 if (cap[h->index] == NULL)
12572 cap[h->index] = pool_alloc2(h->pool);
12573
12574 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012575 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012576
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012577 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012578 if (len > h->len)
12579 len = h->len;
12580
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012581 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012582 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012583 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012584}
12585
Willy Tarreaua9083d02015-05-08 15:27:59 +020012586/* parse an "http-request capture" action. It takes a single argument which is
12587 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012588 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012589 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012590 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012591enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12592 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012593{
12594 struct sample_expr *expr;
12595 struct cap_hdr *hdr;
12596 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012597 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012598
12599 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12600 if (strcmp(args[cur_arg], "if") == 0 ||
12601 strcmp(args[cur_arg], "unless") == 0)
12602 break;
12603
12604 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012605 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012606 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012607 }
12608
Willy Tarreaua9083d02015-05-08 15:27:59 +020012609 cur_arg = *orig_arg;
12610 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12611 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012612 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012613
12614 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12615 memprintf(err,
12616 "fetch method '%s' extracts information from '%s', none of which is available here",
12617 args[cur_arg-1], sample_src_names(expr->fetch->use));
12618 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012619 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012620 }
12621
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012622 if (!args[cur_arg] || !*args[cur_arg]) {
12623 memprintf(err, "expects 'len or 'id'");
12624 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012625 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012626 }
12627
Willy Tarreaua9083d02015-05-08 15:27:59 +020012628 if (strcmp(args[cur_arg], "len") == 0) {
12629 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012630
12631 if (!(px->cap & PR_CAP_FE)) {
12632 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012633 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012634 }
12635
12636 proxy->conf.args.ctx = ARGC_CAP;
12637
Willy Tarreaua9083d02015-05-08 15:27:59 +020012638 if (!args[cur_arg]) {
12639 memprintf(err, "missing length value");
12640 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012641 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012642 }
12643 /* we copy the table name for now, it will be resolved later */
12644 len = atoi(args[cur_arg]);
12645 if (len <= 0) {
12646 memprintf(err, "length must be > 0");
12647 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012648 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012649 }
12650 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012651
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012652 if (!len) {
12653 memprintf(err, "a positive 'len' argument is mandatory");
12654 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012655 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012656 }
12657
12658 hdr = calloc(sizeof(struct cap_hdr), 1);
12659 hdr->next = px->req_cap;
12660 hdr->name = NULL; /* not a header capture */
12661 hdr->namelen = 0;
12662 hdr->len = len;
12663 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12664 hdr->index = px->nb_req_cap++;
12665
12666 px->req_cap = hdr;
12667 px->to_log |= LW_REQHDR;
12668
Thierry FOURNIER42148732015-09-02 17:17:33 +020012669 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012670 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012671 rule->arg.cap.expr = expr;
12672 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012673 }
12674
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012675 else if (strcmp(args[cur_arg], "id") == 0) {
12676 int id;
12677 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012678
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012679 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012680
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012681 if (!args[cur_arg]) {
12682 memprintf(err, "missing id value");
12683 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012684 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012685 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012686
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012687 id = strtol(args[cur_arg], &error, 10);
12688 if (*error != '\0') {
12689 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12690 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012691 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012692 }
12693 cur_arg++;
12694
12695 proxy->conf.args.ctx = ARGC_CAP;
12696
Thierry FOURNIER42148732015-09-02 17:17:33 +020012697 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012698 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012699 rule->arg.capid.expr = expr;
12700 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012701 }
12702
12703 else {
12704 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12705 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012706 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012707 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012708
12709 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012710 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012711}
12712
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012713/* This function executes the "capture" action and store the result in a
12714 * capture slot if exists. It executes a fetch expression, turns the result
12715 * into a string and puts it in a capture slot. It always returns 1. If an
12716 * error occurs the action is cancelled, but the rule processing continues.
12717 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012718enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012719 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012720{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012721 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012722 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012723 char **cap = s->res_cap;
12724 struct proxy *fe = strm_fe(s);
12725 int len;
12726 int i;
12727
12728 /* Look for the original configuration. */
12729 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012730 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012731 i--, h = h->next);
12732 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012733 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012734
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012735 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 +020012736 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012737 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012738
12739 if (cap[h->index] == NULL)
12740 cap[h->index] = pool_alloc2(h->pool);
12741
12742 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012743 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012744
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012745 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012746 if (len > h->len)
12747 len = h->len;
12748
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012749 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012750 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012751 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012752}
12753
12754/* parse an "http-response capture" action. It takes a single argument which is
12755 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12756 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012757 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012758 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012759enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12760 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012761{
12762 struct sample_expr *expr;
12763 int cur_arg;
12764 int id;
12765 char *error;
12766
12767 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12768 if (strcmp(args[cur_arg], "if") == 0 ||
12769 strcmp(args[cur_arg], "unless") == 0)
12770 break;
12771
12772 if (cur_arg < *orig_arg + 3) {
12773 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012774 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012775 }
12776
12777 cur_arg = *orig_arg;
12778 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12779 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012780 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012781
12782 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12783 memprintf(err,
12784 "fetch method '%s' extracts information from '%s', none of which is available here",
12785 args[cur_arg-1], sample_src_names(expr->fetch->use));
12786 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012787 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012788 }
12789
12790 if (!args[cur_arg] || !*args[cur_arg]) {
12791 memprintf(err, "expects 'len or 'id'");
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 (strcmp(args[cur_arg], "id") != 0) {
12797 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
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 cur_arg++;
12803
12804 if (!args[cur_arg]) {
12805 memprintf(err, "missing id value");
12806 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012807 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012808 }
12809
12810 id = strtol(args[cur_arg], &error, 10);
12811 if (*error != '\0') {
12812 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12813 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012814 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012815 }
12816 cur_arg++;
12817
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012818 proxy->conf.args.ctx = ARGC_CAP;
12819
Thierry FOURNIER42148732015-09-02 17:17:33 +020012820 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012821 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012822 rule->arg.capid.expr = expr;
12823 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012824
12825 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012826 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012827}
12828
William Lallemand73025dd2014-04-24 14:38:37 +020012829/*
12830 * Return the struct http_req_action_kw associated to a keyword.
12831 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012832struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012833{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012834 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012835}
12836
12837/*
12838 * Return the struct http_res_action_kw associated to a keyword.
12839 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012840struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012841{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012842 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012843}
12844
Willy Tarreau4a568972010-05-12 08:08:50 +020012845/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012846/* All supported ACL keywords must be declared here. */
12847/************************************************************************/
12848
12849/* Note: must not be declared <const> as its list will be overwritten.
12850 * Please take care of keeping this list alphabetically sorted.
12851 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012852static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012853 { "base", "base", PAT_MATCH_STR },
12854 { "base_beg", "base", PAT_MATCH_BEG },
12855 { "base_dir", "base", PAT_MATCH_DIR },
12856 { "base_dom", "base", PAT_MATCH_DOM },
12857 { "base_end", "base", PAT_MATCH_END },
12858 { "base_len", "base", PAT_MATCH_LEN },
12859 { "base_reg", "base", PAT_MATCH_REG },
12860 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012861
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012862 { "cook", "req.cook", PAT_MATCH_STR },
12863 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12864 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12865 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12866 { "cook_end", "req.cook", PAT_MATCH_END },
12867 { "cook_len", "req.cook", PAT_MATCH_LEN },
12868 { "cook_reg", "req.cook", PAT_MATCH_REG },
12869 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012870
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012871 { "hdr", "req.hdr", PAT_MATCH_STR },
12872 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12873 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12874 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12875 { "hdr_end", "req.hdr", PAT_MATCH_END },
12876 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12877 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12878 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012879
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012880 /* these two declarations uses strings with list storage (in place
12881 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12882 * and delete functions are relative to the list management. The parse
12883 * and match method are related to the corresponding fetch methods. This
12884 * is very particular ACL declaration mode.
12885 */
12886 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12887 { "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 +020012888
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012889 { "path", "path", PAT_MATCH_STR },
12890 { "path_beg", "path", PAT_MATCH_BEG },
12891 { "path_dir", "path", PAT_MATCH_DIR },
12892 { "path_dom", "path", PAT_MATCH_DOM },
12893 { "path_end", "path", PAT_MATCH_END },
12894 { "path_len", "path", PAT_MATCH_LEN },
12895 { "path_reg", "path", PAT_MATCH_REG },
12896 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012897
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012898 { "req_ver", "req.ver", PAT_MATCH_STR },
12899 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012900
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012901 { "scook", "res.cook", PAT_MATCH_STR },
12902 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12903 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12904 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12905 { "scook_end", "res.cook", PAT_MATCH_END },
12906 { "scook_len", "res.cook", PAT_MATCH_LEN },
12907 { "scook_reg", "res.cook", PAT_MATCH_REG },
12908 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012909
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012910 { "shdr", "res.hdr", PAT_MATCH_STR },
12911 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12912 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12913 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12914 { "shdr_end", "res.hdr", PAT_MATCH_END },
12915 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12916 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12917 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012918
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012919 { "url", "url", PAT_MATCH_STR },
12920 { "url_beg", "url", PAT_MATCH_BEG },
12921 { "url_dir", "url", PAT_MATCH_DIR },
12922 { "url_dom", "url", PAT_MATCH_DOM },
12923 { "url_end", "url", PAT_MATCH_END },
12924 { "url_len", "url", PAT_MATCH_LEN },
12925 { "url_reg", "url", PAT_MATCH_REG },
12926 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012927
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012928 { "urlp", "urlp", PAT_MATCH_STR },
12929 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12930 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12931 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12932 { "urlp_end", "urlp", PAT_MATCH_END },
12933 { "urlp_len", "urlp", PAT_MATCH_LEN },
12934 { "urlp_reg", "urlp", PAT_MATCH_REG },
12935 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012936
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012937 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012938}};
12939
12940/************************************************************************/
12941/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012942/************************************************************************/
12943/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012944static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012945 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012946 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012947 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12948
Willy Tarreau87b09662015-04-03 00:22:06 +020012949 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012950 { "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 +020012951
12952 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012953 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12954 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12955 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012956
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012957 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12958 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012959
Willy Tarreau409bcde2013-01-08 00:31:00 +010012960 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12961 * are only here to match the ACL's name, are request-only and are used
12962 * for ACL compatibility only.
12963 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012964 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12965 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012966 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12967 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012968
12969 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12970 * only here to match the ACL's name, are request-only and are used for
12971 * ACL compatibility only.
12972 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012973 { "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 +020012974 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012975 { "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 +020012976 { "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 +010012977
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012978 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012979 { "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 +010012980 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012981 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012982 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012983 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012984
12985 /* HTTP protocol on the request path */
12986 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012987 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012988
12989 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012990 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12991 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012992
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012993 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012994 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12995 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012996 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012997
Willy Tarreau18ed2562013-01-14 15:56:36 +010012998 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012999 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13000 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013001
Willy Tarreau18ed2562013-01-14 15:56:36 +010013002 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013003 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013004 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13005 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013006
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013007 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013008 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013009 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013010 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013011 { "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 +010013012 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013013 { "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 +010013014
13015 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013016 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013017 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13018 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013019
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013020 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013021 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013022 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013023 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013024 { "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 +010013025 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013026 { "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 +010013027
Willy Tarreau409bcde2013-01-08 00:31:00 +010013028 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013029 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013030 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13031 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013032 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013033
13034 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013035 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013036 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013037 { "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 +020013038 { "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 +010013039
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013040 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013041 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013042 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013043 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013044 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013045 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013046 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13047 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013048 { "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 +010013049 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013050}};
13051
Willy Tarreau8797c062007-05-07 00:55:35 +020013052
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013053/************************************************************************/
13054/* All supported converter keywords must be declared here. */
13055/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013056/* Note: must not be declared <const> as its list will be overwritten */
13057static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013058 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013059 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013060 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13061 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013062 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013063 { NULL, NULL, 0, 0, 0 },
13064}};
13065
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013066
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013067/************************************************************************/
13068/* All supported http-request action keywords must be declared here. */
13069/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013070struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013071 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013072 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013073 { "set-method", parse_set_req_line },
13074 { "set-path", parse_set_req_line },
13075 { "set-query", parse_set_req_line },
13076 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013077 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013078 }
13079};
13080
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013081struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013082 .kw = {
13083 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013084 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013085 { NULL, NULL }
13086 }
13087};
13088
Willy Tarreau8797c062007-05-07 00:55:35 +020013089__attribute__((constructor))
13090static void __http_protocol_init(void)
13091{
13092 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013093 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013094 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013095 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013096 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020013097}
13098
13099
Willy Tarreau58f10d72006-12-04 02:26:12 +010013100/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013101 * Local variables:
13102 * c-indent-level: 8
13103 * c-basic-offset: 8
13104 * End:
13105 */