blob: 9acf0a2eb76eb14591fa47a14ab5338a21ca5336 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreau8797c062007-05-07 00:55:35 +020044#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020045#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020064#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020068#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020069
Willy Tarreau522d6c02009-12-06 18:49:18 +010070const char HTTP_100[] =
71 "HTTP/1.1 100 Continue\r\n\r\n";
72
73const struct chunk http_100_chunk = {
74 .str = (char *)&HTTP_100,
75 .len = sizeof(HTTP_100)-1
76};
77
Willy Tarreaua9679ac2010-01-03 17:32:57 +010078/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020079const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010081 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020082 "Location: "; /* not terminated since it will be concatenated with the URL */
83
Willy Tarreau0f772532006-12-23 20:51:41 +010084const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010088 "Location: "; /* not terminated since it will be concatenated with the URL */
89
90/* same as 302 except that the browser MUST retry with the GET method */
91const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010094 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010095 "Location: "; /* not terminated since it will be concatenated with the URL */
96
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040097
98/* same as 302 except that the browser MUST retry with the same method */
99const char *HTTP_307 =
100 "HTTP/1.1 307 Temporary Redirect\r\n"
101 "Cache-Control: no-cache\r\n"
102 "Content-length: 0\r\n"
103 "Location: "; /* not terminated since it will be concatenated with the URL */
104
105/* same as 301 except that the browser MUST retry with the same method */
106const char *HTTP_308 =
107 "HTTP/1.1 308 Permanent Redirect\r\n"
108 "Content-length: 0\r\n"
109 "Location: "; /* not terminated since it will be concatenated with the URL */
110
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
112const char *HTTP_401_fmt =
113 "HTTP/1.0 401 Unauthorized\r\n"
114 "Cache-Control: no-cache\r\n"
115 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200116 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
118 "\r\n"
119 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
120
Willy Tarreau844a7e72010-01-31 21:46:18 +0100121const char *HTTP_407_fmt =
122 "HTTP/1.0 407 Unauthorized\r\n"
123 "Cache-Control: no-cache\r\n"
124 "Connection: close\r\n"
125 "Content-Type: text/html\r\n"
126 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
127 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800128 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100129
Willy Tarreau0f772532006-12-23 20:51:41 +0100130
131const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200132 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100133 [HTTP_ERR_400] = 400,
134 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400135 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100136 [HTTP_ERR_408] = 408,
CJ Ess108b1dd2015-04-07 12:03:37 -0400137 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 [HTTP_ERR_500] = 500,
139 [HTTP_ERR_502] = 502,
140 [HTTP_ERR_503] = 503,
141 [HTTP_ERR_504] = 504,
142};
143
Willy Tarreau80587432006-12-24 17:47:20 +0100144static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200145 [HTTP_ERR_200] =
146 "HTTP/1.0 200 OK\r\n"
147 "Cache-Control: no-cache\r\n"
148 "Connection: close\r\n"
149 "Content-Type: text/html\r\n"
150 "\r\n"
151 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
152
Willy Tarreau0f772532006-12-23 20:51:41 +0100153 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100154 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100155 "Cache-Control: no-cache\r\n"
156 "Connection: close\r\n"
157 "Content-Type: text/html\r\n"
158 "\r\n"
159 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
160
161 [HTTP_ERR_403] =
162 "HTTP/1.0 403 Forbidden\r\n"
163 "Cache-Control: no-cache\r\n"
164 "Connection: close\r\n"
165 "Content-Type: text/html\r\n"
166 "\r\n"
167 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
168
CJ Ess108b1dd2015-04-07 12:03:37 -0400169 [HTTP_ERR_405] =
170 "HTTP/1.0 405 Method Not Allowed\r\n"
171 "Cache-Control: no-cache\r\n"
172 "Connection: close\r\n"
173 "Content-Type: text/html\r\n"
174 "\r\n"
175 "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n",
176
Willy Tarreau0f772532006-12-23 20:51:41 +0100177 [HTTP_ERR_408] =
178 "HTTP/1.0 408 Request Time-out\r\n"
179 "Cache-Control: no-cache\r\n"
180 "Connection: close\r\n"
181 "Content-Type: text/html\r\n"
182 "\r\n"
183 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
184
CJ Ess108b1dd2015-04-07 12:03:37 -0400185 [HTTP_ERR_429] =
186 "HTTP/1.0 429 Too Many Requests\r\n"
187 "Cache-Control: no-cache\r\n"
188 "Connection: close\r\n"
189 "Content-Type: text/html\r\n"
190 "\r\n"
191 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
192
Willy Tarreau0f772532006-12-23 20:51:41 +0100193 [HTTP_ERR_500] =
194 "HTTP/1.0 500 Server Error\r\n"
195 "Cache-Control: no-cache\r\n"
196 "Connection: close\r\n"
197 "Content-Type: text/html\r\n"
198 "\r\n"
199 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
200
201 [HTTP_ERR_502] =
202 "HTTP/1.0 502 Bad Gateway\r\n"
203 "Cache-Control: no-cache\r\n"
204 "Connection: close\r\n"
205 "Content-Type: text/html\r\n"
206 "\r\n"
207 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
208
209 [HTTP_ERR_503] =
210 "HTTP/1.0 503 Service Unavailable\r\n"
211 "Cache-Control: no-cache\r\n"
212 "Connection: close\r\n"
213 "Content-Type: text/html\r\n"
214 "\r\n"
215 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
216
217 [HTTP_ERR_504] =
218 "HTTP/1.0 504 Gateway Time-out\r\n"
219 "Cache-Control: no-cache\r\n"
220 "Connection: close\r\n"
221 "Content-Type: text/html\r\n"
222 "\r\n"
223 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
224
225};
226
Cyril Bonté19979e12012-04-04 12:57:21 +0200227/* status codes available for the stats admin page (strictly 4 chars length) */
228const char *stat_status_codes[STAT_STATUS_SIZE] = {
229 [STAT_STATUS_DENY] = "DENY",
230 [STAT_STATUS_DONE] = "DONE",
231 [STAT_STATUS_ERRP] = "ERRP",
232 [STAT_STATUS_EXCD] = "EXCD",
233 [STAT_STATUS_NONE] = "NONE",
234 [STAT_STATUS_PART] = "PART",
235 [STAT_STATUS_UNKN] = "UNKN",
236};
237
238
William Lallemand73025dd2014-04-24 14:38:37 +0200239/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200240struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200241 .list = LIST_HEAD_INIT(http_req_keywords.list)
242};
243
244/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200245struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200246 .list = LIST_HEAD_INIT(http_res_keywords.list)
247};
248
Willy Tarreau80587432006-12-24 17:47:20 +0100249/* We must put the messages here since GCC cannot initialize consts depending
250 * on strlen().
251 */
252struct chunk http_err_chunks[HTTP_ERR_SIZE];
253
Willy Tarreaua890d072013-04-02 12:01:06 +0200254/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
255static struct hdr_ctx static_hdr_ctx;
256
Willy Tarreau42250582007-04-01 01:30:43 +0200257#define FD_SETS_ARE_BITFIELDS
258#ifdef FD_SETS_ARE_BITFIELDS
259/*
260 * This map is used with all the FD_* macros to check whether a particular bit
261 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
262 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
263 * byte should be encoded. Be careful to always pass bytes from 0 to 255
264 * exclusively to the macros.
265 */
266fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
267fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100268fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200269
270#else
271#error "Check if your OS uses bitfields for fd_sets"
272#endif
273
Willy Tarreau87b09662015-04-03 00:22:06 +0200274static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200275
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200276/* This function returns a reason associated with the HTTP status.
277 * This function never fails, a message is always returned.
278 */
279const char *get_reason(unsigned int status)
280{
281 switch (status) {
282 case 100: return "Continue";
283 case 101: return "Switching Protocols";
284 case 102: return "Processing";
285 case 200: return "OK";
286 case 201: return "Created";
287 case 202: return "Accepted";
288 case 203: return "Non-Authoritative Information";
289 case 204: return "No Content";
290 case 205: return "Reset Content";
291 case 206: return "Partial Content";
292 case 207: return "Multi-Status";
293 case 210: return "Content Different";
294 case 226: return "IM Used";
295 case 300: return "Multiple Choices";
296 case 301: return "Moved Permanently";
297 case 302: return "Moved Temporarily";
298 case 303: return "See Other";
299 case 304: return "Not Modified";
300 case 305: return "Use Proxy";
301 case 307: return "Temporary Redirect";
302 case 308: return "Permanent Redirect";
303 case 310: return "Too many Redirects";
304 case 400: return "Bad Request";
305 case 401: return "Unauthorized";
306 case 402: return "Payment Required";
307 case 403: return "Forbidden";
308 case 404: return "Not Found";
309 case 405: return "Method Not Allowed";
310 case 406: return "Not Acceptable";
311 case 407: return "Proxy Authentication Required";
312 case 408: return "Request Time-out";
313 case 409: return "Conflict";
314 case 410: return "Gone";
315 case 411: return "Length Required";
316 case 412: return "Precondition Failed";
317 case 413: return "Request Entity Too Large";
318 case 414: return "Request-URI Too Long";
319 case 415: return "Unsupported Media Type";
320 case 416: return "Requested range unsatisfiable";
321 case 417: return "Expectation failed";
322 case 418: return "I'm a teapot";
323 case 422: return "Unprocessable entity";
324 case 423: return "Locked";
325 case 424: return "Method failure";
326 case 425: return "Unordered Collection";
327 case 426: return "Upgrade Required";
328 case 428: return "Precondition Required";
329 case 429: return "Too Many Requests";
330 case 431: return "Request Header Fields Too Large";
331 case 449: return "Retry With";
332 case 450: return "Blocked by Windows Parental Controls";
333 case 451: return "Unavailable For Legal Reasons";
334 case 456: return "Unrecoverable Error";
335 case 499: return "client has closed connection";
336 case 500: return "Internal Server Error";
337 case 501: return "Not Implemented";
338 case 502: return "Bad Gateway ou Proxy Error";
339 case 503: return "Service Unavailable";
340 case 504: return "Gateway Time-out";
341 case 505: return "HTTP Version not supported";
342 case 506: return "Variant also negociate";
343 case 507: return "Insufficient storage";
344 case 508: return "Loop detected";
345 case 509: return "Bandwidth Limit Exceeded";
346 case 510: return "Not extended";
347 case 511: return "Network authentication required";
348 case 520: return "Web server is returning an unknown error";
349 default:
350 switch (status) {
351 case 100 ... 199: return "Informational";
352 case 200 ... 299: return "Success";
353 case 300 ... 399: return "Redirection";
354 case 400 ... 499: return "Client Error";
355 case 500 ... 599: return "Server Error";
356 default: return "Other";
357 }
358 }
359}
360
Willy Tarreau80587432006-12-24 17:47:20 +0100361void init_proto_http()
362{
Willy Tarreau42250582007-04-01 01:30:43 +0200363 int i;
364 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100365 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200366
Willy Tarreau80587432006-12-24 17:47:20 +0100367 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
368 if (!http_err_msgs[msg]) {
369 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
370 abort();
371 }
372
373 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
374 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
375 }
Willy Tarreau42250582007-04-01 01:30:43 +0200376
377 /* initialize the log header encoding map : '{|}"#' should be encoded with
378 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
379 * URL encoding only requires '"', '#' to be encoded as well as non-
380 * printable characters above.
381 */
382 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
383 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100384 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200385 for (i = 0; i < 32; i++) {
386 FD_SET(i, hdr_encode_map);
387 FD_SET(i, url_encode_map);
388 }
389 for (i = 127; i < 256; i++) {
390 FD_SET(i, hdr_encode_map);
391 FD_SET(i, url_encode_map);
392 }
393
394 tmp = "\"#{|}";
395 while (*tmp) {
396 FD_SET(*tmp, hdr_encode_map);
397 tmp++;
398 }
399
400 tmp = "\"#";
401 while (*tmp) {
402 FD_SET(*tmp, url_encode_map);
403 tmp++;
404 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200405
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100406 /* initialize the http header encoding map. The draft httpbis define the
407 * header content as:
408 *
409 * HTTP-message = start-line
410 * *( header-field CRLF )
411 * CRLF
412 * [ message-body ]
413 * header-field = field-name ":" OWS field-value OWS
414 * field-value = *( field-content / obs-fold )
415 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
416 * obs-fold = CRLF 1*( SP / HTAB )
417 * field-vchar = VCHAR / obs-text
418 * VCHAR = %x21-7E
419 * obs-text = %x80-FF
420 *
421 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
422 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
423 * "obs-fold" is volontary forgotten because haproxy remove this.
424 */
425 memset(http_encode_map, 0, sizeof(http_encode_map));
426 for (i = 0x00; i <= 0x08; i++)
427 FD_SET(i, http_encode_map);
428 for (i = 0x0a; i <= 0x1f; i++)
429 FD_SET(i, http_encode_map);
430 FD_SET(0x7f, http_encode_map);
431
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200432 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200433 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200434 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100435 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100436}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200437
Willy Tarreau53b6c742006-12-17 13:37:46 +0100438/*
439 * We have 26 list of methods (1 per first letter), each of which can have
440 * up to 3 entries (2 valid, 1 null).
441 */
442struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100443 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100444 int len;
445 const char text[8];
446};
447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100448const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100449 ['C' - 'A'] = {
450 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
451 },
452 ['D' - 'A'] = {
453 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
454 },
455 ['G' - 'A'] = {
456 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
457 },
458 ['H' - 'A'] = {
459 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
460 },
461 ['P' - 'A'] = {
462 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
463 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
464 },
465 ['T' - 'A'] = {
466 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
467 },
468 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200469 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100470 */
471};
472
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100473const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100474 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
475 [HTTP_METH_GET] = { "GET", 3 },
476 [HTTP_METH_HEAD] = { "HEAD", 4 },
477 [HTTP_METH_POST] = { "POST", 4 },
478 [HTTP_METH_PUT] = { "PUT", 3 },
479 [HTTP_METH_DELETE] = { "DELETE", 6 },
480 [HTTP_METH_TRACE] = { "TRACE", 5 },
481 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
482};
483
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100484/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200485 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100486 * RFC2616 for those chars.
487 */
488
489const char http_is_spht[256] = {
490 [' '] = 1, ['\t'] = 1,
491};
492
493const char http_is_crlf[256] = {
494 ['\r'] = 1, ['\n'] = 1,
495};
496
497const char http_is_lws[256] = {
498 [' '] = 1, ['\t'] = 1,
499 ['\r'] = 1, ['\n'] = 1,
500};
501
502const char http_is_sep[256] = {
503 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
504 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
505 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
506 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
507 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
508};
509
510const char http_is_ctl[256] = {
511 [0 ... 31] = 1,
512 [127] = 1,
513};
514
515/*
516 * A token is any ASCII char that is neither a separator nor a CTL char.
517 * Do not overwrite values in assignment since gcc-2.95 will not handle
518 * them correctly. Instead, define every non-CTL char's status.
519 */
520const char http_is_token[256] = {
521 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
522 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
523 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
524 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
525 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
526 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
527 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
528 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
529 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
530 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
531 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
532 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
533 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
534 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
535 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
536 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
537 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
538 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
539 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
540 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
541 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
542 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
543 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
544 ['|'] = 1, ['}'] = 0, ['~'] = 1,
545};
546
547
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100548/*
549 * An http ver_token is any ASCII which can be found in an HTTP version,
550 * which includes 'H', 'T', 'P', '/', '.' and any digit.
551 */
552const char http_is_ver_token[256] = {
553 ['.'] = 1, ['/'] = 1,
554 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
555 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
Thierry FOURNIER63d692c2015-02-28 19:03:56 +0100556 ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1,
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100557};
558
559
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100560/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100561 * Adds a header and its CRLF at the tail of the message's buffer, just before
562 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100563 * The header is also automatically added to the index <hdr_idx>, and the end
564 * of headers is automatically adjusted. The number of bytes added is returned
565 * on success, otherwise <0 is returned indicating an error.
566 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100567int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100568{
569 int bytes, len;
570
571 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200572 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100573 if (!bytes)
574 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100575 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100576 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
577}
578
579/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100580 * Adds a header and its CRLF at the tail of the message's buffer, just before
581 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100582 * the buffer is only opened and the space reserved, but nothing is copied.
583 * The header is also automatically added to the index <hdr_idx>, and the end
584 * of headers is automatically adjusted. The number of bytes added is returned
585 * on success, otherwise <0 is returned indicating an error.
586 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100587int http_header_add_tail2(struct http_msg *msg,
588 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100589{
590 int bytes;
591
Willy Tarreau9b28e032012-10-12 23:49:43 +0200592 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100593 if (!bytes)
594 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100595 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100596 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
597}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200598
599/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100600 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
601 * If so, returns the position of the first non-space character relative to
602 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
603 * to return a pointer to the place after the first space. Returns 0 if the
604 * header name does not match. Checks are case-insensitive.
605 */
606int http_header_match2(const char *hdr, const char *end,
607 const char *name, int len)
608{
609 const char *val;
610
611 if (hdr + len >= end)
612 return 0;
613 if (hdr[len] != ':')
614 return 0;
615 if (strncasecmp(hdr, name, len) != 0)
616 return 0;
617 val = hdr + len + 1;
618 while (val < end && HTTP_IS_SPHT(*val))
619 val++;
620 if ((val >= end) && (len + 2 <= end - hdr))
621 return len + 2; /* we may replace starting from second space */
622 return val - hdr;
623}
624
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200625/* Find the first or next occurrence of header <name> in message buffer <sol>
626 * using headers index <idx>, and return it in the <ctx> structure. This
627 * structure holds everything necessary to use the header and find next
628 * occurrence. If its <idx> member is 0, the header is searched from the
629 * beginning. Otherwise, the next occurrence is returned. The function returns
630 * 1 when it finds a value, and 0 when there is no more. It is very similar to
631 * http_find_header2() except that it is designed to work with full-line headers
632 * whose comma is not a delimiter but is part of the syntax. As a special case,
633 * if ctx->val is NULL when searching for a new values of a header, the current
634 * header is rescanned. This allows rescanning after a header deletion.
635 */
636int http_find_full_header2(const char *name, int len,
637 char *sol, struct hdr_idx *idx,
638 struct hdr_ctx *ctx)
639{
640 char *eol, *sov;
641 int cur_idx, old_idx;
642
643 cur_idx = ctx->idx;
644 if (cur_idx) {
645 /* We have previously returned a header, let's search another one */
646 sol = ctx->line;
647 eol = sol + idx->v[cur_idx].len;
648 goto next_hdr;
649 }
650
651 /* first request for this header */
652 sol += hdr_idx_first_pos(idx);
653 old_idx = 0;
654 cur_idx = hdr_idx_first_idx(idx);
655 while (cur_idx) {
656 eol = sol + idx->v[cur_idx].len;
657
658 if (len == 0) {
659 /* No argument was passed, we want any header.
660 * To achieve this, we simply build a fake request. */
661 while (sol + len < eol && sol[len] != ':')
662 len++;
663 name = sol;
664 }
665
666 if ((len < eol - sol) &&
667 (sol[len] == ':') &&
668 (strncasecmp(sol, name, len) == 0)) {
669 ctx->del = len;
670 sov = sol + len + 1;
671 while (sov < eol && http_is_lws[(unsigned char)*sov])
672 sov++;
673
674 ctx->line = sol;
675 ctx->prev = old_idx;
676 ctx->idx = cur_idx;
677 ctx->val = sov - sol;
678 ctx->tws = 0;
679 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
680 eol--;
681 ctx->tws++;
682 }
683 ctx->vlen = eol - sov;
684 return 1;
685 }
686 next_hdr:
687 sol = eol + idx->v[cur_idx].cr + 1;
688 old_idx = cur_idx;
689 cur_idx = idx->v[cur_idx].next;
690 }
691 return 0;
692}
693
Willy Tarreauc90dc232015-02-20 13:51:36 +0100694/* Find the first or next header field in message buffer <sol> using headers
695 * index <idx>, and return it in the <ctx> structure. This structure holds
696 * everything necessary to use the header and find next occurrence. If its
697 * <idx> member is 0, the first header is retrieved. Otherwise, the next
698 * occurrence is returned. The function returns 1 when it finds a value, and
699 * 0 when there is no more. It is equivalent to http_find_full_header2() with
700 * no header name.
701 */
702int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
703{
704 char *eol, *sov;
705 int cur_idx, old_idx;
706 int len;
707
708 cur_idx = ctx->idx;
709 if (cur_idx) {
710 /* We have previously returned a header, let's search another one */
711 sol = ctx->line;
712 eol = sol + idx->v[cur_idx].len;
713 goto next_hdr;
714 }
715
716 /* first request for this header */
717 sol += hdr_idx_first_pos(idx);
718 old_idx = 0;
719 cur_idx = hdr_idx_first_idx(idx);
720 while (cur_idx) {
721 eol = sol + idx->v[cur_idx].len;
722
723 len = 0;
724 while (1) {
725 if (len >= eol - sol)
726 goto next_hdr;
727 if (sol[len] == ':')
728 break;
729 len++;
730 }
731
732 ctx->del = len;
733 sov = sol + len + 1;
734 while (sov < eol && http_is_lws[(unsigned char)*sov])
735 sov++;
736
737 ctx->line = sol;
738 ctx->prev = old_idx;
739 ctx->idx = cur_idx;
740 ctx->val = sov - sol;
741 ctx->tws = 0;
742
743 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
744 eol--;
745 ctx->tws++;
746 }
747 ctx->vlen = eol - sov;
748 return 1;
749
750 next_hdr:
751 sol = eol + idx->v[cur_idx].cr + 1;
752 old_idx = cur_idx;
753 cur_idx = idx->v[cur_idx].next;
754 }
755 return 0;
756}
757
Willy Tarreau68085d82010-01-18 14:54:04 +0100758/* Find the end of the header value contained between <s> and <e>. See RFC2616,
759 * par 2.2 for more information. Note that it requires a valid header to return
760 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200761 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100762char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200763{
764 int quoted, qdpair;
765
766 quoted = qdpair = 0;
767 for (; s < e; s++) {
768 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200769 else if (quoted) {
770 if (*s == '\\') qdpair = 1;
771 else if (*s == '"') quoted = 0;
772 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200773 else if (*s == '"') quoted = 1;
774 else if (*s == ',') return s;
775 }
776 return s;
777}
778
779/* Find the first or next occurrence of header <name> in message buffer <sol>
780 * using headers index <idx>, and return it in the <ctx> structure. This
781 * structure holds everything necessary to use the header and find next
782 * occurrence. If its <idx> member is 0, the header is searched from the
783 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100784 * 1 when it finds a value, and 0 when there is no more. It is designed to work
785 * with headers defined as comma-separated lists. As a special case, if ctx->val
786 * is NULL when searching for a new values of a header, the current header is
787 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200788 */
789int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100790 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200791 struct hdr_ctx *ctx)
792{
Willy Tarreau68085d82010-01-18 14:54:04 +0100793 char *eol, *sov;
794 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200795
Willy Tarreau68085d82010-01-18 14:54:04 +0100796 cur_idx = ctx->idx;
797 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200798 /* We have previously returned a value, let's search
799 * another one on the same line.
800 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200801 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200802 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100803 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200804 eol = sol + idx->v[cur_idx].len;
805
806 if (sov >= eol)
807 /* no more values in this header */
808 goto next_hdr;
809
Willy Tarreau68085d82010-01-18 14:54:04 +0100810 /* values remaining for this header, skip the comma but save it
811 * for later use (eg: for header deletion).
812 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200813 sov++;
814 while (sov < eol && http_is_lws[(unsigned char)*sov])
815 sov++;
816
817 goto return_hdr;
818 }
819
820 /* first request for this header */
821 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100822 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200823 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200824 while (cur_idx) {
825 eol = sol + idx->v[cur_idx].len;
826
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200827 if (len == 0) {
828 /* No argument was passed, we want any header.
829 * To achieve this, we simply build a fake request. */
830 while (sol + len < eol && sol[len] != ':')
831 len++;
832 name = sol;
833 }
834
Willy Tarreau33a7e692007-06-10 19:45:56 +0200835 if ((len < eol - sol) &&
836 (sol[len] == ':') &&
837 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100838 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200839 sov = sol + len + 1;
840 while (sov < eol && http_is_lws[(unsigned char)*sov])
841 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100842
Willy Tarreau33a7e692007-06-10 19:45:56 +0200843 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100844 ctx->prev = old_idx;
845 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200846 ctx->idx = cur_idx;
847 ctx->val = sov - sol;
848
849 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200850 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200851 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200852 eol--;
853 ctx->tws++;
854 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200855 ctx->vlen = eol - sov;
856 return 1;
857 }
858 next_hdr:
859 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100860 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200861 cur_idx = idx->v[cur_idx].next;
862 }
863 return 0;
864}
865
866int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100867 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200868 struct hdr_ctx *ctx)
869{
870 return http_find_header2(name, strlen(name), sol, idx, ctx);
871}
872
Willy Tarreau68085d82010-01-18 14:54:04 +0100873/* Remove one value of a header. This only works on a <ctx> returned by one of
874 * the http_find_header functions. The value is removed, as well as surrounding
875 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100876 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100877 * message <msg>. The new index is returned. If it is zero, it means there is
878 * no more header, so any processing may stop. The ctx is always left in a form
879 * that can be handled by http_find_header2() to find next occurrence.
880 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100881int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100882{
883 int cur_idx = ctx->idx;
884 char *sol = ctx->line;
885 struct hdr_idx_elem *hdr;
886 int delta, skip_comma;
887
888 if (!cur_idx)
889 return 0;
890
891 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200892 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100893 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200894 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100895 http_msg_move_end(msg, delta);
896 idx->used--;
897 hdr->len = 0; /* unused entry */
898 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100899 if (idx->tail == ctx->idx)
900 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100901 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100902 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100903 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200904 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100905 return ctx->idx;
906 }
907
908 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200909 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
910 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100911 */
912
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200913 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200914 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200915 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100916 NULL, 0);
917 hdr->len += delta;
918 http_msg_move_end(msg, delta);
919 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200920 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100921 return ctx->idx;
922}
923
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100924/* This function handles a server error at the stream interface level. The
925 * stream interface is assumed to be already in a closed state. An optional
926 * message is copied into the input buffer, and an HTTP status code stored.
927 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100928 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200929 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200930static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100931 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100933 channel_auto_read(si_oc(si));
934 channel_abort(si_oc(si));
935 channel_auto_close(si_oc(si));
936 channel_erase(si_oc(si));
937 channel_auto_close(si_ic(si));
938 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100939 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200940 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100941 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200942 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200943 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200944 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200945 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200946 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200947}
948
Willy Tarreau87b09662015-04-03 00:22:06 +0200949/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100950 * and message.
951 */
952
Willy Tarreau87b09662015-04-03 00:22:06 +0200953struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100954{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200955 if (s->be->errmsg[msgnum].str)
956 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200957 else if (strm_fe(s)->errmsg[msgnum].str)
958 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100959 else
960 return &http_err_chunks[msgnum];
961}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200962
Willy Tarreau53b6c742006-12-17 13:37:46 +0100963/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200964 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
965 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100966 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100967enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100968{
969 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100970 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100971
972 m = ((unsigned)*str - 'A');
973
974 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100975 for (h = http_methods[m]; h->len > 0; h++) {
976 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100977 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100978 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100979 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100980 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100981 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200982 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100983}
984
Willy Tarreau21d2af32008-02-14 20:25:24 +0100985/* Parse the URI from the given transaction (which is assumed to be in request
986 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
987 * It is returned otherwise.
988 */
989static char *
990http_get_path(struct http_txn *txn)
991{
992 char *ptr, *end;
993
Willy Tarreau9b28e032012-10-12 23:49:43 +0200994 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100995 end = ptr + txn->req.sl.rq.u_l;
996
997 if (ptr >= end)
998 return NULL;
999
1000 /* RFC2616, par. 5.1.2 :
1001 * Request-URI = "*" | absuri | abspath | authority
1002 */
1003
1004 if (*ptr == '*')
1005 return NULL;
1006
1007 if (isalpha((unsigned char)*ptr)) {
1008 /* this is a scheme as described by RFC3986, par. 3.1 */
1009 ptr++;
1010 while (ptr < end &&
1011 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1012 ptr++;
1013 /* skip '://' */
1014 if (ptr == end || *ptr++ != ':')
1015 return NULL;
1016 if (ptr == end || *ptr++ != '/')
1017 return NULL;
1018 if (ptr == end || *ptr++ != '/')
1019 return NULL;
1020 }
1021 /* skip [user[:passwd]@]host[:[port]] */
1022
1023 while (ptr < end && *ptr != '/')
1024 ptr++;
1025
1026 if (ptr == end)
1027 return NULL;
1028
1029 /* OK, we got the '/' ! */
1030 return ptr;
1031}
1032
William Lallemand65ad6e12014-01-31 15:08:02 +01001033/* Parse the URI from the given string and look for the "/" beginning the PATH.
1034 * If not found, return NULL. It is returned otherwise.
1035 */
1036static char *
1037http_get_path_from_string(char *str)
1038{
1039 char *ptr = str;
1040
1041 /* RFC2616, par. 5.1.2 :
1042 * Request-URI = "*" | absuri | abspath | authority
1043 */
1044
1045 if (*ptr == '*')
1046 return NULL;
1047
1048 if (isalpha((unsigned char)*ptr)) {
1049 /* this is a scheme as described by RFC3986, par. 3.1 */
1050 ptr++;
1051 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1052 ptr++;
1053 /* skip '://' */
1054 if (*ptr == '\0' || *ptr++ != ':')
1055 return NULL;
1056 if (*ptr == '\0' || *ptr++ != '/')
1057 return NULL;
1058 if (*ptr == '\0' || *ptr++ != '/')
1059 return NULL;
1060 }
1061 /* skip [user[:passwd]@]host[:[port]] */
1062
1063 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1064 ptr++;
1065
1066 if (*ptr == '\0' || *ptr == ' ')
1067 return NULL;
1068
1069 /* OK, we got the '/' ! */
1070 return ptr;
1071}
1072
Willy Tarreau71241ab2012-12-27 11:30:54 +01001073/* Returns a 302 for a redirectable request that reaches a server working in
1074 * in redirect mode. This may only be called just after the stream interface
1075 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1076 * follow normal proxy processing. NOTE: this function is designed to support
1077 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001078 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001079void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001080{
1081 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001082 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001083 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001084 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001085
1086 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001087 trash.len = strlen(HTTP_302);
1088 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001089
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001090 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001091
Willy Tarreauefb453c2008-10-26 20:49:47 +01001092 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001093 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001094 return;
1095
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001096 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001097 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001098 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1099 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001100 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001101
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001102 /* 3: add the request URI. Since it was already forwarded, we need
1103 * to temporarily rewind the buffer.
1104 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001105 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001106 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001107
Willy Tarreauefb453c2008-10-26 20:49:47 +01001108 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001109 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 +02001110
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001111 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001112
Willy Tarreauefb453c2008-10-26 20:49:47 +01001113 if (!path)
1114 return;
1115
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001116 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001117 return;
1118
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001119 memcpy(trash.str + trash.len, path, len);
1120 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001121
1122 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001123 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1124 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001125 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001126 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1127 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001128 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001129
1130 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001131 si_shutr(si);
1132 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001133 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001134 si->state = SI_ST_CLO;
1135
1136 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001137 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001138
1139 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001140 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001141 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001142}
1143
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001144/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001145 * that the server side is closed. Note that err_type is actually a
1146 * bitmask, where almost only aborts may be cumulated with other
1147 * values. We consider that aborted operations are more important
1148 * than timeouts or errors due to the fact that nobody else in the
1149 * logs might explain incomplete retries. All others should avoid
1150 * being cumulated. It should normally not be possible to have multiple
1151 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001152 * Note that connection errors appearing on the second request of a keep-alive
1153 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001154 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001155void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001156{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001157 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001158
1159 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001160 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001161 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001162 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001163 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001164 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001165 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001166 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001167 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001168 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001169 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001170 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001171 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001172 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001173 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001174 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001175 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001176 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001177 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1178 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001179 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001180 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001181 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001182 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001183 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001184 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001185 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001186 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001187}
1188
Willy Tarreau42250582007-04-01 01:30:43 +02001189extern const char sess_term_cond[8];
1190extern const char sess_fin_state[8];
1191extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001192struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001193struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001194struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001195struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001196
Willy Tarreau117f59e2007-03-04 18:17:17 +01001197/*
1198 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001199 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001200 */
1201void capture_headers(char *som, struct hdr_idx *idx,
1202 char **cap, struct cap_hdr *cap_hdr)
1203{
1204 char *eol, *sol, *col, *sov;
1205 int cur_idx;
1206 struct cap_hdr *h;
1207 int len;
1208
1209 sol = som + hdr_idx_first_pos(idx);
1210 cur_idx = hdr_idx_first_idx(idx);
1211
1212 while (cur_idx) {
1213 eol = sol + idx->v[cur_idx].len;
1214
1215 col = sol;
1216 while (col < eol && *col != ':')
1217 col++;
1218
1219 sov = col + 1;
1220 while (sov < eol && http_is_lws[(unsigned char)*sov])
1221 sov++;
1222
1223 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001224 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001225 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1226 if (cap[h->index] == NULL)
1227 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001228 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001229
1230 if (cap[h->index] == NULL) {
1231 Alert("HTTP capture : out of memory.\n");
1232 continue;
1233 }
1234
1235 len = eol - sov;
1236 if (len > h->len)
1237 len = h->len;
1238
1239 memcpy(cap[h->index], sov, len);
1240 cap[h->index][len]=0;
1241 }
1242 }
1243 sol = eol + idx->v[cur_idx].cr + 1;
1244 cur_idx = idx->v[cur_idx].next;
1245 }
1246}
1247
1248
Willy Tarreau42250582007-04-01 01:30:43 +02001249/* either we find an LF at <ptr> or we jump to <bad>.
1250 */
1251#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1252
1253/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1254 * otherwise to <http_msg_ood> with <state> set to <st>.
1255 */
1256#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1257 ptr++; \
1258 if (likely(ptr < end)) \
1259 goto good; \
1260 else { \
1261 state = (st); \
1262 goto http_msg_ood; \
1263 } \
1264 } while (0)
1265
1266
Willy Tarreaubaaee002006-06-26 02:48:02 +02001267/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001268 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001269 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1270 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1271 * will give undefined results.
1272 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1273 * and that msg->sol points to the beginning of the response.
1274 * If a complete line is found (which implies that at least one CR or LF is
1275 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1276 * returned indicating an incomplete line (which does not mean that parts have
1277 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1278 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1279 * upon next call.
1280 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001281 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001282 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1283 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001284 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001285 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001286const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001287 enum ht_state state, const char *ptr, const char *end,
1288 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001289{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001290 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001291
Willy Tarreau8973c702007-01-21 23:58:29 +01001292 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001293 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001294 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001295 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001296 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1297
1298 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001299 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001300 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1301 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001302 state = HTTP_MSG_ERROR;
1303 break;
1304
Willy Tarreau8973c702007-01-21 23:58:29 +01001305 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001306 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001307 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001308 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 goto http_msg_rpcode;
1310 }
1311 if (likely(HTTP_IS_SPHT(*ptr)))
1312 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1313 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001314 state = HTTP_MSG_ERROR;
1315 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001316
Willy Tarreau8973c702007-01-21 23:58:29 +01001317 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001318 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001319 if (likely(!HTTP_IS_LWS(*ptr)))
1320 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1321
1322 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001323 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001324 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1325 }
1326
1327 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001328 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001329 http_msg_rsp_reason:
1330 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001331 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001332 msg->sl.st.r_l = 0;
1333 goto http_msg_rpline_eol;
1334
Willy Tarreau8973c702007-01-21 23:58:29 +01001335 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001336 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001337 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001338 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001339 goto http_msg_rpreason;
1340 }
1341 if (likely(HTTP_IS_SPHT(*ptr)))
1342 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1343 /* so it's a CR/LF, so there is no reason phrase */
1344 goto http_msg_rsp_reason;
1345
Willy Tarreau8973c702007-01-21 23:58:29 +01001346 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001347 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001348 if (likely(!HTTP_IS_CRLF(*ptr)))
1349 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001350 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001351 http_msg_rpline_eol:
1352 /* We have seen the end of line. Note that we do not
1353 * necessarily have the \n yet, but at least we know that we
1354 * have EITHER \r OR \n, otherwise the response would not be
1355 * complete. We can then record the response length and return
1356 * to the caller which will be able to register it.
1357 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001358 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001359 return ptr;
1360
Willy Tarreau8973c702007-01-21 23:58:29 +01001361 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001362#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001363 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1364 exit(1);
1365#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001366 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001367 }
1368
1369 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001370 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001371 if (ret_state)
1372 *ret_state = state;
1373 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001374 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001375 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001376}
1377
Willy Tarreau8973c702007-01-21 23:58:29 +01001378/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001379 * This function parses a request line between <ptr> and <end>, starting with
1380 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1381 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1382 * will give undefined results.
1383 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1384 * and that msg->sol points to the beginning of the request.
1385 * If a complete line is found (which implies that at least one CR or LF is
1386 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1387 * returned indicating an incomplete line (which does not mean that parts have
1388 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1389 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1390 * upon next call.
1391 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001392 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1394 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001395 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001396 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001397const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001398 enum ht_state state, const char *ptr, const char *end,
1399 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001400{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001401 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001405 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 if (likely(HTTP_IS_TOKEN(*ptr)))
1407 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001408
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001410 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1412 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001413
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 if (likely(HTTP_IS_CRLF(*ptr))) {
1415 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001416 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001418 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001420 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001422 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 msg->sl.rq.v_l = 0;
1424 goto http_msg_rqline_eol;
1425 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001426 state = HTTP_MSG_ERROR;
1427 break;
1428
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001430 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001432 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 goto http_msg_rquri;
1434 }
1435 if (likely(HTTP_IS_SPHT(*ptr)))
1436 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1437 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1438 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001441 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001442 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001444
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001445 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001446 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1448 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001449
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001450 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001451 /* non-ASCII chars are forbidden unless option
1452 * accept-invalid-http-request is enabled in the frontend.
1453 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001454 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001455 if (msg->err_pos < -1)
1456 goto invalid_char;
1457 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001458 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001459 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1460 }
1461
1462 if (likely(HTTP_IS_CRLF(*ptr))) {
1463 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1464 goto http_msg_req09_uri_e;
1465 }
1466
1467 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001468 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001469 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001470 state = HTTP_MSG_ERROR;
1471 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001472
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001474 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001476 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 goto http_msg_rqver;
1478 }
1479 if (likely(HTTP_IS_SPHT(*ptr)))
1480 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1481 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1482 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001483
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001485 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001486 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001488
1489 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001490 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001491 http_msg_rqline_eol:
1492 /* We have seen the end of line. Note that we do not
1493 * necessarily have the \n yet, but at least we know that we
1494 * have EITHER \r OR \n, otherwise the request would not be
1495 * complete. We can then record the request length and return
1496 * to the caller which will be able to register it.
1497 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001498 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001499 return ptr;
1500 }
1501
1502 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001503 state = HTTP_MSG_ERROR;
1504 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001505
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001507#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1509 exit(1);
1510#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001511 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001514 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001515 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 if (ret_state)
1517 *ret_state = state;
1518 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001519 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001522
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001523/*
1524 * Returns the data from Authorization header. Function may be called more
1525 * than once so data is stored in txn->auth_data. When no header is found
1526 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001527 * searching again for something we are unable to find anyway. However, if
1528 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001529 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001530 */
1531
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001532/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1533 * set according to global.tune.bufsize.
1534 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001535char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001536
1537int
Willy Tarreau87b09662015-04-03 00:22:06 +02001538get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001539{
1540
Willy Tarreaueee5b512015-04-03 23:46:31 +02001541 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001542 struct chunk auth_method;
1543 struct hdr_ctx ctx;
1544 char *h, *p;
1545 int len;
1546
1547#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001548 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001549#endif
1550
1551 if (txn->auth.method == HTTP_AUTH_WRONG)
1552 return 0;
1553
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001554 txn->auth.method = HTTP_AUTH_WRONG;
1555
1556 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001557
1558 if (txn->flags & TX_USE_PX_CONN) {
1559 h = "Proxy-Authorization";
1560 len = strlen(h);
1561 } else {
1562 h = "Authorization";
1563 len = strlen(h);
1564 }
1565
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001566 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001567 return 0;
1568
1569 h = ctx.line + ctx.val;
1570
1571 p = memchr(h, ' ', ctx.vlen);
1572 if (!p || p == h)
1573 return 0;
1574
1575 chunk_initlen(&auth_method, h, 0, p-h);
1576 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1577
1578 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1579
1580 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001581 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001582
1583 if (len < 0)
1584 return 0;
1585
1586
1587 get_http_auth_buff[len] = '\0';
1588
1589 p = strchr(get_http_auth_buff, ':');
1590
1591 if (!p)
1592 return 0;
1593
1594 txn->auth.user = get_http_auth_buff;
1595 *p = '\0';
1596 txn->auth.pass = p+1;
1597
1598 txn->auth.method = HTTP_AUTH_BASIC;
1599 return 1;
1600 }
1601
1602 return 0;
1603}
1604
Willy Tarreau58f10d72006-12-04 02:26:12 +01001605
Willy Tarreau8973c702007-01-21 23:58:29 +01001606/*
1607 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001608 * depending on the initial msg->msg_state. The caller is responsible for
1609 * ensuring that the message does not wrap. The function can be preempted
1610 * everywhere when data are missing and recalled at the exact same location
1611 * with no information loss. The message may even be realigned between two
1612 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001613 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001614 * fields. Note that msg->sol will be initialized after completing the first
1615 * state, so that none of the msg pointers has to be initialized prior to the
1616 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001617 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001618void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001619{
Willy Tarreau3770f232013-12-07 00:01:53 +01001620 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001622 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001623
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001624 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001625 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001626 ptr = buf->p + msg->next;
1627 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001628
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 if (unlikely(ptr >= end))
1630 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001631
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001633 /*
1634 * First, states that are specific to the response only.
1635 * We check them first so that request and headers are
1636 * closer to each other (accessed more often).
1637 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001638 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001639 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001640 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001641 /* we have a start of message, but we have to check
1642 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001643 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001644 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001645 if (unlikely(ptr != buf->p)) {
1646 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001647 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001648 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001649 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001650 }
Willy Tarreau26927362012-05-18 23:22:52 +02001651 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001652 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001653 hdr_idx_init(idx);
1654 state = HTTP_MSG_RPVER;
1655 goto http_msg_rpver;
1656 }
1657
1658 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1659 goto http_msg_invalid;
1660
1661 if (unlikely(*ptr == '\n'))
1662 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1663 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1664 /* stop here */
1665
Willy Tarreau8973c702007-01-21 23:58:29 +01001666 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001667 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001668 EXPECT_LF_HERE(ptr, http_msg_invalid);
1669 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1670 /* stop here */
1671
Willy Tarreau8973c702007-01-21 23:58:29 +01001672 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001673 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001674 case HTTP_MSG_RPVER_SP:
1675 case HTTP_MSG_RPCODE:
1676 case HTTP_MSG_RPCODE_SP:
1677 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001678 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001679 state, ptr, end,
1680 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001681 if (unlikely(!ptr))
1682 return;
1683
1684 /* we have a full response and we know that we have either a CR
1685 * or an LF at <ptr>.
1686 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001687 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1688
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001689 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001690 if (likely(*ptr == '\r'))
1691 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1692 goto http_msg_rpline_end;
1693
Willy Tarreau8973c702007-01-21 23:58:29 +01001694 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001695 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001696 /* msg->sol must point to the first of CR or LF. */
1697 EXPECT_LF_HERE(ptr, http_msg_invalid);
1698 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1699 /* stop here */
1700
1701 /*
1702 * Second, states that are specific to the request only
1703 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001705 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001707 /* we have a start of message, but we have to check
1708 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001709 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001710 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001711 if (likely(ptr != buf->p)) {
1712 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001713 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001714 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001715 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 }
Willy Tarreau26927362012-05-18 23:22:52 +02001717 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001718 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001719 state = HTTP_MSG_RQMETH;
1720 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001722
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001723 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1724 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001725
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001726 if (unlikely(*ptr == '\n'))
1727 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1728 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001729 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001730
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001731 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001732 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 EXPECT_LF_HERE(ptr, http_msg_invalid);
1734 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001735 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001736
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001737 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001738 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 case HTTP_MSG_RQMETH_SP:
1740 case HTTP_MSG_RQURI:
1741 case HTTP_MSG_RQURI_SP:
1742 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001743 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001744 state, ptr, end,
1745 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746 if (unlikely(!ptr))
1747 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001748
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001749 /* we have a full request and we know that we have either a CR
1750 * or an LF at <ptr>.
1751 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001752 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001753
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001754 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001755 if (likely(*ptr == '\r'))
1756 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001758
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001760 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001761 /* check for HTTP/0.9 request : no version information available.
1762 * msg->sol must point to the first of CR or LF.
1763 */
1764 if (unlikely(msg->sl.rq.v_l == 0))
1765 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001766
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001767 EXPECT_LF_HERE(ptr, http_msg_invalid);
1768 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001769 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001770
Willy Tarreau8973c702007-01-21 23:58:29 +01001771 /*
1772 * Common states below
1773 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001774 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001775 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001776 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001777 if (likely(!HTTP_IS_CRLF(*ptr))) {
1778 goto http_msg_hdr_name;
1779 }
1780
1781 if (likely(*ptr == '\r'))
1782 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1783 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001784
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001785 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001786 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001787 /* assumes msg->sol points to the first char */
1788 if (likely(HTTP_IS_TOKEN(*ptr)))
1789 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001790
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001791 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001792 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001793
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001794 if (likely(msg->err_pos < -1) || *ptr == '\n')
1795 goto http_msg_invalid;
1796
1797 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001798 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001799
1800 /* and we still accept this non-token character */
1801 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001802
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001803 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001804 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001805 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 if (likely(HTTP_IS_SPHT(*ptr)))
1807 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001808
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001809 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001810 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001811
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001812 if (likely(!HTTP_IS_CRLF(*ptr))) {
1813 goto http_msg_hdr_val;
1814 }
1815
1816 if (likely(*ptr == '\r'))
1817 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1818 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001819
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001820 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001821 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001822 EXPECT_LF_HERE(ptr, http_msg_invalid);
1823 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001824
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001825 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001826 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001827 if (likely(HTTP_IS_SPHT(*ptr))) {
1828 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001829 for (; buf->p + msg->sov < ptr; msg->sov++)
1830 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001831 goto http_msg_hdr_l1_sp;
1832 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001833 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001834 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001835 goto http_msg_complete_header;
1836
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001837 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001838 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001839 /* assumes msg->sol points to the first char, and msg->sov
1840 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001841 */
1842 if (likely(!HTTP_IS_CRLF(*ptr)))
1843 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001844
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001845 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001846 /* Note: we could also copy eol into ->eoh so that we have the
1847 * real header end in case it ends with lots of LWS, but is this
1848 * really needed ?
1849 */
1850 if (likely(*ptr == '\r'))
1851 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1852 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001853
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001854 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001855 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001856 EXPECT_LF_HERE(ptr, http_msg_invalid);
1857 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001858
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001859 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001860 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001861 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1862 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001863 for (; buf->p + msg->eol < ptr; msg->eol++)
1864 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001865 goto http_msg_hdr_val;
1866 }
1867 http_msg_complete_header:
1868 /*
1869 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001870 * Assumes msg->sol points to the first char, msg->sov points
1871 * to the first character of the value and msg->eol to the
1872 * first CR or LF so we know how the line ends. We insert last
1873 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001874 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001875 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001876 idx, idx->tail) < 0))
1877 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001878
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001879 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001880 if (likely(!HTTP_IS_CRLF(*ptr))) {
1881 goto http_msg_hdr_name;
1882 }
1883
1884 if (likely(*ptr == '\r'))
1885 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1886 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001887
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001888 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001889 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001890 /* Assumes msg->sol points to the first of either CR or LF.
1891 * Sets ->sov and ->next to the total header length, ->eoh to
1892 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1893 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001894 EXPECT_LF_HERE(ptr, http_msg_invalid);
1895 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001896 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001897 msg->eoh = msg->sol;
1898 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001899 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001900 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001901 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001902
1903 case HTTP_MSG_ERROR:
1904 /* this may only happen if we call http_msg_analyser() twice with an error */
1905 break;
1906
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001907 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001908#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001909 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1910 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001911#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001912 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001913 }
1914 http_msg_ood:
1915 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001916 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001917 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001918 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001919
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001920 http_msg_invalid:
1921 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001922 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001923 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001924 return;
1925}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001926
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001927/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1928 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1929 * nothing is done and 1 is returned.
1930 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001931static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001932{
1933 int delta;
1934 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001935 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001936
1937 if (msg->sl.rq.v_l != 0)
1938 return 1;
1939
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001940 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1941 if (txn->meth != HTTP_METH_GET)
1942 return 0;
1943
Willy Tarreau9b28e032012-10-12 23:49:43 +02001944 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001945 delta = 0;
1946
1947 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001948 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1949 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001950 }
1951 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001952 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001953 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001954 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001955 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001956 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001957 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001958 NULL, NULL);
1959 if (unlikely(!cur_end))
1960 return 0;
1961
1962 /* we have a full HTTP/1.0 request now and we know that
1963 * we have either a CR or an LF at <ptr>.
1964 */
1965 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1966 return 1;
1967}
1968
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001969/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001970 * and "keep-alive" values. If we already know that some headers may safely
1971 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001972 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1973 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001974 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001975 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1976 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1977 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001978 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001979 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001980void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001981{
Willy Tarreau5b154472009-12-21 20:11:07 +01001982 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001983 const char *hdr_val = "Connection";
1984 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001985
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001986 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001987 return;
1988
Willy Tarreau88d349d2010-01-25 12:15:43 +01001989 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1990 hdr_val = "Proxy-Connection";
1991 hdr_len = 16;
1992 }
1993
Willy Tarreau5b154472009-12-21 20:11:07 +01001994 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001995 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001996 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001997 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1998 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001999 if (to_del & 2)
2000 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002001 else
2002 txn->flags |= TX_CON_KAL_SET;
2003 }
2004 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2005 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002006 if (to_del & 1)
2007 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002008 else
2009 txn->flags |= TX_CON_CLO_SET;
2010 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002011 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2012 txn->flags |= TX_HDR_CONN_UPG;
2013 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002014 }
2015
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002016 txn->flags |= TX_HDR_CONN_PRS;
2017 return;
2018}
Willy Tarreau5b154472009-12-21 20:11:07 +01002019
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002020/* Apply desired changes on the Connection: header. Values may be removed and/or
2021 * added depending on the <wanted> flags, which are exclusively composed of
2022 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2023 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2024 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002025void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002026{
2027 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002028 const char *hdr_val = "Connection";
2029 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002030
2031 ctx.idx = 0;
2032
Willy Tarreau88d349d2010-01-25 12:15:43 +01002033
2034 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2035 hdr_val = "Proxy-Connection";
2036 hdr_len = 16;
2037 }
2038
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002039 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002040 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002041 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2042 if (wanted & TX_CON_KAL_SET)
2043 txn->flags |= TX_CON_KAL_SET;
2044 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002045 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002046 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002047 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2048 if (wanted & TX_CON_CLO_SET)
2049 txn->flags |= TX_CON_CLO_SET;
2050 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002051 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002052 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002053 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002054
2055 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2056 return;
2057
2058 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2059 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002060 hdr_val = "Connection: close";
2061 hdr_len = 17;
2062 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2063 hdr_val = "Proxy-Connection: close";
2064 hdr_len = 23;
2065 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002066 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002067 }
2068
2069 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2070 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002071 hdr_val = "Connection: keep-alive";
2072 hdr_len = 22;
2073 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2074 hdr_val = "Proxy-Connection: keep-alive";
2075 hdr_len = 28;
2076 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002077 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002078 }
2079 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002080}
2081
Willy Tarreauc24715e2014-04-17 15:21:20 +02002082/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
2083 * the first byte of data after the chunk size, so that we know we can forward
2084 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
2085 * the chunk size. That way it is always possible to differentiate between the
2086 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01002087 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002088 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002089 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002090static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002091{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002092 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002093 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002094 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002095 const char *end = buf->data + buf->size;
2096 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002097 unsigned int chunk = 0;
2098
2099 /* The chunk size is in the following form, though we are only
2100 * interested in the size and CRLF :
2101 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2102 */
2103 while (1) {
2104 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002105 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002106 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002107 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002108 if (c < 0) /* not a hex digit anymore */
2109 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002110 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002111 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002112 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002113 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002114 chunk = (chunk << 4) + c;
2115 }
2116
Willy Tarreaud98cf932009-12-27 22:54:55 +01002117 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002118 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002119 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002120
2121 while (http_is_spht[(unsigned char)*ptr]) {
2122 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002123 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002124 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002125 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002126 }
2127
Willy Tarreaud98cf932009-12-27 22:54:55 +01002128 /* Up to there, we know that at least one byte is present at *ptr. Check
2129 * for the end of chunk size.
2130 */
2131 while (1) {
2132 if (likely(HTTP_IS_CRLF(*ptr))) {
2133 /* we now have a CR or an LF at ptr */
2134 if (likely(*ptr == '\r')) {
2135 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002136 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002137 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002138 return 0;
2139 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002140
Willy Tarreaud98cf932009-12-27 22:54:55 +01002141 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002142 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002143 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002144 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002145 /* done */
2146 break;
2147 }
2148 else if (*ptr == ';') {
2149 /* chunk extension, ends at next CRLF */
2150 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002151 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002152 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002153 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002154
2155 while (!HTTP_IS_CRLF(*ptr)) {
2156 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002157 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002158 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002159 return 0;
2160 }
2161 /* we have a CRLF now, loop above */
2162 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002163 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002164 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002165 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002166 }
2167
Willy Tarreaud98cf932009-12-27 22:54:55 +01002168 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002169 * which may or may not be present. We save that into ->next,
2170 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002171 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002172 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002173 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002174 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002175 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002176 msg->chunk_len = chunk;
2177 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002178 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002179 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002180 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002181 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002182 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002183}
2184
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002185/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002186 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002187 * the trailers is found, it is automatically scheduled to be forwarded,
2188 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2189 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002190 * except maybe msg->next if it could parse some lines, and returns zero.
2191 * If a parse error is encountered, the function returns < 0 and does not
2192 * change anything except maybe msg->next. Note that the message must
2193 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002194 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002195 * forwarding, and that msg->next exactly matches the length of trailers
2196 * already parsed and not forwarded. It is also important to note that this
2197 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002198 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002199static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002200{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002201 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002202
Willy Tarreaua458b672012-03-05 11:17:50 +01002203 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002204 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002205 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002206 const char *ptr = b_ptr(buf, msg->next);
2207 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002208 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002209
2210 /* scan current line and stop at LF or CRLF */
2211 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002212 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002213 return 0;
2214
2215 if (*ptr == '\n') {
2216 if (!p1)
2217 p1 = ptr;
2218 p2 = ptr;
2219 break;
2220 }
2221
2222 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002223 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002224 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002225 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002226 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002227 p1 = ptr;
2228 }
2229
2230 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002231 if (ptr >= buf->data + buf->size)
2232 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002233 }
2234
2235 /* after LF; point to beginning of next line */
2236 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002237 if (p2 >= buf->data + buf->size)
2238 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002239
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002240 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002241 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002242 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002243
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002244 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002245 /* LF/CRLF at beginning of line => end of trailers at p2.
2246 * Everything was scheduled for forwarding, there's nothing
2247 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002248 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002249 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002250 msg->msg_state = HTTP_MSG_DONE;
2251 return 1;
2252 }
2253 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002254 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002255 }
2256}
2257
Willy Tarreauc24715e2014-04-17 15:21:20 +02002258/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2259 * or a possible LF alone at the end of a chunk. It automatically adjusts
2260 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002261 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002262 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2263 * not enough data are available, the function does not change anything and
2264 * returns zero. If a parse error is encountered, the function returns < 0 and
2265 * does not change anything. Note: this function is designed to parse wrapped
2266 * CRLF at the end of the buffer.
2267 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002268static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002269{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002270 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002271 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002272 int bytes;
2273
2274 /* NB: we'll check data availabilty at the end. It's not a
2275 * problem because whatever we match first will be checked
2276 * against the correct length.
2277 */
2278 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002279 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002280 if (*ptr == '\r') {
2281 bytes++;
2282 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002283 if (ptr >= buf->data + buf->size)
2284 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002285 }
2286
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002287 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002288 return 0;
2289
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002290 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002291 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002292 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002293 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002294
2295 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002296 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002297 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002298 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002299 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002300 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2301 return 1;
2302}
Willy Tarreau5b154472009-12-21 20:11:07 +01002303
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002304/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2305 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2306 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2307 * Unparsable qvalues return 1000 as "q=1.000".
2308 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002309int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002310{
2311 int q = 1000;
2312
Willy Tarreau506c69a2014-07-08 00:59:48 +02002313 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002314 goto out;
2315 q = (*qvalue++ - '0') * 1000;
2316
2317 if (*qvalue++ != '.')
2318 goto out;
2319
Willy Tarreau506c69a2014-07-08 00:59:48 +02002320 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002321 goto out;
2322 q += (*qvalue++ - '0') * 100;
2323
Willy Tarreau506c69a2014-07-08 00:59:48 +02002324 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002325 goto out;
2326 q += (*qvalue++ - '0') * 10;
2327
Willy Tarreau506c69a2014-07-08 00:59:48 +02002328 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002329 goto out;
2330 q += (*qvalue++ - '0') * 1;
2331 out:
2332 if (q > 1000)
2333 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002334 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002335 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002336 return q;
2337}
William Lallemand82fe75c2012-10-23 10:25:10 +02002338
2339/*
2340 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002341 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002342int select_compression_request_header(struct stream *s, struct buffer *req)
William Lallemand82fe75c2012-10-23 10:25:10 +02002343{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002344 struct http_txn *txn = s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002345 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002346 struct hdr_ctx ctx;
2347 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002348 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002349
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002350 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2351 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002352 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2353 */
2354 ctx.idx = 0;
2355 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2356 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002357 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2358 (ctx.vlen < 31 ||
2359 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2360 ctx.line[ctx.val + 30] < '6' ||
2361 (ctx.line[ctx.val + 30] == '6' &&
2362 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2363 s->comp_algo = NULL;
2364 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002365 }
2366
William Lallemand82fe75c2012-10-23 10:25:10 +02002367 /* search for the algo in the backend in priority or the frontend */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002368 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 +01002369 int best_q = 0;
2370
William Lallemand82fe75c2012-10-23 10:25:10 +02002371 ctx.idx = 0;
2372 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002373 const char *qval;
2374 int q;
2375 int toklen;
2376
2377 /* try to isolate the token from the optional q-value */
2378 toklen = 0;
2379 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2380 toklen++;
2381
2382 qval = ctx.line + ctx.val + toklen;
2383 while (1) {
2384 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2385 qval++;
2386
2387 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2388 qval = NULL;
2389 break;
2390 }
2391 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002392
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002393 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2394 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002395
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002396 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2397 qval = NULL;
2398 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002399 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002400 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2401 break;
2402
2403 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2404 qval++;
2405 }
2406
2407 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002408 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002409
2410 if (q <= best_q)
2411 continue;
2412
2413 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2414 if (*(ctx.line + ctx.val) == '*' ||
Willy Tarreau615105e2015-03-28 16:40:46 +01002415 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002416 s->comp_algo = comp_algo;
2417 best_q = q;
2418 break;
2419 }
2420 }
2421 }
2422 }
2423
2424 /* remove all occurrences of the header when "compression offload" is set */
2425 if (s->comp_algo) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002426 if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002427 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2428 ctx.idx = 0;
2429 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2430 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002431 }
2432 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002433 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002434 }
2435
2436 /* identity is implicit does not require headers */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002437 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 +02002438 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
Willy Tarreau615105e2015-03-28 16:40:46 +01002439 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002440 s->comp_algo = comp_algo;
2441 return 1;
2442 }
2443 }
2444 }
2445
2446 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002447 return 0;
2448}
2449
2450/*
2451 * Selects a comression algorithm depending of the server response.
2452 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002453int select_compression_response_header(struct stream *s, struct buffer *res)
William Lallemand82fe75c2012-10-23 10:25:10 +02002454{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002455 struct http_txn *txn = s->txn;
William Lallemand82fe75c2012-10-23 10:25:10 +02002456 struct http_msg *msg = &txn->rsp;
2457 struct hdr_ctx ctx;
2458 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002459
2460 /* no common compression algorithm was found in request header */
2461 if (s->comp_algo == NULL)
2462 goto fail;
2463
2464 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002465 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002466 goto fail;
2467
Jesse Hathaway2468d4e2015-03-06 20:16:15 +00002468 /* compress 200,201,202,203 responses only */
2469 if ((txn->status != 200) &&
2470 (txn->status != 201) &&
2471 (txn->status != 202) &&
2472 (txn->status != 203))
William Lallemandd3002612012-11-26 14:34:47 +01002473 goto fail;
2474
William Lallemand82fe75c2012-10-23 10:25:10 +02002475 /* Content-Length is null */
2476 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2477 goto fail;
2478
2479 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002480 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002481 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2482 goto fail;
2483
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002484 /* no compression when Cache-Control: no-transform is present in the message */
2485 ctx.idx = 0;
2486 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2487 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2488 goto fail;
2489 }
2490
William Lallemand82fe75c2012-10-23 10:25:10 +02002491 comp_type = NULL;
2492
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002493 /* we don't want to compress multipart content-types, nor content-types that are
2494 * not listed in the "compression type" directive if any. If no content-type was
2495 * found but configuration requires one, we don't compress either. Backend has
2496 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002497 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002498 ctx.idx = 0;
2499 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2500 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2501 goto fail;
2502
2503 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002504 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002505 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002506 if (ctx.vlen >= comp_type->name_len &&
2507 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002508 /* this Content-Type should be compressed */
2509 break;
2510 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002511 /* this Content-Type should not be compressed */
2512 if (comp_type == NULL)
2513 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002514 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002515 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002516 else { /* no content-type header */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002517 if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->comp->types))
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002518 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002519 }
2520
William Lallemandd85f9172012-11-09 17:05:39 +01002521 /* limit compression rate */
2522 if (global.comp_rate_lim > 0)
2523 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2524 goto fail;
2525
William Lallemand072a2bf2012-11-20 17:01:01 +01002526 /* limit cpu usage */
2527 if (idle_pct < compress_min_idle)
2528 goto fail;
2529
William Lallemand4c49fae2012-11-07 15:00:23 +01002530 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002531 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002532 goto fail;
2533
Willy Tarreaue7dff022015-04-03 01:14:29 +02002534 s->flags |= SF_COMP_READY;
William Lallemandec3e3892012-11-12 17:02:18 +01002535
William Lallemand82fe75c2012-10-23 10:25:10 +02002536 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002537 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002538 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2539 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2540
2541 /* add Transfer-Encoding header */
2542 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2543 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2544
2545 /*
2546 * Add Content-Encoding header when it's not identity encoding.
2547 * RFC 2616 : Identity encoding: This content-coding is used only in the
2548 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2549 * header.
2550 */
Willy Tarreau615105e2015-03-28 16:40:46 +01002551 if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002552 trash.len = 18;
2553 memcpy(trash.str, "Content-Encoding: ", trash.len);
Willy Tarreau615105e2015-03-28 16:40:46 +01002554 memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len);
2555 trash.len += s->comp_algo->ua_name_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002556 trash.str[trash.len] = '\0';
2557 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002558 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002559 return 1;
2560
2561fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002562 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002563 return 0;
2564}
2565
Willy Tarreau87b09662015-04-03 00:22:06 +02002566void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002567{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002568 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002569 int tmp = TX_CON_WANT_KAL;
2570
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002571 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2572 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002573 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2574 tmp = TX_CON_WANT_TUN;
2575
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002576 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002577 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2578 tmp = TX_CON_WANT_TUN;
2579 }
2580
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002581 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002582 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2583 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002584 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002585 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2586 tmp = TX_CON_WANT_CLO;
2587 else
2588 tmp = TX_CON_WANT_SCL;
2589 }
2590
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002591 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002592 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2593 tmp = TX_CON_WANT_CLO;
2594
2595 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2596 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2597
2598 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2599 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2600 /* parse the Connection header and possibly clean it */
2601 int to_del = 0;
2602 if ((msg->flags & HTTP_MSGF_VER_11) ||
2603 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002604 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002605 to_del |= 2; /* remove "keep-alive" */
2606 if (!(msg->flags & HTTP_MSGF_VER_11))
2607 to_del |= 1; /* remove "close" */
2608 http_parse_connection_header(txn, msg, to_del);
2609 }
2610
2611 /* check if client or config asks for explicit close in KAL/SCL */
2612 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2613 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2614 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2615 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2616 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002617 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002618 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2619}
William Lallemand82fe75c2012-10-23 10:25:10 +02002620
Willy Tarreaud787e662009-07-07 10:14:51 +02002621/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2622 * processing can continue on next analysers, or zero if it either needs more
2623 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002624 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002625 * when it has nothing left to do, and may remove any analyser when it wants to
2626 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002627 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002628int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002629{
Willy Tarreau59234e92008-11-30 23:51:27 +01002630 /*
2631 * We will parse the partial (or complete) lines.
2632 * We will check the request syntax, and also join multi-line
2633 * headers. An index of all the lines will be elaborated while
2634 * parsing.
2635 *
2636 * For the parsing, we use a 28 states FSM.
2637 *
2638 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002639 * req->buf->p = beginning of request
2640 * req->buf->p + msg->eoh = end of processed headers / start of current one
2641 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002642 * msg->eol = end of current header or line (LF or CRLF)
2643 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002644 *
2645 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002646 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002647 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2648 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002649 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002650
Willy Tarreau59234e92008-11-30 23:51:27 +01002651 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002652 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002653 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002654 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002655 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002656
Willy Tarreau87b09662015-04-03 00:22:06 +02002657 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 +01002658 now_ms, __FUNCTION__,
2659 s,
2660 req,
2661 req->rex, req->wex,
2662 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002663 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002664 req->analysers);
2665
Willy Tarreau52a0c602009-08-16 22:45:38 +02002666 /* we're speaking HTTP here, so let's speak HTTP to the client */
2667 s->srv_error = http_return_srv_error;
2668
Willy Tarreau83e3af02009-12-28 17:39:57 +01002669 /* There's a protected area at the end of the buffer for rewriting
2670 * purposes. We don't want to start to parse the request if the
2671 * protected area is affected, because we may have to move processed
2672 * data later, which is much more complicated.
2673 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002674 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002675 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002676 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002677 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002678 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002679 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002680 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002681 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002682 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002683 return 0;
2684 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002685 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2686 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2687 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002688 }
2689
Willy Tarreau065e8332010-01-08 00:30:20 +01002690 /* Note that we have the same problem with the response ; we
2691 * may want to send a redirect, error or anything which requires
2692 * some spare space. So we'll ensure that we have at least
2693 * maxrewrite bytes available in the response buffer before
2694 * processing that one. This will only affect pipelined
2695 * keep-alive requests.
2696 */
2697 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002698 unlikely(!channel_is_rewritable(&s->res) ||
2699 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2700 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2701 if (s->res.buf->o) {
2702 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002703 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002704 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002705 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002706 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2707 s->res.flags |= CF_WAKE_WRITE;
2708 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002709 return 0;
2710 }
2711 }
2712
Willy Tarreau9b28e032012-10-12 23:49:43 +02002713 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002714 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002715 }
2716
Willy Tarreau59234e92008-11-30 23:51:27 +01002717 /* 1: we might have to print this header in debug mode */
2718 if (unlikely((global.mode & MODE_DEBUG) &&
2719 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002720 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002721 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002722
Willy Tarreau9b28e032012-10-12 23:49:43 +02002723 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002724 /* this is a bit complex : in case of error on the request line,
2725 * we know that rq.l is still zero, so we display only the part
2726 * up to the end of the line (truncated by debug_hdr).
2727 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002728 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002729 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002730
Willy Tarreau59234e92008-11-30 23:51:27 +01002731 sol += hdr_idx_first_pos(&txn->hdr_idx);
2732 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002733
Willy Tarreau59234e92008-11-30 23:51:27 +01002734 while (cur_idx) {
2735 eol = sol + txn->hdr_idx.v[cur_idx].len;
2736 debug_hdr("clihdr", s, sol, eol);
2737 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2738 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002739 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002740 }
2741
Willy Tarreau58f10d72006-12-04 02:26:12 +01002742
Willy Tarreau59234e92008-11-30 23:51:27 +01002743 /*
2744 * Now we quickly check if we have found a full valid request.
2745 * If not so, we check the FD and buffer states before leaving.
2746 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002747 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002748 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002749 * on a keep-alive stream, if we encounter and error, close, t/o,
2750 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002751 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002752 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002753 * Last, we may increase some tracked counters' http request errors on
2754 * the cases that are deliberately the client's fault. For instance,
2755 * a timeout or connection reset is not counted as an error. However
2756 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002757 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002758
Willy Tarreau655dce92009-11-08 13:10:58 +01002759 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002760 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002761 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002762 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002763 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002764 stream_inc_http_req_ctr(s);
2765 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002766 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002767 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002768 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002769
Willy Tarreau59234e92008-11-30 23:51:27 +01002770 /* 1: Since we are in header mode, if there's no space
2771 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002772 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002773 * must terminate it now.
2774 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002775 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002776 /* FIXME: check if URI is set and return Status
2777 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002778 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002779 stream_inc_http_req_ctr(s);
2780 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002781 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002782 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002783 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002784 goto return_bad_req;
2785 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002786
Willy Tarreau59234e92008-11-30 23:51:27 +01002787 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002788 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002789 if (!(s->flags & SF_ERR_MASK))
2790 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002791
Willy Tarreaufcffa692010-01-10 14:21:19 +01002792 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002793 goto failed_keep_alive;
2794
Willy Tarreau0f228a02015-05-01 15:37:53 +02002795 if (sess->fe->options & PR_O_IGNORE_PRB)
2796 goto failed_keep_alive;
2797
Willy Tarreau59234e92008-11-30 23:51:27 +01002798 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002799 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002800 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002801 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002802 }
2803
Willy Tarreaudc979f22012-12-04 10:39:01 +01002804 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002805 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002806 msg->msg_state = HTTP_MSG_ERROR;
2807 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002808
Willy Tarreau87b09662015-04-03 00:22:06 +02002809 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002810 proxy_inc_fe_req_ctr(sess->fe);
2811 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002812 if (sess->listener->counters)
2813 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002814
Willy Tarreaue7dff022015-04-03 01:14:29 +02002815 if (!(s->flags & SF_FINST_MASK))
2816 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002817 return 0;
2818 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002819
Willy Tarreau59234e92008-11-30 23:51:27 +01002820 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002821 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002822 if (!(s->flags & SF_ERR_MASK))
2823 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002824
Willy Tarreaufcffa692010-01-10 14:21:19 +01002825 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002826 goto failed_keep_alive;
2827
Willy Tarreau0f228a02015-05-01 15:37:53 +02002828 if (sess->fe->options & PR_O_IGNORE_PRB)
2829 goto failed_keep_alive;
2830
Willy Tarreau59234e92008-11-30 23:51:27 +01002831 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002832 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002833 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002834 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002835 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002836 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01002837 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002838 msg->msg_state = HTTP_MSG_ERROR;
2839 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002840
Willy Tarreau87b09662015-04-03 00:22:06 +02002841 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002842 proxy_inc_fe_req_ctr(sess->fe);
2843 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002844 if (sess->listener->counters)
2845 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002846
Willy Tarreaue7dff022015-04-03 01:14:29 +02002847 if (!(s->flags & SF_FINST_MASK))
2848 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002849 return 0;
2850 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002851
Willy Tarreau59234e92008-11-30 23:51:27 +01002852 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002853 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002854 if (!(s->flags & SF_ERR_MASK))
2855 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002856
Willy Tarreaufcffa692010-01-10 14:21:19 +01002857 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002858 goto failed_keep_alive;
2859
Willy Tarreau0f228a02015-05-01 15:37:53 +02002860 if (sess->fe->options & PR_O_IGNORE_PRB)
2861 goto failed_keep_alive;
2862
Willy Tarreau4076a152009-04-02 15:18:36 +02002863 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002864 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002865 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002866 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002867 msg->msg_state = HTTP_MSG_ERROR;
2868 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002869
Willy Tarreau87b09662015-04-03 00:22:06 +02002870 stream_inc_http_err_ctr(s);
2871 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002872 proxy_inc_fe_req_ctr(sess->fe);
2873 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002874 if (sess->listener->counters)
2875 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002876
Willy Tarreaue7dff022015-04-03 01:14:29 +02002877 if (!(s->flags & SF_FINST_MASK))
2878 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002879 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002880 }
2881
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002882 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002883 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002884 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002885#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002886 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2887 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002888 /* We need more data, we have to re-enable quick-ack in case we
2889 * previously disabled it, otherwise we might cause the client
2890 * to delay next data.
2891 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002892 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002893 }
2894#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002895
Willy Tarreaufcffa692010-01-10 14:21:19 +01002896 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2897 /* If the client starts to talk, let's fall back to
2898 * request timeout processing.
2899 */
2900 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002901 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002902 }
2903
Willy Tarreau59234e92008-11-30 23:51:27 +01002904 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002905 if (!tick_isset(req->analyse_exp)) {
2906 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2907 (txn->flags & TX_WAIT_NEXT_RQ) &&
2908 tick_isset(s->be->timeout.httpka))
2909 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2910 else
2911 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2912 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002913
Willy Tarreau59234e92008-11-30 23:51:27 +01002914 /* we're not ready yet */
2915 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002916
2917 failed_keep_alive:
2918 /* Here we process low-level errors for keep-alive requests. In
2919 * short, if the request is not the first one and it experiences
2920 * a timeout, read error or shutdown, we just silently close so
2921 * that the client can try again.
2922 */
2923 txn->status = 0;
2924 msg->msg_state = HTTP_MSG_RQBEFORE;
2925 req->analysers = 0;
2926 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002927 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002928 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau350f4872014-11-28 14:42:25 +01002929 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002930 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002931 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002932
Willy Tarreaud787e662009-07-07 10:14:51 +02002933 /* OK now we have a complete HTTP request with indexed headers. Let's
2934 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002935 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002936 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002937 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002938 * byte after the last LF. msg->sov points to the first byte of data.
2939 * msg->eol cannot be trusted because it may have been left uninitialized
2940 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002941 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002942
Willy Tarreau87b09662015-04-03 00:22:06 +02002943 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002944 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002945
Willy Tarreaub16a5742010-01-10 14:46:16 +01002946 if (txn->flags & TX_WAIT_NEXT_RQ) {
2947 /* kill the pending keep-alive timeout */
2948 txn->flags &= ~TX_WAIT_NEXT_RQ;
2949 req->analyse_exp = TICK_ETERNITY;
2950 }
2951
2952
Willy Tarreaud787e662009-07-07 10:14:51 +02002953 /* Maybe we found in invalid header name while we were configured not
2954 * to block on that, so we have to capture it now.
2955 */
2956 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002957 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002958
Willy Tarreau59234e92008-11-30 23:51:27 +01002959 /*
2960 * 1: identify the method
2961 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002962 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002963
2964 /* we can make use of server redirect on GET and HEAD */
2965 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002966 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002967
Willy Tarreau59234e92008-11-30 23:51:27 +01002968 /*
2969 * 2: check if the URI matches the monitor_uri.
2970 * We have to do this for every request which gets in, because
2971 * the monitor-uri is defined by the frontend.
2972 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002973 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2974 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002975 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002976 sess->fe->monitor_uri,
2977 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002978 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002979 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002980 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002981 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002982
Willy Tarreaue7dff022015-04-03 01:14:29 +02002983 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002984 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002985
Willy Tarreau59234e92008-11-30 23:51:27 +01002986 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002987 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002988 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002989
Willy Tarreau59234e92008-11-30 23:51:27 +01002990 ret = acl_pass(ret);
2991 if (cond->pol == ACL_COND_UNLESS)
2992 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002993
Willy Tarreau59234e92008-11-30 23:51:27 +01002994 if (ret) {
2995 /* we fail this request, let's return 503 service unavail */
2996 txn->status = 503;
Willy Tarreau350f4872014-11-28 14:42:25 +01002997 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002998 if (!(s->flags & SF_ERR_MASK))
2999 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01003000 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01003001 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003002 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01003003
Willy Tarreau59234e92008-11-30 23:51:27 +01003004 /* nothing to fail, let's reply normaly */
3005 txn->status = 200;
Willy Tarreau350f4872014-11-28 14:42:25 +01003006 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02003007 if (!(s->flags & SF_ERR_MASK))
3008 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01003009 goto return_prx_cond;
3010 }
3011
3012 /*
3013 * 3: Maybe we have to copy the original REQURI for the logs ?
3014 * Note: we cannot log anymore if the request has been
3015 * classified as invalid.
3016 */
3017 if (unlikely(s->logs.logwait & LW_REQ)) {
3018 /* we have a complete HTTP request that we must log */
3019 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
3020 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003021
Willy Tarreau59234e92008-11-30 23:51:27 +01003022 if (urilen >= REQURI_LEN)
3023 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02003024 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01003025 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003026
Willy Tarreaud79a3b22012-12-28 09:40:16 +01003027 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01003028 s->do_log(s);
3029 } else {
3030 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003031 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003032 }
Willy Tarreau06619262006-12-17 08:37:22 +01003033
Willy Tarreau91852eb2015-05-01 13:26:00 +02003034 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
3035 * exactly one digit "." one digit. This check may be disabled using
3036 * option accept-invalid-http-request.
3037 */
3038 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
3039 if (msg->sl.rq.v_l != 8) {
3040 msg->err_pos = msg->sl.rq.v;
3041 goto return_bad_req;
3042 }
3043
3044 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
3045 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
3046 req->buf->p[msg->sl.rq.v + 6] != '.' ||
3047 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
3048 msg->err_pos = msg->sl.rq.v + 4;
3049 goto return_bad_req;
3050 }
3051 }
Willy Tarreau13317662015-05-01 13:47:08 +02003052 else {
3053 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
3054 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
3055 goto return_bad_req;
3056 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02003057
Willy Tarreau5b154472009-12-21 20:11:07 +01003058 /* ... and check if the request is HTTP/1.1 or above */
3059 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003060 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
3061 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
3062 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003063 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01003064
3065 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01003066 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 +01003067
Willy Tarreau88d349d2010-01-25 12:15:43 +01003068 /* if the frontend has "option http-use-proxy-header", we'll check if
3069 * we have what looks like a proxied connection instead of a connection,
3070 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
3071 * Note that this is *not* RFC-compliant, however browsers and proxies
3072 * happen to do that despite being non-standard :-(
3073 * We consider that a request not beginning with either '/' or '*' is
3074 * a proxied connection, which covers both "scheme://location" and
3075 * CONNECT ip:port.
3076 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003077 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003078 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01003079 txn->flags |= TX_USE_PX_CONN;
3080
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003081 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003082 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003083
Willy Tarreau59234e92008-11-30 23:51:27 +01003084 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003085 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003086 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003087 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003088
Willy Tarreau557f1992015-05-01 10:05:17 +02003089 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3090 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003091 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003092 * The length of a message body is determined by one of the following
3093 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003094 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003095 * 1. Any response to a HEAD request and any response with a 1xx
3096 * (Informational), 204 (No Content), or 304 (Not Modified) status
3097 * code is always terminated by the first empty line after the
3098 * header fields, regardless of the header fields present in the
3099 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003100 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003101 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3102 * the connection will become a tunnel immediately after the empty
3103 * line that concludes the header fields. A client MUST ignore any
3104 * Content-Length or Transfer-Encoding header fields received in
3105 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003106 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003107 * 3. If a Transfer-Encoding header field is present and the chunked
3108 * transfer coding (Section 4.1) is the final encoding, the message
3109 * body length is determined by reading and decoding the chunked
3110 * data until the transfer coding indicates the data is complete.
3111 *
3112 * If a Transfer-Encoding header field is present in a response and
3113 * the chunked transfer coding is not the final encoding, the
3114 * message body length is determined by reading the connection until
3115 * it is closed by the server. If a Transfer-Encoding header field
3116 * is present in a request and the chunked transfer coding is not
3117 * the final encoding, the message body length cannot be determined
3118 * reliably; the server MUST respond with the 400 (Bad Request)
3119 * status code and then close the connection.
3120 *
3121 * If a message is received with both a Transfer-Encoding and a
3122 * Content-Length header field, the Transfer-Encoding overrides the
3123 * Content-Length. Such a message might indicate an attempt to
3124 * perform request smuggling (Section 9.5) or response splitting
3125 * (Section 9.4) and ought to be handled as an error. A sender MUST
3126 * remove the received Content-Length field prior to forwarding such
3127 * a message downstream.
3128 *
3129 * 4. If a message is received without Transfer-Encoding and with
3130 * either multiple Content-Length header fields having differing
3131 * field-values or a single Content-Length header field having an
3132 * invalid value, then the message framing is invalid and the
3133 * recipient MUST treat it as an unrecoverable error. If this is a
3134 * request message, the server MUST respond with a 400 (Bad Request)
3135 * status code and then close the connection. If this is a response
3136 * message received by a proxy, the proxy MUST close the connection
3137 * to the server, discard the received response, and send a 502 (Bad
3138 * Gateway) response to the client. If this is a response message
3139 * received by a user agent, the user agent MUST close the
3140 * connection to the server and discard the received response.
3141 *
3142 * 5. If a valid Content-Length header field is present without
3143 * Transfer-Encoding, its decimal value defines the expected message
3144 * body length in octets. If the sender closes the connection or
3145 * the recipient times out before the indicated number of octets are
3146 * received, the recipient MUST consider the message to be
3147 * incomplete and close the connection.
3148 *
3149 * 6. If this is a request message and none of the above are true, then
3150 * the message body length is zero (no message body is present).
3151 *
3152 * 7. Otherwise, this is a response message without a declared message
3153 * body length, so the message body length is determined by the
3154 * number of octets received prior to the server closing the
3155 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003156 */
3157
Willy Tarreau32b47f42009-10-18 20:55:02 +02003158 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003159 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003160 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003161 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003162 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3163 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003164 /* chunked not last, return badreq */
3165 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003166 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003167 }
3168
Willy Tarreau1c913912015-04-30 10:57:51 +02003169 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003170 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003171 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3172 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3173 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3174 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003175 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003176 signed long long cl;
3177
Willy Tarreauad14f752011-09-02 20:33:27 +02003178 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003179 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003180 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003181 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003182
Willy Tarreauad14f752011-09-02 20:33:27 +02003183 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003184 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003185 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003186 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003187
Willy Tarreauad14f752011-09-02 20:33:27 +02003188 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003189 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003190 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003191 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003192
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003193 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003194 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003195 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003196 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003197
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003198 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003199 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003200 }
3201
Willy Tarreau34dfc602015-05-01 10:09:49 +02003202 /* even bodyless requests have a known length */
3203 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003204
Willy Tarreau179085c2014-04-28 16:48:56 +02003205 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3206 * only change if both the request and the config reference something else.
3207 * Option httpclose by itself sets tunnel mode where headers are mangled.
3208 * However, if another mode is set, it will affect it (eg: server-close/
3209 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3210 * if FE and BE have the same settings (common). The method consists in
3211 * checking if options changed between the two calls (implying that either
3212 * one is non-null, or one of them is non-null and we are there for the first
3213 * time.
3214 */
3215 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003216 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003217 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003218
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003219 /* we may have to wait for the request's body */
3220 if ((s->be->options & PR_O_WREQ_BODY) &&
3221 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3222 req->analysers |= AN_REQ_HTTP_BODY;
3223
Willy Tarreaud787e662009-07-07 10:14:51 +02003224 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003225 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003226 req->analyse_exp = TICK_ETERNITY;
3227 return 1;
3228
3229 return_bad_req:
3230 /* We centralize bad requests processing here */
3231 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3232 /* we detected a parsing error. We want to archive this request
3233 * in the dedicated proxy area for later troubleshooting.
3234 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003235 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003236 }
3237
3238 txn->req.msg_state = HTTP_MSG_ERROR;
3239 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01003240 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003241
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003242 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003243 if (sess->listener->counters)
3244 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003245
3246 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003247 if (!(s->flags & SF_ERR_MASK))
3248 s->flags |= SF_ERR_PRXCOND;
3249 if (!(s->flags & SF_FINST_MASK))
3250 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003251
3252 req->analysers = 0;
3253 req->analyse_exp = TICK_ETERNITY;
3254 return 0;
3255}
3256
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003257
Willy Tarreau347a35d2013-11-22 17:51:09 +01003258/* This function prepares an applet to handle the stats. It can deal with the
3259 * "100-continue" expectation, check that admin rules are met for POST requests,
3260 * and program a response message if something was unexpected. It cannot fail
3261 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003262 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003263 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003264 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003265 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003266int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003267{
3268 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003269 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003270 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003271 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003272 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003273 struct uri_auth *uri_auth = s->be->uri_auth;
3274 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003275 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003276
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003277 appctx = si_appctx(si);
3278 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3279 appctx->st1 = appctx->st2 = 0;
3280 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3281 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003282 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003283 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003284
3285 uri = msg->chn->buf->p + msg->sl.rq.u;
3286 lookup = uri + uri_auth->uri_len;
3287
3288 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3289 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003290 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003291 break;
3292 }
3293 }
3294
3295 if (uri_auth->refresh) {
3296 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3297 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003298 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003299 break;
3300 }
3301 }
3302 }
3303
3304 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3305 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003306 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003307 break;
3308 }
3309 }
3310
3311 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3312 if (memcmp(h, ";st=", 4) == 0) {
3313 int i;
3314 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003315 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003316 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3317 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003318 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003319 break;
3320 }
3321 }
3322 break;
3323 }
3324 }
3325
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003326 appctx->ctx.stats.scope_str = 0;
3327 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003328 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3329 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3330 int itx = 0;
3331 const char *h2;
3332 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3333 const char *err;
3334
3335 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3336 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003337 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003338 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3339 itx++;
3340 h++;
3341 }
3342
3343 if (itx > STAT_SCOPE_TXT_MAXLEN)
3344 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003345 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003346
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003347 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003348 memcpy(scope_txt, h2, itx);
3349 scope_txt[itx] = '\0';
3350 err = invalid_char(scope_txt);
3351 if (err) {
3352 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003353 appctx->ctx.stats.scope_str = 0;
3354 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003355 }
3356 break;
3357 }
3358 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003359
3360 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003361 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003362 int ret = 1;
3363
3364 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003365 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 +01003366 ret = acl_pass(ret);
3367 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3368 ret = !ret;
3369 }
3370
3371 if (ret) {
3372 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003373 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003374 break;
3375 }
3376 }
3377
3378 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003379 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003380 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003381 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003382 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3383 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003384 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003385 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003386 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003387 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3388 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003389 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003390 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003391 else {
3392 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003393 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003394 }
3395
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003396 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003397 return 1;
3398}
3399
Lukas Tribus67db8df2013-06-23 17:37:13 +02003400/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3401 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3402 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003403void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003404{
3405#ifdef IP_TOS
3406 if (from.ss_family == AF_INET)
3407 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3408#endif
3409#ifdef IPV6_TCLASS
3410 if (from.ss_family == AF_INET6) {
3411 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3412 /* v4-mapped addresses need IP_TOS */
3413 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3414 else
3415 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3416 }
3417#endif
3418}
3419
Willy Tarreau87b09662015-04-03 00:22:06 +02003420int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003421 const char* name, unsigned int name_len,
3422 const char *str, struct my_regex *re,
3423 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003424{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003425 struct hdr_ctx ctx;
3426 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003427 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003428 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3429 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003430 struct chunk *output = get_trash_chunk();
3431
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003432 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003433
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003434 /* Choose the header browsing function. */
3435 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003436 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003437 http_find_hdr_func = http_find_header2;
3438 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003439 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003440 http_find_hdr_func = http_find_full_header2;
3441 break;
3442 default: /* impossible */
3443 return -1;
3444 }
3445
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003446 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3447 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003448 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003449 char *val = ctx.line + ctx.val;
3450 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003451
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003452 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3453 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003454
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003455 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003456 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003457 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003458
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003459 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003460
3461 hdr->len += delta;
3462 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003463
3464 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003465 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003466 }
3467
3468 return 0;
3469}
3470
Willy Tarreau87b09662015-04-03 00:22:06 +02003471static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003472 const char* name, unsigned int name_len,
3473 struct list *fmt, struct my_regex *re,
3474 int action)
3475{
3476 struct chunk *replace = get_trash_chunk();
3477
3478 replace->len = build_logline(s, replace->str, replace->size, fmt);
3479 if (replace->len >= replace->size - 1)
3480 return -1;
3481
3482 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3483}
3484
Willy Tarreau87b09662015-04-03 00:22:06 +02003485/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003486 * transaction <txn>. Returns the verdict of the first rule that prevents
3487 * further processing of the request (auth, deny, ...), and defaults to
3488 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3489 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3490 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003491 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003492enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003493http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003494{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003495 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003496 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003497 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003498 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003499 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003500 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003501
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003502 /* If "the current_rule_list" match the executed rule list, we are in
3503 * resume condition. If a resume is needed it is always in the action
3504 * and never in the ACL or converters. In this case, we initialise the
3505 * current rule, and go to the action execution point.
3506 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003507 if (s->current_rule) {
3508 rule = s->current_rule;
3509 s->current_rule = NULL;
3510 if (s->current_rule_list == rules)
3511 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003512 }
3513 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003514
Willy Tarreauff011f22011-01-06 17:51:27 +01003515 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003516
Willy Tarreau96257ec2012-12-27 10:46:37 +01003517 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003518 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003519 int ret;
3520
Willy Tarreau192252e2015-04-04 01:47:55 +02003521 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003522 ret = acl_pass(ret);
3523
Willy Tarreauff011f22011-01-06 17:51:27 +01003524 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003525 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003526
3527 if (!ret) /* condition not matched */
3528 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003529 }
3530
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003531resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003532 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003533 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003534 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003535
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003536 case ACT_ACTION_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003537 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003538 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003539
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003540 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003541 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003542 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003543 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003544
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003545 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003546 /* Auth might be performed on regular http-req rules as well as on stats */
3547 auth_realm = rule->arg.auth.realm;
3548 if (!auth_realm) {
3549 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3550 auth_realm = STATS_DEFAULT_REALM;
3551 else
3552 auth_realm = px->id;
3553 }
3554 /* send 401/407 depending on whether we use a proxy or not. We still
3555 * count one error, because normal browsing won't significantly
3556 * increase the counter but brute force attempts will.
3557 */
3558 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3559 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3560 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003561 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003562 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003563
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003564 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003565 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3566 return HTTP_RULE_RES_BADREQ;
3567 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003568
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003569 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003570 s->task->nice = rule->arg.nice;
3571 break;
3572
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003573 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003574 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003575 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003576 break;
3577
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003578 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003579#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003580 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003581 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003582#endif
3583 break;
3584
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003585 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003586 s->logs.level = rule->arg.loglevel;
3587 break;
3588
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003589 case ACT_HTTP_REPLACE_HDR:
3590 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003591 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3592 rule->arg.hdr_add.name_len,
3593 &rule->arg.hdr_add.fmt,
3594 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003595 return HTTP_RULE_RES_BADREQ;
3596 break;
3597
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003598 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003599 ctx.idx = 0;
3600 /* remove all occurrences of the header */
3601 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3602 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3603 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003604 }
Willy Tarreau85603282015-01-21 20:39:27 +01003605 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003606
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003607 case ACT_HTTP_SET_HDR:
3608 case ACT_HTTP_ADD_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003609 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3610 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3611 trash.len = rule->arg.hdr_add.name_len;
3612 trash.str[trash.len++] = ':';
3613 trash.str[trash.len++] = ' ';
3614 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 +01003615
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003616 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003617 /* remove all occurrences of the header */
3618 ctx.idx = 0;
3619 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3620 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3621 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3622 }
3623 }
3624
Willy Tarreau96257ec2012-12-27 10:46:37 +01003625 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3626 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003627
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003628 case ACT_HTTP_DEL_ACL:
3629 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003630 struct pat_ref *ref;
3631 char *key;
3632 int len;
3633
3634 /* collect reference */
3635 ref = pat_ref_lookup(rule->arg.map.ref);
3636 if (!ref)
3637 continue;
3638
3639 /* collect key */
3640 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3641 key = trash.str;
3642 key[len] = '\0';
3643
3644 /* perform update */
3645 /* returned code: 1=ok, 0=ko */
3646 pat_ref_delete(ref, key);
3647
3648 break;
3649 }
3650
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003651 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003652 struct pat_ref *ref;
3653 char *key;
3654 struct chunk *trash_key;
3655 int len;
3656
3657 trash_key = get_trash_chunk();
3658
3659 /* collect reference */
3660 ref = pat_ref_lookup(rule->arg.map.ref);
3661 if (!ref)
3662 continue;
3663
3664 /* collect key */
3665 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3666 key = trash_key->str;
3667 key[len] = '\0';
3668
3669 /* perform update */
3670 /* add entry only if it does not already exist */
3671 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003672 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003673
3674 break;
3675 }
3676
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003677 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003678 struct pat_ref *ref;
3679 char *key, *value;
3680 struct chunk *trash_key, *trash_value;
3681 int len;
3682
3683 trash_key = get_trash_chunk();
3684 trash_value = get_trash_chunk();
3685
3686 /* collect reference */
3687 ref = pat_ref_lookup(rule->arg.map.ref);
3688 if (!ref)
3689 continue;
3690
3691 /* collect key */
3692 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3693 key = trash_key->str;
3694 key[len] = '\0';
3695
3696 /* collect value */
3697 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3698 value = trash_value->str;
3699 value[len] = '\0';
3700
3701 /* perform update */
3702 if (pat_ref_find_elt(ref, key) != NULL)
3703 /* update entry if it exists */
3704 pat_ref_set(ref, key, value, NULL);
3705 else
3706 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003707 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003708
3709 break;
3710 }
William Lallemand73025dd2014-04-24 14:38:37 +02003711
Thierry FOURNIER42148732015-09-02 17:17:33 +02003712 case ACT_CUSTOM:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003713 switch (rule->action_ptr(rule, px, s->sess, s)) {
3714 case ACT_RET_ERR:
3715 case ACT_RET_CONT:
3716 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003717 case ACT_RET_STOP:
3718 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003719 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003720 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003721 return HTTP_RULE_RES_YIELD;
3722 }
William Lallemand73025dd2014-04-24 14:38:37 +02003723 break;
3724
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003725 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003726 /* Note: only the first valid tracking parameter of each
3727 * applies.
3728 */
3729
3730 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3731 struct stktable *t;
3732 struct stksess *ts;
3733 struct stktable_key *key;
3734 void *ptr;
3735
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003736 t = rule->arg.trk_ctr.table.t;
3737 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 +02003738
3739 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003740 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003741
3742 /* let's count a new HTTP request as it's the first time we do it */
3743 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3744 if (ptr)
3745 stktable_data_cast(ptr, http_req_cnt)++;
3746
3747 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3748 if (ptr)
3749 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3750 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3751
3752 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003753 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003754 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3755 }
3756 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003757 break;
3758
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003759 case ACT_HTTP_REQ_SET_SRC:
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003760 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
3761 struct sample *smp;
3762
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02003763 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 +02003764
3765 if (smp) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003766 if (smp->data.type == SMP_T_IPV4) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003767 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003768 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003769 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003770 } else if (smp->data.type == SMP_T_IPV6) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003771 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003772 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 +02003773 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0;
3774 }
3775 }
3776 }
3777 break;
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003778
3779 /* other flags exists, but normaly, they never be matched. */
3780 default:
3781 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003782 }
3783 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003784
3785 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003786 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003787}
3788
Willy Tarreau71241ab2012-12-27 11:30:54 +01003789
Willy Tarreau51d861a2015-05-22 17:30:48 +02003790/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3791 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3792 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3793 * is returned, the process can continue the evaluation of next rule list. If
3794 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3795 * is returned, it means the operation could not be processed and a server error
3796 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3797 * deny rule. If *YIELD is returned, the caller must call again the function
3798 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003799 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003800static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003801http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003802{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003803 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003804 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003805 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003806 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003807 struct hdr_ctx ctx;
3808
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003809 /* If "the current_rule_list" match the executed rule list, we are in
3810 * resume condition. If a resume is needed it is always in the action
3811 * and never in the ACL or converters. In this case, we initialise the
3812 * current rule, and go to the action execution point.
3813 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003814 if (s->current_rule) {
3815 rule = s->current_rule;
3816 s->current_rule = NULL;
3817 if (s->current_rule_list == rules)
3818 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003819 }
3820 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003821
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003822 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003823
3824 /* check optional condition */
3825 if (rule->cond) {
3826 int ret;
3827
Willy Tarreau192252e2015-04-04 01:47:55 +02003828 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003829 ret = acl_pass(ret);
3830
3831 if (rule->cond->pol == ACL_COND_UNLESS)
3832 ret = !ret;
3833
3834 if (!ret) /* condition not matched */
3835 continue;
3836 }
3837
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003838resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003839 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003840 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003841 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003842
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003843 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003844 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003845 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003846
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003847 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003848 s->task->nice = rule->arg.nice;
3849 break;
3850
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003851 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003852 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003853 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003854 break;
3855
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003856 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003857#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003858 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003859 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003860#endif
3861 break;
3862
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003863 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003864 s->logs.level = rule->arg.loglevel;
3865 break;
3866
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003867 case ACT_HTTP_REPLACE_HDR:
3868 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003869 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3870 rule->arg.hdr_add.name_len,
3871 &rule->arg.hdr_add.fmt,
3872 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003873 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003874 break;
3875
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003876 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003877 ctx.idx = 0;
3878 /* remove all occurrences of the header */
3879 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3880 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3881 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3882 }
Willy Tarreau85603282015-01-21 20:39:27 +01003883 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003884
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003885 case ACT_HTTP_SET_HDR:
3886 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003887 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3888 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3889 trash.len = rule->arg.hdr_add.name_len;
3890 trash.str[trash.len++] = ':';
3891 trash.str[trash.len++] = ' ';
3892 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 +01003893
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003894 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003895 /* remove all occurrences of the header */
3896 ctx.idx = 0;
3897 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3898 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3899 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3900 }
3901 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003902 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3903 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003904
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003905 case ACT_HTTP_DEL_ACL:
3906 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003907 struct pat_ref *ref;
3908 char *key;
3909 int len;
3910
3911 /* collect reference */
3912 ref = pat_ref_lookup(rule->arg.map.ref);
3913 if (!ref)
3914 continue;
3915
3916 /* collect key */
3917 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3918 key = trash.str;
3919 key[len] = '\0';
3920
3921 /* perform update */
3922 /* returned code: 1=ok, 0=ko */
3923 pat_ref_delete(ref, key);
3924
3925 break;
3926 }
3927
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003928 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003929 struct pat_ref *ref;
3930 char *key;
3931 struct chunk *trash_key;
3932 int len;
3933
3934 trash_key = get_trash_chunk();
3935
3936 /* collect reference */
3937 ref = pat_ref_lookup(rule->arg.map.ref);
3938 if (!ref)
3939 continue;
3940
3941 /* collect key */
3942 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3943 key = trash_key->str;
3944 key[len] = '\0';
3945
3946 /* perform update */
3947 /* check if the entry already exists */
3948 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003949 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003950
3951 break;
3952 }
3953
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003954 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003955 struct pat_ref *ref;
3956 char *key, *value;
3957 struct chunk *trash_key, *trash_value;
3958 int len;
3959
3960 trash_key = get_trash_chunk();
3961 trash_value = get_trash_chunk();
3962
3963 /* collect reference */
3964 ref = pat_ref_lookup(rule->arg.map.ref);
3965 if (!ref)
3966 continue;
3967
3968 /* collect key */
3969 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3970 key = trash_key->str;
3971 key[len] = '\0';
3972
3973 /* collect value */
3974 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3975 value = trash_value->str;
3976 value[len] = '\0';
3977
3978 /* perform update */
3979 if (pat_ref_find_elt(ref, key) != NULL)
3980 /* update entry if it exists */
3981 pat_ref_set(ref, key, value, NULL);
3982 else
3983 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003984 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003985
3986 break;
3987 }
William Lallemand73025dd2014-04-24 14:38:37 +02003988
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003989 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003990 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3991 return HTTP_RULE_RES_BADREQ;
3992 return HTTP_RULE_RES_DONE;
3993
Thierry FOURNIER42148732015-09-02 17:17:33 +02003994 case ACT_CUSTOM:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003995 switch (rule->action_ptr(rule, px, s->sess, s)) {
3996 case ACT_RET_ERR:
3997 case ACT_RET_CONT:
3998 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003999 case ACT_RET_STOP:
4000 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004001 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02004002 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004003 return HTTP_RULE_RES_YIELD;
4004 }
William Lallemand73025dd2014-04-24 14:38:37 +02004005 break;
4006
Thierry FOURNIER22e49012015-08-05 19:13:48 +02004007 /* other flags exists, but normaly, they never be matched. */
4008 default:
4009 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004010 }
4011 }
4012
4013 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004014 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004015}
4016
4017
Willy Tarreau71241ab2012-12-27 11:30:54 +01004018/* Perform an HTTP redirect based on the information in <rule>. The function
4019 * returns non-zero on success, or zero in case of a, irrecoverable error such
4020 * as too large a request to build a valid response.
4021 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004022static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004023{
Willy Tarreaub329a312015-05-22 16:27:37 +02004024 struct http_msg *req = &txn->req;
4025 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004026 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004027 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004028
4029 /* build redirect message */
4030 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004031 case 308:
4032 msg_fmt = HTTP_308;
4033 break;
4034 case 307:
4035 msg_fmt = HTTP_307;
4036 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004037 case 303:
4038 msg_fmt = HTTP_303;
4039 break;
4040 case 301:
4041 msg_fmt = HTTP_301;
4042 break;
4043 case 302:
4044 default:
4045 msg_fmt = HTTP_302;
4046 break;
4047 }
4048
4049 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
4050 return 0;
4051
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004052 location = trash.str + trash.len;
4053
Willy Tarreau71241ab2012-12-27 11:30:54 +01004054 switch(rule->type) {
4055 case REDIRECT_TYPE_SCHEME: {
4056 const char *path;
4057 const char *host;
4058 struct hdr_ctx ctx;
4059 int pathlen;
4060 int hostlen;
4061
4062 host = "";
4063 hostlen = 0;
4064 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004065 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004066 host = ctx.line + ctx.val;
4067 hostlen = ctx.vlen;
4068 }
4069
4070 path = http_get_path(txn);
4071 /* build message using path */
4072 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004073 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004074 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4075 int qs = 0;
4076 while (qs < pathlen) {
4077 if (path[qs] == '?') {
4078 pathlen = qs;
4079 break;
4080 }
4081 qs++;
4082 }
4083 }
4084 } else {
4085 path = "/";
4086 pathlen = 1;
4087 }
4088
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004089 if (rule->rdr_str) { /* this is an old "redirect" rule */
4090 /* check if we can add scheme + "://" + host + path */
4091 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
4092 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004093
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004094 /* add scheme */
4095 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4096 trash.len += rule->rdr_len;
4097 }
4098 else {
4099 /* add scheme with executing log format */
4100 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004101
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004102 /* check if we can add scheme + "://" + host + path */
4103 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
4104 return 0;
4105 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004106 /* add "://" */
4107 memcpy(trash.str + trash.len, "://", 3);
4108 trash.len += 3;
4109
4110 /* add host */
4111 memcpy(trash.str + trash.len, host, hostlen);
4112 trash.len += hostlen;
4113
4114 /* add path */
4115 memcpy(trash.str + trash.len, path, pathlen);
4116 trash.len += pathlen;
4117
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004118 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004119 if (trash.len && trash.str[trash.len - 1] != '/' &&
4120 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4121 if (trash.len > trash.size - 5)
4122 return 0;
4123 trash.str[trash.len] = '/';
4124 trash.len++;
4125 }
4126
4127 break;
4128 }
4129 case REDIRECT_TYPE_PREFIX: {
4130 const char *path;
4131 int pathlen;
4132
4133 path = http_get_path(txn);
4134 /* build message using path */
4135 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004136 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004137 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4138 int qs = 0;
4139 while (qs < pathlen) {
4140 if (path[qs] == '?') {
4141 pathlen = qs;
4142 break;
4143 }
4144 qs++;
4145 }
4146 }
4147 } else {
4148 path = "/";
4149 pathlen = 1;
4150 }
4151
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004152 if (rule->rdr_str) { /* this is an old "redirect" rule */
4153 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4154 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004155
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004156 /* add prefix. Note that if prefix == "/", we don't want to
4157 * add anything, otherwise it makes it hard for the user to
4158 * configure a self-redirection.
4159 */
4160 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4161 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4162 trash.len += rule->rdr_len;
4163 }
4164 }
4165 else {
4166 /* add prefix with executing log format */
4167 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4168
4169 /* Check length */
4170 if (trash.len + pathlen > trash.size - 4)
4171 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004172 }
4173
4174 /* add path */
4175 memcpy(trash.str + trash.len, path, pathlen);
4176 trash.len += pathlen;
4177
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004178 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004179 if (trash.len && trash.str[trash.len - 1] != '/' &&
4180 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4181 if (trash.len > trash.size - 5)
4182 return 0;
4183 trash.str[trash.len] = '/';
4184 trash.len++;
4185 }
4186
4187 break;
4188 }
4189 case REDIRECT_TYPE_LOCATION:
4190 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004191 if (rule->rdr_str) { /* this is an old "redirect" rule */
4192 if (trash.len + rule->rdr_len > trash.size - 4)
4193 return 0;
4194
4195 /* add location */
4196 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4197 trash.len += rule->rdr_len;
4198 }
4199 else {
4200 /* add location with executing log format */
4201 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004202
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004203 /* Check left length */
4204 if (trash.len > trash.size - 4)
4205 return 0;
4206 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004207 break;
4208 }
4209
4210 if (rule->cookie_len) {
4211 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4212 trash.len += 14;
4213 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4214 trash.len += rule->cookie_len;
4215 memcpy(trash.str + trash.len, "\r\n", 2);
4216 trash.len += 2;
4217 }
4218
4219 /* add end of headers and the keep-alive/close status.
4220 * We may choose to set keep-alive if the Location begins
4221 * with a slash, because the client will come back to the
4222 * same server.
4223 */
4224 txn->status = rule->code;
4225 /* let's log the request time */
4226 s->logs.tv_request = now;
4227
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004228 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004229 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004230 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004231 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4232 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4233 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004234 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004235 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4236 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4237 trash.len += 30;
4238 } else {
4239 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4240 trash.len += 24;
4241 }
4242 }
4243 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4244 trash.len += 4;
Willy Tarreaub329a312015-05-22 16:27:37 +02004245 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004246 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004247 bi_fast_delete(req->chn->buf, req->sov);
4248 req->next -= req->sov;
4249 req->sov = 0;
4250 s->req.analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004251 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreaub329a312015-05-22 16:27:37 +02004252 req->msg_state = HTTP_MSG_CLOSED;
4253 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004254 /* Trim any possible response */
4255 res->chn->buf->i = 0;
4256 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004257 } else {
4258 /* keep-alive not possible */
4259 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4260 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4261 trash.len += 29;
4262 } else {
4263 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4264 trash.len += 23;
4265 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004266 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreaub329a312015-05-22 16:27:37 +02004267 req->chn->analysers = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004268 }
4269
Willy Tarreaue7dff022015-04-03 01:14:29 +02004270 if (!(s->flags & SF_ERR_MASK))
4271 s->flags |= SF_ERR_LOCAL;
4272 if (!(s->flags & SF_FINST_MASK))
4273 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004274
4275 return 1;
4276}
4277
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004278/* This stream analyser runs all HTTP request processing which is common to
4279 * frontends and backends, which means blocking ACLs, filters, connection-close,
4280 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004281 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004282 * either needs more data or wants to immediately abort the request (eg: deny,
4283 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004284 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004285int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004286{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004287 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004288 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004289 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004290 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004291 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004292 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004293
Willy Tarreau655dce92009-11-08 13:10:58 +01004294 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004295 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004296 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004297 }
4298
Willy Tarreau87b09662015-04-03 00:22:06 +02004299 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 +02004300 now_ms, __FUNCTION__,
4301 s,
4302 req,
4303 req->rex, req->wex,
4304 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004305 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004306 req->analysers);
4307
Willy Tarreau65410832014-04-28 21:25:43 +02004308 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004309 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004310
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004311 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004312 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004313 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004314
Willy Tarreau0b748332014-04-29 00:13:29 +02004315 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004316 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4317 goto return_prx_yield;
4318
Willy Tarreau0b748332014-04-29 00:13:29 +02004319 case HTTP_RULE_RES_CONT:
4320 case HTTP_RULE_RES_STOP: /* nothing to do */
4321 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004322
Willy Tarreau0b748332014-04-29 00:13:29 +02004323 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4324 if (txn->flags & TX_CLTARPIT)
4325 goto tarpit;
4326 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004327
Willy Tarreau0b748332014-04-29 00:13:29 +02004328 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4329 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004330
Willy Tarreau0b748332014-04-29 00:13:29 +02004331 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004332 goto done;
4333
Willy Tarreau0b748332014-04-29 00:13:29 +02004334 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4335 goto return_bad_req;
4336 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004337 }
4338
Willy Tarreau52542592014-04-28 18:33:26 +02004339 /* OK at this stage, we know that the request was accepted according to
4340 * the http-request rules, we can check for the stats. Note that the
4341 * URI is detected *before* the req* rules in order not to be affected
4342 * by a possible reqrep, while they are processed *after* so that a
4343 * reqdeny can still block them. This clearly needs to change in 1.6!
4344 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004345 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004346 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004347 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004348 txn->status = 500;
4349 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004350 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004351
Willy Tarreaue7dff022015-04-03 01:14:29 +02004352 if (!(s->flags & SF_ERR_MASK))
4353 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004354 goto return_prx_cond;
4355 }
4356
4357 /* parse the whole stats request and extract the relevant information */
4358 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004359 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004360 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004361
Willy Tarreau0b748332014-04-29 00:13:29 +02004362 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4363 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004364
Willy Tarreau0b748332014-04-29 00:13:29 +02004365 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4366 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004367 }
4368
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004369 /* evaluate the req* rules except reqadd */
4370 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004371 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004372 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004373
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004374 if (txn->flags & TX_CLDENY)
4375 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004376
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004377 if (txn->flags & TX_CLTARPIT)
4378 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004379 }
Willy Tarreau06619262006-12-17 08:37:22 +01004380
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004381 /* add request headers from the rule sets in the same order */
4382 list_for_each_entry(wl, &px->req_add, list) {
4383 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004384 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004385 ret = acl_pass(ret);
4386 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4387 ret = !ret;
4388 if (!ret)
4389 continue;
4390 }
4391
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004392 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004393 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004394 }
4395
Willy Tarreau52542592014-04-28 18:33:26 +02004396
4397 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004398 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004399 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004400 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4401 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004402
Willy Tarreaue7dff022015-04-03 01:14:29 +02004403 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4404 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4405 if (!(s->flags & SF_FINST_MASK))
4406 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004407
Willy Tarreau70730dd2014-04-24 18:06:27 +02004408 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004409 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004410 select_compression_request_header(s, req->buf);
4411
4412 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004413 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004414 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004415 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004416
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004417 /* check whether we have some ACLs set to redirect this request */
4418 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004419 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004420 int ret;
4421
Willy Tarreau192252e2015-04-04 01:47:55 +02004422 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004423 ret = acl_pass(ret);
4424 if (rule->cond->pol == ACL_COND_UNLESS)
4425 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004426 if (!ret)
4427 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004428 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004429 if (!http_apply_redirect_rule(rule, s, txn))
4430 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004431 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004432 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004433
Willy Tarreau2be39392010-01-03 17:24:51 +01004434 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4435 * If this happens, then the data will not come immediately, so we must
4436 * send all what we have without waiting. Note that due to the small gain
4437 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004438 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004439 * itself once used.
4440 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004441 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004442
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004443 done: /* done with this analyser, continue with next ones that the calling
4444 * points will have set, if any.
4445 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004446 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004447 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4448 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004449 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004450
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004451 tarpit:
4452 /* When a connection is tarpitted, we use the tarpit timeout,
4453 * which may be the same as the connect timeout if unspecified.
4454 * If unset, then set it to zero because we really want it to
4455 * eventually expire. We build the tarpit as an analyser.
4456 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004457 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004458
4459 /* wipe the request out so that we can drop the connection early
4460 * if the client closes first.
4461 */
4462 channel_dont_connect(req);
4463 req->analysers = 0; /* remove switching rules etc... */
4464 req->analysers |= AN_REQ_HTTP_TARPIT;
4465 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4466 if (!req->analyse_exp)
4467 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004468 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004469 sess->fe->fe_counters.denied_req++;
4470 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004471 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004472 if (sess->listener->counters)
4473 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004474 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004475
4476 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004477 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004478 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004479 s->logs.tv_request = now;
CJ Ess108b1dd2015-04-07 12:03:37 -04004480 stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004481 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004482 sess->fe->fe_counters.denied_req++;
4483 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004484 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004485 if (sess->listener->counters)
4486 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004487 goto return_prx_cond;
4488
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004489 return_bad_req:
4490 /* We centralize bad requests processing here */
4491 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4492 /* we detected a parsing error. We want to archive this request
4493 * in the dedicated proxy area for later troubleshooting.
4494 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004495 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004496 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004497
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004498 txn->req.msg_state = HTTP_MSG_ERROR;
4499 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004500 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004501
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004502 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004503 if (sess->listener->counters)
4504 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004505
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004506 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004507 if (!(s->flags & SF_ERR_MASK))
4508 s->flags |= SF_ERR_PRXCOND;
4509 if (!(s->flags & SF_FINST_MASK))
4510 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004511
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004512 req->analysers = 0;
4513 req->analyse_exp = TICK_ETERNITY;
4514 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004515
4516 return_prx_yield:
4517 channel_dont_connect(req);
4518 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004519}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004520
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004521/* This function performs all the processing enabled for the current request.
4522 * It returns 1 if the processing can continue on next analysers, or zero if it
4523 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004524 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004525 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004526int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004527{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004528 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004529 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004530 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004531 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004532
Willy Tarreau655dce92009-11-08 13:10:58 +01004533 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004534 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004535 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004536 return 0;
4537 }
4538
Willy Tarreau87b09662015-04-03 00:22:06 +02004539 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 +02004540 now_ms, __FUNCTION__,
4541 s,
4542 req,
4543 req->rex, req->wex,
4544 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004545 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004546 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004547
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004548 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004549 select_compression_request_header(s, req->buf);
4550
Willy Tarreau59234e92008-11-30 23:51:27 +01004551 /*
4552 * Right now, we know that we have processed the entire headers
4553 * and that unwanted requests have been filtered out. We can do
4554 * whatever we want with the remaining request. Also, now we
4555 * may have separate values for ->fe, ->be.
4556 */
Willy Tarreau06619262006-12-17 08:37:22 +01004557
Willy Tarreau59234e92008-11-30 23:51:27 +01004558 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004559 * If HTTP PROXY is set we simply get remote server address parsing
4560 * incoming request. Note that this requires that a connection is
4561 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004562 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004563 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004564 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004565 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004566
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004567 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004568 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004569 txn->req.msg_state = HTTP_MSG_ERROR;
4570 txn->status = 500;
4571 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004572 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004573
Willy Tarreaue7dff022015-04-03 01:14:29 +02004574 if (!(s->flags & SF_ERR_MASK))
4575 s->flags |= SF_ERR_RESOURCE;
4576 if (!(s->flags & SF_FINST_MASK))
4577 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004578
4579 return 0;
4580 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004581
4582 path = http_get_path(txn);
4583 url2sa(req->buf->p + msg->sl.rq.u,
4584 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004585 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004586 /* if the path was found, we have to remove everything between
4587 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4588 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4589 * u_l characters by a single "/".
4590 */
4591 if (path) {
4592 char *cur_ptr = req->buf->p;
4593 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4594 int delta;
4595
4596 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4597 http_msg_move_end(&txn->req, delta);
4598 cur_end += delta;
4599 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4600 goto return_bad_req;
4601 }
4602 else {
4603 char *cur_ptr = req->buf->p;
4604 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4605 int delta;
4606
4607 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4608 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
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 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004614 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004615
Willy Tarreau59234e92008-11-30 23:51:27 +01004616 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004617 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004618 * Note that doing so might move headers in the request, but
4619 * the fields will stay coherent and the URI will not move.
4620 * This should only be performed in the backend.
4621 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02004622 if ((s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004623 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4624 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004625
William Lallemanda73203e2012-03-12 12:48:57 +01004626 /* add unique-id if "header-unique-id" is specified */
4627
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004628 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004629 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4630 goto return_bad_req;
4631 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004632 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004633 }
William Lallemanda73203e2012-03-12 12:48:57 +01004634
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004635 if (sess->fe->header_unique_id && s->unique_id) {
4636 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004637 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004638 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004639 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004640 goto return_bad_req;
4641 }
4642
Cyril Bontéb21570a2009-11-29 20:04:48 +01004643 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004644 * 9: add X-Forwarded-For if either the frontend or the backend
4645 * asks for it.
4646 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004647 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004648 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004649 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4650 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4651 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004652 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004653 /* The header is set to be added only if none is present
4654 * and we found it, so don't do anything.
4655 */
4656 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004657 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004658 /* Add an X-Forwarded-For header unless the source IP is
4659 * in the 'except' network range.
4660 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004661 if ((!sess->fe->except_mask.s_addr ||
4662 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4663 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004664 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004665 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004666 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004667 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004668 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004669 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004670
4671 /* Note: we rely on the backend to get the header name to be used for
4672 * x-forwarded-for, because the header is really meant for the backends.
4673 * However, if the backend did not specify any option, we have to rely
4674 * on the frontend's header name.
4675 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004676 if (s->be->fwdfor_hdr_len) {
4677 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004678 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004679 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004680 len = sess->fe->fwdfor_hdr_len;
4681 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004682 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004683 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 +01004684
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004685 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004686 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004687 }
4688 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004689 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004690 /* FIXME: for the sake of completeness, we should also support
4691 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004692 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004693 int len;
4694 char pn[INET6_ADDRSTRLEN];
4695 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004696 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004697 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004698
Willy Tarreau59234e92008-11-30 23:51:27 +01004699 /* Note: we rely on the backend to get the header name to be used for
4700 * x-forwarded-for, because the header is really meant for the backends.
4701 * However, if the backend did not specify any option, we have to rely
4702 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004703 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004704 if (s->be->fwdfor_hdr_len) {
4705 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004706 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004707 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004708 len = sess->fe->fwdfor_hdr_len;
4709 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004710 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004711 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004712
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004713 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004714 goto return_bad_req;
4715 }
4716 }
4717
4718 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004719 * 10: add X-Original-To if either the frontend or the backend
4720 * asks for it.
4721 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004722 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004723
4724 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004725 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004726 /* Add an X-Original-To header unless the destination IP is
4727 * in the 'except' network range.
4728 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004729 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004730
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004731 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004732 ((!sess->fe->except_mask_to.s_addr ||
4733 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4734 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004735 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004736 (((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 +02004737 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004738 int len;
4739 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004740 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004741
4742 /* Note: we rely on the backend to get the header name to be used for
4743 * x-original-to, because the header is really meant for the backends.
4744 * However, if the backend did not specify any option, we have to rely
4745 * on the frontend's header name.
4746 */
4747 if (s->be->orgto_hdr_len) {
4748 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004749 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004750 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004751 len = sess->fe->orgto_hdr_len;
4752 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004753 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004754 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 +02004755
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004756 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004757 goto return_bad_req;
4758 }
4759 }
4760 }
4761
Willy Tarreau50fc7772012-11-11 22:19:57 +01004762 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4763 * If an "Upgrade" token is found, the header is left untouched in order not to have
4764 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4765 * "Upgrade" is present in the Connection header.
4766 */
4767 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4768 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004769 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004770 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004771 unsigned int want_flags = 0;
4772
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004773 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004774 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004775 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004776 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004777 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004778 want_flags |= TX_CON_CLO_SET;
4779 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004780 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
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 Tarreaue36cbcb2015-04-03 15:40:56 +02004783 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004784 want_flags |= TX_CON_KAL_SET;
4785 }
4786
4787 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004788 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004789 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004790
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004791
Willy Tarreau522d6c02009-12-06 18:49:18 +01004792 /* If we have no server assigned yet and we're balancing on url_param
4793 * with a POST request, we may be interested in checking the body for
4794 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004795 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004796 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004797 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004798 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004799 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004800 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004801 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004802
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004803 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004804 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004805#ifdef TCP_QUICKACK
4806 /* We expect some data from the client. Unless we know for sure
4807 * we already have a full request, we have to re-enable quick-ack
4808 * in case we previously disabled it, otherwise we might cause
4809 * the client to delay further data.
4810 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004811 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004812 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004813 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004814 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004815 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004816#endif
4817 }
Willy Tarreau03945942009-12-22 16:50:27 +01004818
Willy Tarreau59234e92008-11-30 23:51:27 +01004819 /*************************************************************
4820 * OK, that's finished for the headers. We have done what we *
4821 * could. Let's switch to the DATA state. *
4822 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004823 req->analyse_exp = TICK_ETERNITY;
4824 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004825
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004826 /* if the server closes the connection, we want to immediately react
4827 * and close the socket to save packets and syscalls.
4828 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004829 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004830 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004831
Willy Tarreau59234e92008-11-30 23:51:27 +01004832 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004833 /* OK let's go on with the BODY now */
4834 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004835
Willy Tarreau59234e92008-11-30 23:51:27 +01004836 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004837 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004838 /* we detected a parsing error. We want to archive this request
4839 * in the dedicated proxy area for later troubleshooting.
4840 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004841 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004842 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004843
Willy Tarreau59234e92008-11-30 23:51:27 +01004844 txn->req.msg_state = HTTP_MSG_ERROR;
4845 txn->status = 400;
4846 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004847 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004848
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004849 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004850 if (sess->listener->counters)
4851 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004852
Willy Tarreaue7dff022015-04-03 01:14:29 +02004853 if (!(s->flags & SF_ERR_MASK))
4854 s->flags |= SF_ERR_PRXCOND;
4855 if (!(s->flags & SF_FINST_MASK))
4856 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004857 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004858}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004859
Willy Tarreau60b85b02008-11-30 23:28:40 +01004860/* This function is an analyser which processes the HTTP tarpit. It always
4861 * returns zero, at the beginning because it prevents any other processing
4862 * from occurring, and at the end because it terminates the request.
4863 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004864int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004865{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004866 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004867
4868 /* This connection is being tarpitted. The CLIENT side has
4869 * already set the connect expiration date to the right
4870 * timeout. We just have to check that the client is still
4871 * there and that the timeout has not expired.
4872 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004873 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004874 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004875 !tick_is_expired(req->analyse_exp, now_ms))
4876 return 0;
4877
4878 /* We will set the queue timer to the time spent, just for
4879 * logging purposes. We fake a 500 server error, so that the
4880 * attacker will not suspect his connection has been tarpitted.
4881 * It will not cause trouble to the logs because we can exclude
4882 * the tarpitted connections by filtering on the 'PT' status flags.
4883 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004884 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4885
4886 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004887 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004888 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004889
4890 req->analysers = 0;
4891 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004892
Willy Tarreaue7dff022015-04-03 01:14:29 +02004893 if (!(s->flags & SF_ERR_MASK))
4894 s->flags |= SF_ERR_PRXCOND;
4895 if (!(s->flags & SF_FINST_MASK))
4896 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004897 return 0;
4898}
4899
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004900/* This function is an analyser which waits for the HTTP request body. It waits
4901 * for either the buffer to be full, or the full advertised contents to have
4902 * reached the buffer. It must only be called after the standard HTTP request
4903 * processing has occurred, because it expects the request to be parsed and will
4904 * look for the Expect header. It may send a 100-Continue interim response. It
4905 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4906 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4907 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004908 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004909int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004910{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004911 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004912 struct http_txn *txn = s->txn;
4913 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004914
4915 /* We have to parse the HTTP request body to find any required data.
4916 * "balance url_param check_post" should have been the only way to get
4917 * into this. We were brought here after HTTP header analysis, so all
4918 * related structures are ready.
4919 */
4920
Willy Tarreau890988f2014-04-10 11:59:33 +02004921 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4922 /* This is the first call */
4923 if (msg->msg_state < HTTP_MSG_BODY)
4924 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004925
Willy Tarreau890988f2014-04-10 11:59:33 +02004926 if (msg->msg_state < HTTP_MSG_100_SENT) {
4927 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4928 * send an HTTP/1.1 100 Continue intermediate response.
4929 */
4930 if (msg->flags & HTTP_MSGF_VER_11) {
4931 struct hdr_ctx ctx;
4932 ctx.idx = 0;
4933 /* Expect is allowed in 1.1, look for it */
4934 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4935 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004936 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004937 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004938 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004939 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004940 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004941
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004942 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004943 * req->buf->p still points to the beginning of the message. We
4944 * must save the body in msg->next because it survives buffer
4945 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004946 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004947 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004948
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004949 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004950 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4951 else
4952 msg->msg_state = HTTP_MSG_DATA;
4953 }
4954
Willy Tarreau890988f2014-04-10 11:59:33 +02004955 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4956 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004957 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004958 goto missing_data;
4959
4960 /* OK we have everything we need now */
4961 goto http_end;
4962 }
4963
4964 /* OK here we're parsing a chunked-encoded message */
4965
Willy Tarreau522d6c02009-12-06 18:49:18 +01004966 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004967 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004968 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004969 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004970 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004971 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004972
Willy Tarreau115acb92009-12-26 13:56:06 +01004973 if (!ret)
4974 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004975 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004976 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004977 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004978 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004979 }
4980
Willy Tarreaud98cf932009-12-27 22:54:55 +01004981 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004982 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4983 * for at least a whole chunk or the whole content length bytes after
4984 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004985 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004986 if (msg->msg_state == HTTP_MSG_TRAILERS)
4987 goto http_end;
4988
Willy Tarreaue115b492015-05-01 23:05:14 +02004989 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004990 goto http_end;
4991
4992 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004993 /* we get here if we need to wait for more data. If the buffer is full,
4994 * we have the maximum we can expect.
4995 */
4996 if (buffer_full(req->buf, global.tune.maxrewrite))
4997 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004998
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004999 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01005000 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01005001 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02005002
Willy Tarreaue7dff022015-04-03 01:14:29 +02005003 if (!(s->flags & SF_ERR_MASK))
5004 s->flags |= SF_ERR_CLITO;
5005 if (!(s->flags & SF_FINST_MASK))
5006 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005007 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005008 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005009
5010 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005011 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005012 /* Not enough data. We'll re-use the http-request
5013 * timeout here. Ideally, we should set the timeout
5014 * relative to the accept() date. We just set the
5015 * request timeout once at the beginning of the
5016 * request.
5017 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005018 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005019 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005020 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005021 return 0;
5022 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005023
5024 http_end:
5025 /* The situation will not evolve, so let's give up on the analysis. */
5026 s->logs.tv_request = now; /* update the request timer to reflect full request */
5027 req->analysers &= ~an_bit;
5028 req->analyse_exp = TICK_ETERNITY;
5029 return 1;
5030
5031 return_bad_req: /* let's centralize all bad requests */
5032 txn->req.msg_state = HTTP_MSG_ERROR;
5033 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005034 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005035
Willy Tarreaue7dff022015-04-03 01:14:29 +02005036 if (!(s->flags & SF_ERR_MASK))
5037 s->flags |= SF_ERR_PRXCOND;
5038 if (!(s->flags & SF_FINST_MASK))
5039 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005040
Willy Tarreau522d6c02009-12-06 18:49:18 +01005041 return_err_msg:
5042 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005043 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005044 if (sess->listener->counters)
5045 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005046 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005047}
5048
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005049/* send a server's name with an outgoing request over an established connection.
5050 * Note: this function is designed to be called once the request has been scheduled
5051 * for being forwarded. This is the reason why it rewinds the buffer before
5052 * proceeding.
5053 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005054int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005055
5056 struct hdr_ctx ctx;
5057
Mark Lamourinec2247f02012-01-04 13:02:01 -05005058 char *hdr_name = be->server_id_hdr_name;
5059 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005060 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005061 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005062 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005063
William Lallemandd9e90662012-01-30 17:27:17 +01005064 ctx.idx = 0;
5065
Willy Tarreau211cdec2014-04-17 20:18:08 +02005066 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005067 if (old_o) {
5068 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005069 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005070 txn->req.next += old_o;
5071 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005072 }
5073
Willy Tarreau9b28e032012-10-12 23:49:43 +02005074 old_i = chn->buf->i;
5075 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 -05005076 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005077 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005078 }
5079
5080 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005081 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005082 memcpy(hdr_val, hdr_name, hdr_name_len);
5083 hdr_val += hdr_name_len;
5084 *hdr_val++ = ':';
5085 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005086 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5087 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005088
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005089 if (old_o) {
5090 /* If this was a forwarded request, we must readjust the amount of
5091 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005092 * variations. Note that the current state is >= HTTP_MSG_BODY,
5093 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005094 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005095 old_o += chn->buf->i - old_i;
5096 b_adv(chn->buf, old_o);
5097 txn->req.next -= old_o;
5098 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005099 }
5100
Mark Lamourinec2247f02012-01-04 13:02:01 -05005101 return 0;
5102}
5103
Willy Tarreau610ecce2010-01-04 21:15:02 +01005104/* Terminate current transaction and prepare a new one. This is very tricky
5105 * right now but it works.
5106 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005107void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005108{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005109 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005110 struct proxy *fe = strm_fe(s);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005111 struct connection *srv_conn;
5112 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005113 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005114
Willy Tarreau610ecce2010-01-04 21:15:02 +01005115 /* FIXME: We need a more portable way of releasing a backend's and a
5116 * server's connections. We need a safer way to reinitialize buffer
5117 * flags. We also need a more accurate method for computing per-request
5118 * data.
5119 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005120 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005121
Willy Tarreau4213a112013-12-15 10:25:42 +01005122 /* unless we're doing keep-alive, we want to quickly close the connection
5123 * to the server.
5124 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005125 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005126 !si_conn_ready(&s->si[1])) {
5127 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5128 si_shutr(&s->si[1]);
5129 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005130 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005131
Willy Tarreaue7dff022015-04-03 01:14:29 +02005132 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005133 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005134 if (unlikely(s->srv_conn))
5135 sess_change_server(s, NULL);
5136 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005137
5138 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005139 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005140
Willy Tarreaueee5b512015-04-03 23:46:31 +02005141 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005142 int n;
5143
Willy Tarreaueee5b512015-04-03 23:46:31 +02005144 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005145 if (n < 1 || n > 5)
5146 n = 0;
5147
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005148 if (fe->mode == PR_MODE_HTTP) {
5149 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005150 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005151 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005152 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005153 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005154 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005155 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005156 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005157 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005158 s->be->be_counters.p.http.comp_rsp++;
5159 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005160 }
5161
5162 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005163 s->logs.bytes_in -= s->req.buf->i;
5164 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005165
5166 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005167 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005168 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005169 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005170 s->do_log(s);
5171 }
5172
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005173 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005174 stream_stop_content_counters(s);
5175 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005176
Willy Tarreau610ecce2010-01-04 21:15:02 +01005177 s->logs.accept_date = date; /* user-visible date for logging */
5178 s->logs.tv_accept = now; /* corrected date for internal use */
5179 tv_zero(&s->logs.tv_request);
5180 s->logs.t_queue = -1;
5181 s->logs.t_connect = -1;
5182 s->logs.t_data = -1;
5183 s->logs.t_close = 0;
5184 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5185 s->logs.srv_queue_size = 0; /* we will get this number soon */
5186
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005187 s->logs.bytes_in = s->req.total = s->req.buf->i;
5188 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005189
5190 if (s->pend_pos)
5191 pendconn_free(s->pend_pos);
5192
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005193 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005194 if (s->flags & SF_CURR_SESS) {
5195 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005196 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005197 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005198 if (may_dequeue_tasks(objt_server(s->target), s->be))
5199 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005200 }
5201
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005202 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005203
Willy Tarreau4213a112013-12-15 10:25:42 +01005204 /* only release our endpoint if we don't intend to reuse the
5205 * connection.
5206 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005207 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005208 !si_conn_ready(&s->si[1])) {
5209 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005210 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005211 }
5212
Willy Tarreau350f4872014-11-28 14:42:25 +01005213 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5214 s->si[1].err_type = SI_ET_NONE;
5215 s->si[1].conn_retries = 0; /* used for logging too */
5216 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005217 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 +01005218 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);
5219 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 +02005220 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5221 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5222 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005223
Willy Tarreaueee5b512015-04-03 23:46:31 +02005224 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005225 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005226 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005227
5228 if (prev_status == 401 || prev_status == 407) {
5229 /* In HTTP keep-alive mode, if we receive a 401, we still have
5230 * a chance of being able to send the visitor again to the same
5231 * server over the same connection. This is required by some
5232 * broken protocols such as NTLM, and anyway whenever there is
5233 * an opportunity for sending the challenge to the proper place,
5234 * it's better to do it (at least it helps with debugging).
5235 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005236 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005237 if (srv_conn)
5238 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005239 }
5240
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005241 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005242 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005243
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005244 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005245 s->req.flags |= CF_NEVER_WAIT;
5246 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005247 }
5248
Willy Tarreau610ecce2010-01-04 21:15:02 +01005249 /* if the request buffer is not empty, it means we're
5250 * about to process another request, so send pending
5251 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005252 * Just don't do this if the buffer is close to be full,
5253 * because the request will wait for it to flush a little
5254 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005255 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005256 if (s->req.buf->i) {
5257 if (s->res.buf->o &&
5258 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5259 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5260 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005261 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005262
5263 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005264 channel_auto_read(&s->req);
5265 channel_auto_close(&s->req);
5266 channel_auto_read(&s->res);
5267 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005268
Willy Tarreau27375622013-12-17 00:00:28 +01005269 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005270 if (srv_conn) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005271 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005272 if (!srv)
5273 si_idle_conn(&s->si[1], NULL);
5274 else if ((srv_conn->flags & CO_FL_PRIVATE) ||
5275 ((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR))
5276 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005277 else if (prev_flags & TX_NOT_FIRST)
5278 /* note: we check the request, not the connection, but
5279 * this is valid for strategies SAFE and AGGR, and in
5280 * case of ALWS, we don't care anyway.
5281 */
5282 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005283 else
5284 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005285 }
Willy Tarreau27375622013-12-17 00:00:28 +01005286
Willy Tarreauc29d0cd2015-09-23 12:18:14 +02005287 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005288 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005289}
5290
5291
5292/* This function updates the request state machine according to the response
5293 * state machine and buffer flags. It returns 1 if it changes anything (flag
5294 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5295 * it is only used to find when a request/response couple is complete. Both
5296 * this function and its equivalent should loop until both return zero. It
5297 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5298 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005299int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005300{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005301 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005302 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005303 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005304 unsigned int old_state = txn->req.msg_state;
5305
Willy Tarreau610ecce2010-01-04 21:15:02 +01005306 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5307 return 0;
5308
5309 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005310 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005311 * We can shut the read side unless we want to abort_on_close,
5312 * or we have a POST request. The issue with POST requests is
5313 * that some browsers still send a CRLF after the request, and
5314 * this CRLF must be read so that it does not remain in the kernel
5315 * buffers, otherwise a close could cause an RST on some systems
5316 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005317 * Note that if we're using keep-alive on the client side, we'd
5318 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005319 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005320 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005321 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005322 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5323 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5324 !(s->be->options & PR_O_ABRT_CLOSE) &&
5325 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005326 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005327
Willy Tarreau40f151a2012-12-20 12:10:09 +01005328 /* if the server closes the connection, we want to immediately react
5329 * and close the socket to save packets and syscalls.
5330 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005331 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005332
Willy Tarreau610ecce2010-01-04 21:15:02 +01005333 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5334 goto wait_other_side;
5335
5336 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5337 /* The server has not finished to respond, so we
5338 * don't want to move in order not to upset it.
5339 */
5340 goto wait_other_side;
5341 }
5342
5343 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5344 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005345 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005346 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005347 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005348 goto wait_other_side;
5349 }
5350
5351 /* When we get here, it means that both the request and the
5352 * response have finished receiving. Depending on the connection
5353 * mode, we'll have to wait for the last bytes to leave in either
5354 * direction, and sometimes for a close to be effective.
5355 */
5356
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005357 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5358 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005359 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5360 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005361 }
5362 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5363 /* Option forceclose is set, or either side wants to close,
5364 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005365 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005366 * once both states are CLOSED.
5367 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005368 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5369 channel_shutr_now(chn);
5370 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005371 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005372 }
5373 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005374 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5375 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005376 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005377 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5378 channel_auto_read(chn);
5379 txn->req.msg_state = HTTP_MSG_TUNNEL;
5380 chn->flags |= CF_NEVER_WAIT;
5381 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005382 }
5383
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005384 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005385 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005386 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005387
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005388 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005389 txn->req.msg_state = HTTP_MSG_CLOSING;
5390 goto http_msg_closing;
5391 }
5392 else {
5393 txn->req.msg_state = HTTP_MSG_CLOSED;
5394 goto http_msg_closed;
5395 }
5396 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005397 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005398 }
5399
5400 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5401 http_msg_closing:
5402 /* nothing else to forward, just waiting for the output buffer
5403 * to be empty and for the shutw_now to take effect.
5404 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005405 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005406 txn->req.msg_state = HTTP_MSG_CLOSED;
5407 goto http_msg_closed;
5408 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005409 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005410 txn->req.msg_state = HTTP_MSG_ERROR;
5411 goto wait_other_side;
5412 }
5413 }
5414
5415 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5416 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005417 /* see above in MSG_DONE why we only do this in these states */
5418 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5419 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5420 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005421 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005422 goto wait_other_side;
5423 }
5424
5425 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005426 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005427}
5428
5429
5430/* This function updates the response state machine according to the request
5431 * state machine and buffer flags. It returns 1 if it changes anything (flag
5432 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5433 * it is only used to find when a request/response couple is complete. Both
5434 * this function and its equivalent should loop until both return zero. It
5435 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5436 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005437int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005438{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005439 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005440 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005441 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005442 unsigned int old_state = txn->rsp.msg_state;
5443
Willy Tarreau610ecce2010-01-04 21:15:02 +01005444 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5445 return 0;
5446
5447 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5448 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005449 * still monitor the server connection for a possible close
5450 * while the request is being uploaded, so we don't disable
5451 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005452 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005453 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005454
5455 if (txn->req.msg_state == HTTP_MSG_ERROR)
5456 goto wait_other_side;
5457
5458 if (txn->req.msg_state < HTTP_MSG_DONE) {
5459 /* The client seems to still be sending data, probably
5460 * because we got an error response during an upload.
5461 * We have the choice of either breaking the connection
5462 * or letting it pass through. Let's do the later.
5463 */
5464 goto wait_other_side;
5465 }
5466
5467 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5468 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005469 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005470 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005471 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005472 goto wait_other_side;
5473 }
5474
5475 /* When we get here, it means that both the request and the
5476 * response have finished receiving. Depending on the connection
5477 * mode, we'll have to wait for the last bytes to leave in either
5478 * direction, and sometimes for a close to be effective.
5479 */
5480
5481 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5482 /* Server-close mode : shut read and wait for the request
5483 * side to close its output buffer. The caller will detect
5484 * when we're in DONE and the other is in CLOSED and will
5485 * catch that for the final cleanup.
5486 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005487 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5488 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005489 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005490 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5491 /* Option forceclose is set, or either side wants to close,
5492 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005493 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005494 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005495 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005496 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5497 channel_shutr_now(chn);
5498 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005499 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005500 }
5501 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005502 /* The last possible modes are keep-alive and tunnel. Tunnel will
5503 * need to forward remaining data. Keep-alive will need to monitor
5504 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005505 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005506 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005507 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005508 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5509 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005510 }
5511
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005512 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005513 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005514 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005515 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5516 goto http_msg_closing;
5517 }
5518 else {
5519 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5520 goto http_msg_closed;
5521 }
5522 }
5523 goto wait_other_side;
5524 }
5525
5526 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5527 http_msg_closing:
5528 /* nothing else to forward, just waiting for the output buffer
5529 * to be empty and for the shutw_now to take effect.
5530 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005531 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005532 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5533 goto http_msg_closed;
5534 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005535 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005536 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005537 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005538 if (objt_server(s->target))
5539 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005540 goto wait_other_side;
5541 }
5542 }
5543
5544 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5545 http_msg_closed:
5546 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005547 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005548 channel_auto_close(chn);
5549 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005550 goto wait_other_side;
5551 }
5552
5553 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005554 /* We force the response to leave immediately if we're waiting for the
5555 * other side, since there is no pending shutdown to push it out.
5556 */
5557 if (!channel_is_empty(chn))
5558 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005559 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005560}
5561
5562
5563/* Resync the request and response state machines. Return 1 if either state
5564 * changes.
5565 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005566int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005567{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005568 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005569 int old_req_state = txn->req.msg_state;
5570 int old_res_state = txn->rsp.msg_state;
5571
Willy Tarreau610ecce2010-01-04 21:15:02 +01005572 http_sync_req_state(s);
5573 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005574 if (!http_sync_res_state(s))
5575 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005576 if (!http_sync_req_state(s))
5577 break;
5578 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005579
Willy Tarreau610ecce2010-01-04 21:15:02 +01005580 /* OK, both state machines agree on a compatible state.
5581 * There are a few cases we're interested in :
5582 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5583 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5584 * directions, so let's simply disable both analysers.
5585 * - HTTP_MSG_CLOSED on the response only means we must abort the
5586 * request.
5587 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5588 * with server-close mode means we've completed one request and we
5589 * must re-initialize the server connection.
5590 */
5591
5592 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5593 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5594 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5595 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005596 s->req.analysers = 0;
5597 channel_auto_close(&s->req);
5598 channel_auto_read(&s->req);
5599 s->res.analysers = 0;
5600 channel_auto_close(&s->res);
5601 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005602 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005603 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005604 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005605 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005606 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005607 s->res.analysers = 0;
5608 channel_auto_close(&s->res);
5609 channel_auto_read(&s->res);
5610 s->req.analysers = 0;
5611 channel_abort(&s->req);
5612 channel_auto_close(&s->req);
5613 channel_auto_read(&s->req);
5614 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005615 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005616 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5617 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005618 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005619 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5620 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5621 /* server-close/keep-alive: terminate this transaction,
5622 * possibly killing the server connection and reinitialize
5623 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005624 */
5625 http_end_txn_clean_session(s);
5626 }
5627
Willy Tarreau610ecce2010-01-04 21:15:02 +01005628 return txn->req.msg_state != old_req_state ||
5629 txn->rsp.msg_state != old_res_state;
5630}
5631
Willy Tarreaud98cf932009-12-27 22:54:55 +01005632/* This function is an analyser which forwards request body (including chunk
5633 * sizes if any). It is called as soon as we must forward, even if we forward
5634 * zero byte. The only situation where it must not be called is when we're in
5635 * tunnel mode and we want to forward till the close. It's used both to forward
5636 * remaining data and to resync after end of body. It expects the msg_state to
5637 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005638 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005639 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005640 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005641 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005642int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005643{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005644 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005645 struct http_txn *txn = s->txn;
5646 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005647
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005648 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5649 return 0;
5650
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005651 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005652 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005653 /* Output closed while we were sending data. We must abort and
5654 * wake the other side up.
5655 */
5656 msg->msg_state = HTTP_MSG_ERROR;
5657 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005658 return 1;
5659 }
5660
Willy Tarreaud98cf932009-12-27 22:54:55 +01005661 /* Note that we don't have to send 100-continue back because we don't
5662 * need the data to complete our job, and it's up to the server to
5663 * decide whether to return 100, 417 or anything else in return of
5664 * an "Expect: 100-continue" header.
5665 */
5666
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005667 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005668 /* we have msg->sov which points to the first byte of message
5669 * body, and req->buf.p still points to the beginning of the
5670 * message. We forward the headers now, as we don't need them
5671 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005672 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005673 b_adv(req->buf, msg->sov);
5674 msg->next -= msg->sov;
5675 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005676
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005677 /* The previous analysers guarantee that the state is somewhere
5678 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5679 * msg->next are always correct.
5680 */
5681 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5682 if (msg->flags & HTTP_MSGF_TE_CHNK)
5683 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5684 else
5685 msg->msg_state = HTTP_MSG_DATA;
5686 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005687 }
5688
Willy Tarreau7ba23542014-04-17 21:50:00 +02005689 /* Some post-connect processing might want us to refrain from starting to
5690 * forward data. Currently, the only reason for this is "balance url_param"
5691 * whichs need to parse/process the request after we've enabled forwarding.
5692 */
5693 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005694 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005695 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005696 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005697 goto missing_data;
5698 }
5699 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5700 }
5701
Willy Tarreau80a92c02014-03-12 10:41:13 +01005702 /* in most states, we should abort in case of early close */
5703 channel_auto_close(req);
5704
Willy Tarreauefdf0942014-04-24 20:08:57 +02005705 if (req->to_forward) {
5706 /* We can't process the buffer's contents yet */
5707 req->flags |= CF_WAKE_WRITE;
5708 goto missing_data;
5709 }
5710
Willy Tarreaud98cf932009-12-27 22:54:55 +01005711 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005712 if (msg->msg_state == HTTP_MSG_DATA) {
5713 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005714 /* we may have some pending data starting at req->buf->p */
5715 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005716 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005717 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005718 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005719 msg->next += msg->chunk_len;
5720 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005721
5722 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005723 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005724 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005725 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005726 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005727 }
5728 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005729 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005730 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005731 * TRAILERS state.
5732 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005733 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005734
Willy Tarreau54d23df2012-10-25 19:04:45 +02005735 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005736 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005737 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005738 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005739 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005740 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005741 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005742 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005743 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005744 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005745 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005746 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005747 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005748
5749 if (ret == 0)
5750 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005751 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005752 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005753 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005754 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005755 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005756 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005757 /* we're in MSG_CHUNK_SIZE now */
5758 }
5759 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005760 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005761
5762 if (ret == 0)
5763 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005764 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005765 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005766 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005767 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005768 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005769 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005770 /* we're in HTTP_MSG_DONE now */
5771 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005772 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005773 int old_state = msg->msg_state;
5774
Willy Tarreau610ecce2010-01-04 21:15:02 +01005775 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005776
5777 /* we may have some pending data starting at req->buf->p
5778 * such as last chunk of data or trailers.
5779 */
5780 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005781 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005782 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005783 msg->next = 0;
5784
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005785 /* we don't want to forward closes on DONE except in
5786 * tunnel mode.
5787 */
5788 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005789 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005790 if (http_resync_states(s)) {
5791 /* some state changes occurred, maybe the analyser
5792 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005793 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005794 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005795 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005796 /* request errors are most likely due to
5797 * the server aborting the transfer.
5798 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005799 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005800 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005801 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005802 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005803 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005804 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005805 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005806 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005807
5808 /* If "option abortonclose" is set on the backend, we
5809 * want to monitor the client's connection and forward
5810 * any shutdown notification to the server, which will
5811 * decide whether to close or to go on processing the
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005812 * request. We only do that in tunnel mode, and not in
5813 * other modes since it can be abused to exhaust source
5814 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005815 */
5816 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005817 channel_auto_read(req);
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005818 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5819 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5820 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005821 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005822 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005823 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005824 /* POST requests may require to read extra CRLF
5825 * sent by broken browsers and which could cause
5826 * an RST to be sent upon close on some systems
5827 * (eg: Linux).
5828 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005829 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005830 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005831
Willy Tarreau610ecce2010-01-04 21:15:02 +01005832 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005833 }
5834 }
5835
Willy Tarreaud98cf932009-12-27 22:54:55 +01005836 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005837 /* we may have some pending data starting at req->buf->p */
5838 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005839 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005840 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5841
Willy Tarreaubed410e2014-04-22 08:19:34 +02005842 msg->next = 0;
5843 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5844
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005845 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005846 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005847 if (!(s->flags & SF_ERR_MASK))
5848 s->flags |= SF_ERR_CLICL;
5849 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005850 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005851 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005852 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005853 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005854 }
5855
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005856 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005857 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005858 if (objt_server(s->target))
5859 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005860
5861 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005862 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005863
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005864 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005865 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005866 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005867
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005868 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005869 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005870 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005871 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005872 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005873
Willy Tarreau5c620922011-05-11 19:56:11 +02005874 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005875 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005876 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005877 * modes are already handled by the stream sock layer. We must not do
5878 * this in content-length mode because it could present the MSG_MORE
5879 * flag with the last block of forwarded data, which would cause an
5880 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005881 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005882 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005883 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005884
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005885 return 0;
5886
Willy Tarreaud98cf932009-12-27 22:54:55 +01005887 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005888 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005889 if (sess->listener->counters)
5890 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005891
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005892 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005893 /* we may have some pending data starting at req->buf->p */
5894 b_adv(req->buf, msg->next);
5895 msg->next = 0;
5896
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005897 txn->req.msg_state = HTTP_MSG_ERROR;
5898 if (txn->status) {
5899 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005900 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005901 } else {
5902 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005903 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005904 }
5905 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005906 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005907
Willy Tarreaue7dff022015-04-03 01:14:29 +02005908 if (!(s->flags & SF_ERR_MASK))
5909 s->flags |= SF_ERR_PRXCOND;
5910 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005911 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005912 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005913 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005914 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005915 }
5916 return 0;
5917
5918 aborted_xfer:
5919 txn->req.msg_state = HTTP_MSG_ERROR;
5920 if (txn->status) {
5921 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005922 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005923 } else {
5924 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005925 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005926 }
5927 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005928 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005929
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005930 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005931 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005932 if (objt_server(s->target))
5933 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005934
Willy Tarreaue7dff022015-04-03 01:14:29 +02005935 if (!(s->flags & SF_ERR_MASK))
5936 s->flags |= SF_ERR_SRVCL;
5937 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005938 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005939 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005940 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005941 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005942 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005943 return 0;
5944}
5945
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005946/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5947 * processing can continue on next analysers, or zero if it either needs more
5948 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005949 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005950 * when it has nothing left to do, and may remove any analyser when it wants to
5951 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005952 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005953int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005954{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005955 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005956 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005957 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005958 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005959 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005960 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005961 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005962
Willy Tarreau87b09662015-04-03 00:22:06 +02005963 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 +02005964 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005965 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005966 rep,
5967 rep->rex, rep->wex,
5968 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005969 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005970 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005971
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005972 /*
5973 * Now parse the partial (or complete) lines.
5974 * We will check the response syntax, and also join multi-line
5975 * headers. An index of all the lines will be elaborated while
5976 * parsing.
5977 *
5978 * For the parsing, we use a 28 states FSM.
5979 *
5980 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005981 * rep->buf->p = beginning of response
5982 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5983 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005984 * msg->eol = end of current header or line (LF or CRLF)
5985 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005986 */
5987
Willy Tarreau628c40c2014-04-24 19:11:26 +02005988 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005989 /* There's a protected area at the end of the buffer for rewriting
5990 * purposes. We don't want to start to parse the request if the
5991 * protected area is affected, because we may have to move processed
5992 * data later, which is much more complicated.
5993 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005994 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005995 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005996 /* some data has still not left the buffer, wake us once that's done */
5997 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5998 goto abort_response;
5999 channel_dont_close(rep);
6000 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01006001 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02006002 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01006003 }
6004
Willy Tarreau379357a2013-06-08 12:55:46 +02006005 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
6006 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
6007 buffer_slow_realign(rep->buf);
6008
Willy Tarreau9b28e032012-10-12 23:49:43 +02006009 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01006010 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01006011 }
6012
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006013 /* 1: we might have to print this header in debug mode */
6014 if (unlikely((global.mode & MODE_DEBUG) &&
6015 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02006016 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006017 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006018
Willy Tarreau9b28e032012-10-12 23:49:43 +02006019 sol = rep->buf->p;
6020 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006021 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006022
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006023 sol += hdr_idx_first_pos(&txn->hdr_idx);
6024 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006025
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006026 while (cur_idx) {
6027 eol = sol + txn->hdr_idx.v[cur_idx].len;
6028 debug_hdr("srvhdr", s, sol, eol);
6029 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
6030 cur_idx = txn->hdr_idx.v[cur_idx].next;
6031 }
6032 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006033
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006034 /*
6035 * Now we quickly check if we have found a full valid response.
6036 * If not so, we check the FD and buffer states before leaving.
6037 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01006038 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006039 * responses are checked first.
6040 *
6041 * Depending on whether the client is still there or not, we
6042 * may send an error response back or not. Note that normally
6043 * we should only check for HTTP status there, and check I/O
6044 * errors somewhere else.
6045 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006046
Willy Tarreau655dce92009-11-08 13:10:58 +01006047 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006048 /* Invalid response */
6049 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6050 /* we detected a parsing error. We want to archive this response
6051 * in the dedicated proxy area for later troubleshooting.
6052 */
6053 hdr_response_bad:
6054 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006055 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006056
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006057 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006058 if (objt_server(s->target)) {
6059 objt_server(s->target)->counters.failed_resp++;
6060 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006061 }
Willy Tarreau64648412010-03-05 10:41:54 +01006062 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006063 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006064 rep->analysers = 0;
6065 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006066 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006067 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006068 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006069
Willy Tarreaue7dff022015-04-03 01:14:29 +02006070 if (!(s->flags & SF_ERR_MASK))
6071 s->flags |= SF_ERR_PRXCOND;
6072 if (!(s->flags & SF_FINST_MASK))
6073 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006074
6075 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006076 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006077
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006078 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006079 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006080 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006081 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006082 goto hdr_response_bad;
6083 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006084
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006085 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006086 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006087 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006088 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006089 else if (txn->flags & TX_NOT_FIRST)
6090 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006091
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006092 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006093 if (objt_server(s->target)) {
6094 objt_server(s->target)->counters.failed_resp++;
6095 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006096 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006097
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006098 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006099 rep->analysers = 0;
6100 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006101 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006102 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006103 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006104
Willy Tarreaue7dff022015-04-03 01:14:29 +02006105 if (!(s->flags & SF_ERR_MASK))
6106 s->flags |= SF_ERR_SRVCL;
6107 if (!(s->flags & SF_FINST_MASK))
6108 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006109 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006110 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006111
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006112 /* read timeout : return a 504 to the client. */
6113 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006114 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006115 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006116 else if (txn->flags & TX_NOT_FIRST)
6117 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006118
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006119 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006120 if (objt_server(s->target)) {
6121 objt_server(s->target)->counters.failed_resp++;
6122 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006123 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006124
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006125 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006126 rep->analysers = 0;
6127 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006128 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006129 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006130 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006131
Willy Tarreaue7dff022015-04-03 01:14:29 +02006132 if (!(s->flags & SF_ERR_MASK))
6133 s->flags |= SF_ERR_SRVTO;
6134 if (!(s->flags & SF_FINST_MASK))
6135 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006136 return 0;
6137 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006138
Willy Tarreauf003d372012-11-26 13:35:37 +01006139 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006140 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006141 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006142 s->be->be_counters.cli_aborts++;
6143 if (objt_server(s->target))
6144 objt_server(s->target)->counters.cli_aborts++;
6145
6146 rep->analysers = 0;
6147 channel_auto_close(rep);
6148
6149 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006150 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006151 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006152
Willy Tarreaue7dff022015-04-03 01:14:29 +02006153 if (!(s->flags & SF_ERR_MASK))
6154 s->flags |= SF_ERR_CLICL;
6155 if (!(s->flags & SF_FINST_MASK))
6156 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006157
Willy Tarreau87b09662015-04-03 00:22:06 +02006158 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006159 return 0;
6160 }
6161
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006162 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006163 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006164 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006165 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006166 else if (txn->flags & TX_NOT_FIRST)
6167 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006168
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006169 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006170 if (objt_server(s->target)) {
6171 objt_server(s->target)->counters.failed_resp++;
6172 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006173 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006174
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006175 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006176 rep->analysers = 0;
6177 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006178 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006179 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006180 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006181
Willy Tarreaue7dff022015-04-03 01:14:29 +02006182 if (!(s->flags & SF_ERR_MASK))
6183 s->flags |= SF_ERR_SRVCL;
6184 if (!(s->flags & SF_FINST_MASK))
6185 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006186 return 0;
6187 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006188
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006189 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006190 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006191 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006192 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006193 else if (txn->flags & TX_NOT_FIRST)
6194 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006195
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006196 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006197 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006198 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006199
Willy Tarreaue7dff022015-04-03 01:14:29 +02006200 if (!(s->flags & SF_ERR_MASK))
6201 s->flags |= SF_ERR_CLICL;
6202 if (!(s->flags & SF_FINST_MASK))
6203 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006204
Willy Tarreau87b09662015-04-03 00:22:06 +02006205 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006206 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006207 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006208
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006209 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006210 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006211 return 0;
6212 }
6213
6214 /* More interesting part now : we know that we have a complete
6215 * response which at least looks like HTTP. We have an indicator
6216 * of each header's length, so we can parse them quickly.
6217 */
6218
6219 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006220 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006221
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006222 /*
6223 * 1: get the status code
6224 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006225 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006226 if (n < 1 || n > 5)
6227 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006228 /* when the client triggers a 4xx from the server, it's most often due
6229 * to a missing object or permission. These events should be tracked
6230 * because if they happen often, it may indicate a brute force or a
6231 * vulnerability scan.
6232 */
6233 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006234 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006235
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006236 if (objt_server(s->target))
6237 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006238
Willy Tarreau91852eb2015-05-01 13:26:00 +02006239 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6240 * exactly one digit "." one digit. This check may be disabled using
6241 * option accept-invalid-http-response.
6242 */
6243 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6244 if (msg->sl.st.v_l != 8) {
6245 msg->err_pos = 0;
6246 goto hdr_response_bad;
6247 }
6248
6249 if (rep->buf->p[4] != '/' ||
6250 !isdigit((unsigned char)rep->buf->p[5]) ||
6251 rep->buf->p[6] != '.' ||
6252 !isdigit((unsigned char)rep->buf->p[7])) {
6253 msg->err_pos = 4;
6254 goto hdr_response_bad;
6255 }
6256 }
6257
Willy Tarreau5b154472009-12-21 20:11:07 +01006258 /* check if the response is HTTP/1.1 or above */
6259 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006260 ((rep->buf->p[5] > '1') ||
6261 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006262 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006263
6264 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006265 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 +01006266
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006267 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006268 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006269
Willy Tarreau9b28e032012-10-12 23:49:43 +02006270 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006271
Willy Tarreau39650402010-03-15 19:44:39 +01006272 /* Adjust server's health based on status code. Note: status codes 501
6273 * and 505 are triggered on demand by client request, so we must not
6274 * count them as server failures.
6275 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006276 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006277 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006278 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006279 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006280 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006281 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006282
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006283 /*
6284 * 2: check for cacheability.
6285 */
6286
6287 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006288 case 100:
6289 /*
6290 * We may be facing a 100-continue response, in which case this
6291 * is not the right response, and we're waiting for the next one.
6292 * Let's allow this response to go to the client and wait for the
6293 * next one.
6294 */
6295 hdr_idx_init(&txn->hdr_idx);
6296 msg->next -= channel_forward(rep, msg->next);
6297 msg->msg_state = HTTP_MSG_RPBEFORE;
6298 txn->status = 0;
6299 s->logs.t_data = -1; /* was not a response yet */
6300 goto next_one;
6301
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006302 case 200:
6303 case 203:
6304 case 206:
6305 case 300:
6306 case 301:
6307 case 410:
6308 /* RFC2616 @13.4:
6309 * "A response received with a status code of
6310 * 200, 203, 206, 300, 301 or 410 MAY be stored
6311 * by a cache (...) unless a cache-control
6312 * directive prohibits caching."
6313 *
6314 * RFC2616 @9.5: POST method :
6315 * "Responses to this method are not cacheable,
6316 * unless the response includes appropriate
6317 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006318 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006319 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006320 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006321 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6322 break;
6323 default:
6324 break;
6325 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006326
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006327 /*
6328 * 3: we may need to capture headers
6329 */
6330 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006331 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006332 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006333 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006334
Willy Tarreau557f1992015-05-01 10:05:17 +02006335 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6336 * by RFC7230#3.3.3 :
6337 *
6338 * The length of a message body is determined by one of the following
6339 * (in order of precedence):
6340 *
6341 * 1. Any response to a HEAD request and any response with a 1xx
6342 * (Informational), 204 (No Content), or 304 (Not Modified) status
6343 * code is always terminated by the first empty line after the
6344 * header fields, regardless of the header fields present in the
6345 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006346 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006347 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6348 * the connection will become a tunnel immediately after the empty
6349 * line that concludes the header fields. A client MUST ignore any
6350 * Content-Length or Transfer-Encoding header fields received in
6351 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006352 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006353 * 3. If a Transfer-Encoding header field is present and the chunked
6354 * transfer coding (Section 4.1) is the final encoding, the message
6355 * body length is determined by reading and decoding the chunked
6356 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006357 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006358 * If a Transfer-Encoding header field is present in a response and
6359 * the chunked transfer coding is not the final encoding, the
6360 * message body length is determined by reading the connection until
6361 * it is closed by the server. If a Transfer-Encoding header field
6362 * is present in a request and the chunked transfer coding is not
6363 * the final encoding, the message body length cannot be determined
6364 * reliably; the server MUST respond with the 400 (Bad Request)
6365 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006366 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006367 * If a message is received with both a Transfer-Encoding and a
6368 * Content-Length header field, the Transfer-Encoding overrides the
6369 * Content-Length. Such a message might indicate an attempt to
6370 * perform request smuggling (Section 9.5) or response splitting
6371 * (Section 9.4) and ought to be handled as an error. A sender MUST
6372 * remove the received Content-Length field prior to forwarding such
6373 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006374 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006375 * 4. If a message is received without Transfer-Encoding and with
6376 * either multiple Content-Length header fields having differing
6377 * field-values or a single Content-Length header field having an
6378 * invalid value, then the message framing is invalid and the
6379 * recipient MUST treat it as an unrecoverable error. If this is a
6380 * request message, the server MUST respond with a 400 (Bad Request)
6381 * status code and then close the connection. If this is a response
6382 * message received by a proxy, the proxy MUST close the connection
6383 * to the server, discard the received response, and send a 502 (Bad
6384 * Gateway) response to the client. If this is a response message
6385 * received by a user agent, the user agent MUST close the
6386 * connection to the server and discard the received response.
6387 *
6388 * 5. If a valid Content-Length header field is present without
6389 * Transfer-Encoding, its decimal value defines the expected message
6390 * body length in octets. If the sender closes the connection or
6391 * the recipient times out before the indicated number of octets are
6392 * received, the recipient MUST consider the message to be
6393 * incomplete and close the connection.
6394 *
6395 * 6. If this is a request message and none of the above are true, then
6396 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006397 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006398 * 7. Otherwise, this is a response message without a declared message
6399 * body length, so the message body length is determined by the
6400 * number of octets received prior to the server closing the
6401 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006402 */
6403
6404 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006405 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006406 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006407 * FIXME: should we parse anyway and return an error on chunked encoding ?
6408 */
6409 if (txn->meth == HTTP_METH_HEAD ||
6410 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006411 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006412 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006413 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006414 goto skip_content_length;
6415 }
6416
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006417 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006418 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006419 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006420 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006421 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6422 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006423 /* bad transfer-encoding (chunked followed by something else) */
6424 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006425 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006426 break;
6427 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006428 }
6429
Willy Tarreau1c913912015-04-30 10:57:51 +02006430 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006431 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006432 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006433 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6434 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6435 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006436 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006437 signed long long cl;
6438
Willy Tarreauad14f752011-09-02 20:33:27 +02006439 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006440 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006441 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006442 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006443
Willy Tarreauad14f752011-09-02 20:33:27 +02006444 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006445 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006446 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006447 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006448
Willy Tarreauad14f752011-09-02 20:33:27 +02006449 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006450 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006451 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006452 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006453
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006454 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006455 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006456 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006457 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006458
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006459 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006460 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006461 }
6462
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006463 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006464 select_compression_response_header(s, rep->buf);
6465
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006466skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006467 /* Now we have to check if we need to modify the Connection header.
6468 * This is more difficult on the response than it is on the request,
6469 * because we can have two different HTTP versions and we don't know
6470 * how the client will interprete a response. For instance, let's say
6471 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6472 * HTTP/1.1 response without any header. Maybe it will bound itself to
6473 * HTTP/1.0 because it only knows about it, and will consider the lack
6474 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6475 * the lack of header as a keep-alive. Thus we will use two flags
6476 * indicating how a request MAY be understood by the client. In case
6477 * of multiple possibilities, we'll fix the header to be explicit. If
6478 * ambiguous cases such as both close and keepalive are seen, then we
6479 * will fall back to explicit close. Note that we won't take risks with
6480 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006481 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006482 */
6483
Willy Tarreaudc008c52010-02-01 16:20:08 +01006484 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6485 txn->status == 101)) {
6486 /* Either we've established an explicit tunnel, or we're
6487 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006488 * to understand the next protocols. We have to switch to tunnel
6489 * mode, so that we transfer the request and responses then let
6490 * this protocol pass unmodified. When we later implement specific
6491 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006492 * header which contains information about that protocol for
6493 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006494 */
6495 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6496 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006497 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6498 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006499 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006500 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006501 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006502
Willy Tarreau70dffda2014-01-30 03:07:23 +01006503 /* this situation happens when combining pretend-keepalive with httpclose. */
6504 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006505 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006506 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006507 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6508
Willy Tarreau60466522010-01-18 19:08:45 +01006509 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006510 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006511 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6512 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006513
Willy Tarreau60466522010-01-18 19:08:45 +01006514 /* now adjust header transformations depending on current state */
6515 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6516 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6517 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006518 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006519 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006520 }
Willy Tarreau60466522010-01-18 19:08:45 +01006521 else { /* SCL / KAL */
6522 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006523 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006524 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006525 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006526
Willy Tarreau60466522010-01-18 19:08:45 +01006527 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006528 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006529
Willy Tarreau60466522010-01-18 19:08:45 +01006530 /* Some keep-alive responses are converted to Server-close if
6531 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006532 */
Willy Tarreau60466522010-01-18 19:08:45 +01006533 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6534 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006535 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006536 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006537 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006538 }
6539
Willy Tarreau7959a552013-09-23 16:44:27 +02006540 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006541 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006542
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006543 /* end of job, return OK */
6544 rep->analysers &= ~an_bit;
6545 rep->analyse_exp = TICK_ETERNITY;
6546 channel_auto_close(rep);
6547 return 1;
6548
6549 abort_keep_alive:
6550 /* A keep-alive request to the server failed on a network error.
6551 * The client is required to retry. We need to close without returning
6552 * any other information so that the client retries.
6553 */
6554 txn->status = 0;
6555 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006556 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006557 channel_auto_close(rep);
6558 s->logs.logwait = 0;
6559 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006560 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006561 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006562 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006563 return 0;
6564}
6565
6566/* This function performs all the processing enabled for the current response.
6567 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006568 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006569 * other functions. It works like process_request (see indications above).
6570 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006571int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006572{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006573 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006574 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006575 struct http_msg *msg = &txn->rsp;
6576 struct proxy *cur_proxy;
6577 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006578 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006579
Willy Tarreau87b09662015-04-03 00:22:06 +02006580 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 +02006581 now_ms, __FUNCTION__,
6582 s,
6583 rep,
6584 rep->rex, rep->wex,
6585 rep->flags,
6586 rep->buf->i,
6587 rep->analysers);
6588
6589 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6590 return 0;
6591
Willy Tarreau70730dd2014-04-24 18:06:27 +02006592 /* The stats applet needs to adjust the Connection header but we don't
6593 * apply any filter there.
6594 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006595 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6596 rep->analysers &= ~an_bit;
6597 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006598 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006599 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006600
Willy Tarreau58975672014-04-24 21:13:57 +02006601 /*
6602 * We will have to evaluate the filters.
6603 * As opposed to version 1.2, now they will be evaluated in the
6604 * filters order and not in the header order. This means that
6605 * each filter has to be validated among all headers.
6606 *
6607 * Filters are tried with ->be first, then with ->fe if it is
6608 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006609 *
6610 * Maybe we are in resume condiion. In this case I choose the
6611 * "struct proxy" which contains the rule list matching the resume
6612 * pointer. If none of theses "struct proxy" match, I initialise
6613 * the process with the first one.
6614 *
6615 * In fact, I check only correspondance betwwen the current list
6616 * pointer and the ->fe rule list. If it doesn't match, I initialize
6617 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006618 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006619 if (s->current_rule_list == &sess->fe->http_res_rules)
6620 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006621 else
6622 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006623 while (1) {
6624 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006625
Willy Tarreau58975672014-04-24 21:13:57 +02006626 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006627 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006628 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006629
Willy Tarreau51d861a2015-05-22 17:30:48 +02006630 if (ret == HTTP_RULE_RES_BADREQ)
6631 goto return_srv_prx_502;
6632
6633 if (ret == HTTP_RULE_RES_DONE) {
6634 rep->analysers &= ~an_bit;
6635 rep->analyse_exp = TICK_ETERNITY;
6636 return 1;
6637 }
6638 }
6639
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006640 /* we need to be called again. */
6641 if (ret == HTTP_RULE_RES_YIELD) {
6642 channel_dont_close(rep);
6643 return 0;
6644 }
6645
Willy Tarreau58975672014-04-24 21:13:57 +02006646 /* try headers filters */
6647 if (rule_set->rsp_exp != NULL) {
6648 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6649 return_bad_resp:
6650 if (objt_server(s->target)) {
6651 objt_server(s->target)->counters.failed_resp++;
6652 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006653 }
Willy Tarreau58975672014-04-24 21:13:57 +02006654 s->be->be_counters.failed_resp++;
6655 return_srv_prx_502:
6656 rep->analysers = 0;
6657 txn->status = 502;
6658 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006659 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006660 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006661 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006662 if (!(s->flags & SF_ERR_MASK))
6663 s->flags |= SF_ERR_PRXCOND;
6664 if (!(s->flags & SF_FINST_MASK))
6665 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006666 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006667 }
Willy Tarreau58975672014-04-24 21:13:57 +02006668 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006669
Willy Tarreau58975672014-04-24 21:13:57 +02006670 /* has the response been denied ? */
6671 if (txn->flags & TX_SVDENY) {
6672 if (objt_server(s->target))
6673 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006674
Willy Tarreau58975672014-04-24 21:13:57 +02006675 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006676 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006677 if (sess->listener->counters)
6678 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006679
Willy Tarreau58975672014-04-24 21:13:57 +02006680 goto return_srv_prx_502;
6681 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006682
Willy Tarreau58975672014-04-24 21:13:57 +02006683 /* add response headers from the rule sets in the same order */
6684 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006685 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006686 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006687 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006688 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006689 ret = acl_pass(ret);
6690 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6691 ret = !ret;
6692 if (!ret)
6693 continue;
6694 }
6695 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6696 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006697 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006698
Willy Tarreau58975672014-04-24 21:13:57 +02006699 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006700 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006701 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006702 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006703 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006704
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006705 /* After this point, this anayzer can't return yield, so we can
6706 * remove the bit corresponding to this analyzer from the list.
6707 *
6708 * Note that the intermediate returns and goto found previously
6709 * reset the analyzers.
6710 */
6711 rep->analysers &= ~an_bit;
6712 rep->analyse_exp = TICK_ETERNITY;
6713
Willy Tarreau58975672014-04-24 21:13:57 +02006714 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006715 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006716 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006717
Willy Tarreau58975672014-04-24 21:13:57 +02006718 /*
6719 * Now check for a server cookie.
6720 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006721 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006722 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006723
Willy Tarreau58975672014-04-24 21:13:57 +02006724 /*
6725 * Check for cache-control or pragma headers if required.
6726 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006727 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 +02006728 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006729
Willy Tarreau58975672014-04-24 21:13:57 +02006730 /*
6731 * Add server cookie in the response if needed
6732 */
6733 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6734 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006735 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006736 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6737 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6738 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6739 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6740 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006741 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006742 /* the server is known, it's not the one the client requested, or the
6743 * cookie's last seen date needs to be refreshed. We have to
6744 * insert a set-cookie here, except if we want to insert only on POST
6745 * requests and this one isn't. Note that servers which don't have cookies
6746 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006747 */
Willy Tarreau58975672014-04-24 21:13:57 +02006748 if (!objt_server(s->target)->cookie) {
6749 chunk_printf(&trash,
6750 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6751 s->be->cookie_name);
6752 }
6753 else {
6754 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006755
Willy Tarreau58975672014-04-24 21:13:57 +02006756 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6757 /* emit last_date, which is mandatory */
6758 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6759 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6760 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006761
Willy Tarreau58975672014-04-24 21:13:57 +02006762 if (s->be->cookie_maxlife) {
6763 /* emit first_date, which is either the original one or
6764 * the current date.
6765 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006766 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006767 s30tob64(txn->cookie_first_date ?
6768 txn->cookie_first_date >> 2 :
6769 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006770 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006771 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006772 }
Willy Tarreau58975672014-04-24 21:13:57 +02006773 chunk_appendf(&trash, "; path=/");
6774 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006775
Willy Tarreau58975672014-04-24 21:13:57 +02006776 if (s->be->cookie_domain)
6777 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006778
Willy Tarreau58975672014-04-24 21:13:57 +02006779 if (s->be->ck_opts & PR_CK_HTTPONLY)
6780 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006781
Willy Tarreau58975672014-04-24 21:13:57 +02006782 if (s->be->ck_opts & PR_CK_SECURE)
6783 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006784
Willy Tarreau58975672014-04-24 21:13:57 +02006785 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6786 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006787
Willy Tarreau58975672014-04-24 21:13:57 +02006788 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006789 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006790 /* the server did not change, only the date was updated */
6791 txn->flags |= TX_SCK_UPDATED;
6792 else
6793 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006794
Willy Tarreau58975672014-04-24 21:13:57 +02006795 /* Here, we will tell an eventual cache on the client side that we don't
6796 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6797 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6798 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006799 */
Willy Tarreau58975672014-04-24 21:13:57 +02006800 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006801
Willy Tarreau58975672014-04-24 21:13:57 +02006802 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006803
Willy Tarreau58975672014-04-24 21:13:57 +02006804 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6805 "Cache-control: private", 22) < 0))
6806 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006807 }
Willy Tarreau58975672014-04-24 21:13:57 +02006808 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006809
Willy Tarreau58975672014-04-24 21:13:57 +02006810 /*
6811 * Check if result will be cacheable with a cookie.
6812 * We'll block the response if security checks have caught
6813 * nasty things such as a cacheable cookie.
6814 */
6815 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6816 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6817 (s->be->options & PR_O_CHK_CACHE)) {
6818 /* we're in presence of a cacheable response containing
6819 * a set-cookie header. We'll block it as requested by
6820 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006821 */
Willy Tarreau58975672014-04-24 21:13:57 +02006822 if (objt_server(s->target))
6823 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006824
Willy Tarreau58975672014-04-24 21:13:57 +02006825 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006826 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006827 if (sess->listener->counters)
6828 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006829
Willy Tarreau58975672014-04-24 21:13:57 +02006830 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6831 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6832 send_log(s->be, LOG_ALERT,
6833 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6834 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6835 goto return_srv_prx_502;
6836 }
Willy Tarreau03945942009-12-22 16:50:27 +01006837
Willy Tarreau70730dd2014-04-24 18:06:27 +02006838 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006839 /*
6840 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6841 * If an "Upgrade" token is found, the header is left untouched in order
6842 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006843 * if anything but "Upgrade" is present in the Connection header. We don't
6844 * want to touch any 101 response either since it's switching to another
6845 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006846 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006847 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006848 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006849 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006850 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6851 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006852
Willy Tarreau58975672014-04-24 21:13:57 +02006853 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6854 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6855 /* we want a keep-alive response here. Keep-alive header
6856 * required if either side is not 1.1.
6857 */
6858 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6859 want_flags |= TX_CON_KAL_SET;
6860 }
6861 else {
6862 /* we want a close response here. Close header required if
6863 * the server is 1.1, regardless of the client.
6864 */
6865 if (msg->flags & HTTP_MSGF_VER_11)
6866 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006867 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006868
Willy Tarreau58975672014-04-24 21:13:57 +02006869 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6870 http_change_connection_header(txn, msg, want_flags);
6871 }
6872
6873 skip_header_mangling:
6874 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6875 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6876 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006877
Willy Tarreau58975672014-04-24 21:13:57 +02006878 /* if the user wants to log as soon as possible, without counting
6879 * bytes from the server, then this is the right moment. We have
6880 * to temporarily assign bytes_out to log what we currently have.
6881 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006882 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006883 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6884 s->logs.bytes_out = txn->rsp.eoh;
6885 s->do_log(s);
6886 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006887 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006888 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006889}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006890
Willy Tarreaud98cf932009-12-27 22:54:55 +01006891/* This function is an analyser which forwards response body (including chunk
6892 * sizes if any). It is called as soon as we must forward, even if we forward
6893 * zero byte. The only situation where it must not be called is when we're in
6894 * tunnel mode and we want to forward till the close. It's used both to forward
6895 * remaining data and to resync after end of body. It expects the msg_state to
6896 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006897 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006898 *
6899 * It is capable of compressing response data both in content-length mode and
6900 * in chunked mode. The state machines follows different flows depending on
6901 * whether content-length and chunked modes are used, since there are no
6902 * trailers in content-length :
6903 *
6904 * chk-mode cl-mode
6905 * ,----- BODY -----.
6906 * / \
6907 * V size > 0 V chk-mode
6908 * .--> SIZE -------------> DATA -------------> CRLF
6909 * | | size == 0 | last byte |
6910 * | v final crlf v inspected |
6911 * | TRAILERS -----------> DONE |
6912 * | |
6913 * `----------------------------------------------'
6914 *
6915 * Compression only happens in the DATA state, and must be flushed in final
6916 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6917 * is performed at once on final states for all bytes parsed, or when leaving
6918 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006919 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006920int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006921{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006922 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006923 struct http_txn *txn = s->txn;
6924 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006925 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006926 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006927 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006928
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006929 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6930 return 0;
6931
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006932 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006933 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006934 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006935 /* Output closed while we were sending data. We must abort and
6936 * wake the other side up.
6937 */
6938 msg->msg_state = HTTP_MSG_ERROR;
6939 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006940 return 1;
6941 }
6942
Willy Tarreau4fe41902010-06-07 22:27:41 +02006943 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006944 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006945
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006946 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006947 /* we have msg->sov which points to the first byte of message
6948 * body, and res->buf.p still points to the beginning of the
6949 * message. We forward the headers now, as we don't need them
6950 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006951 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006952 b_adv(res->buf, msg->sov);
6953 msg->next -= msg->sov;
6954 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006955
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006956 /* The previous analysers guarantee that the state is somewhere
6957 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6958 * msg->next are always correct.
6959 */
6960 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6961 if (msg->flags & HTTP_MSGF_TE_CHNK)
6962 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6963 else
6964 msg->msg_state = HTTP_MSG_DATA;
6965 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006966 }
6967
Willy Tarreauefdf0942014-04-24 20:08:57 +02006968 if (res->to_forward) {
6969 /* We can't process the buffer's contents yet */
6970 res->flags |= CF_WAKE_WRITE;
6971 goto missing_data;
6972 }
6973
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006974 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6975 /* We need a compression buffer in the DATA state to put the
6976 * output of compressed data, and in CRLF state to let the
6977 * TRAILERS state finish the job of removing the trailing CRLF.
6978 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006979 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006980 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006981 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006982 goto aborted_xfer; /* no memory */
6983 }
6984
6985 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006986 if (ret < 0) {
6987 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006988 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006989 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006990 compressing = 1;
6991 }
6992
Willy Tarreaud98cf932009-12-27 22:54:55 +01006993 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006994 switch (msg->msg_state - HTTP_MSG_DATA) {
6995 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006996 /* we may have some pending data starting at res->buf->p */
6997 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006998 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006999 if (ret < 0)
7000 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02007001
Willy Tarreaud5a67832014-04-21 10:54:27 +02007002 if (msg->chunk_len) {
7003 /* input empty or output full */
7004 if (res->buf->i > msg->next)
7005 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02007006 goto missing_data;
7007 }
William Lallemandbf3ae612012-11-19 12:35:37 +01007008 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007009 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02007010 if (msg->chunk_len > res->buf->i - msg->next) {
7011 /* output full */
7012 res->flags |= CF_WAKE_WRITE;
7013 goto missing_data;
7014 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007015 msg->next += msg->chunk_len;
7016 msg->chunk_len = 0;
7017 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01007018
7019 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01007020 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02007021 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01007022 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01007023 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007024 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01007025 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007026 /* fall through for HTTP_MSG_CHUNK_CRLF */
7027
7028 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
7029 /* we want the CRLF after the data */
7030
7031 ret = http_skip_chunk_crlf(msg);
7032 if (ret == 0)
7033 goto missing_data;
7034 else if (ret < 0) {
7035 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007036 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007037 goto return_bad_res;
7038 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007039 /* we're in MSG_CHUNK_SIZE now, fall through */
7040
7041 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01007042 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02007043 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01007044 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01007045 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007046
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007047 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02007048 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01007049 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007050 else if (ret < 0) {
7051 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007052 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007053 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007054 }
Willy Tarreau0161d622013-04-02 01:26:55 +02007055 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007056 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01007057
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007058 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02007059 if (unlikely(compressing)) {
7060 /* we need to flush output contents before syncing FSMs */
7061 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7062 compressing = 0;
7063 }
7064
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007065 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007066 if (ret == 0)
7067 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007068 else if (ret < 0) {
7069 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007070 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007071 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007072 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02007073 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007074
7075 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01007076 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007077 if (unlikely(compressing)) {
7078 /* we need to flush output contents before syncing FSMs */
7079 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7080 compressing = 0;
7081 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007082
Willy Tarreauc623c172014-04-18 09:53:50 +02007083 /* we may have some pending data starting at res->buf->p
7084 * such as a last chunk of data or trailers.
7085 */
7086 b_adv(res->buf, msg->next);
7087 msg->next = 0;
7088
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007089 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02007090 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007091 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7092 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007093 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02007094
Willy Tarreau610ecce2010-01-04 21:15:02 +01007095 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01007096 /* some state changes occurred, maybe the analyser
7097 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01007098 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007099 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007100 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007101 /* response errors are most likely due to
7102 * the client aborting the transfer.
7103 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007104 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007105 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007106 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007107 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01007108 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007109 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007110 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01007111 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007112 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007113 }
7114 }
7115
Willy Tarreaud98cf932009-12-27 22:54:55 +01007116 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02007117 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007118 if (unlikely(compressing)) {
7119 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02007120 compressing = 0;
7121 }
Willy Tarreauf003d372012-11-26 13:35:37 +01007122
Willy Tarreauc623c172014-04-18 09:53:50 +02007123 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
7124 b_adv(res->buf, msg->next);
7125 msg->next = 0;
7126 msg->chunk_len -= channel_forward(res, msg->chunk_len);
7127 }
7128
Willy Tarreauf003d372012-11-26 13:35:37 +01007129 if (res->flags & CF_SHUTW)
7130 goto aborted_xfer;
7131
7132 /* stop waiting for data if the input is closed before the end. If the
7133 * client side was already closed, it means that the client has aborted,
7134 * so we don't want to count this as a server abort. Otherwise it's a
7135 * server abort.
7136 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007137 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007138 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01007139 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007140 if (!(s->flags & SF_ERR_MASK))
7141 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007142 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007143 if (objt_server(s->target))
7144 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007145 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01007146 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01007147
Willy Tarreau40dba092010-03-04 18:14:51 +01007148 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007149 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01007150 goto return_bad_res;
7151
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007152 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007153 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007154 * Similarly, with keep-alive on the client side, we don't want to forward a
7155 * close.
7156 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007157 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007158 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7159 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007160 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007161
Willy Tarreau5c620922011-05-11 19:56:11 +02007162 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007163 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007164 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007165 * modes are already handled by the stream sock layer. We must not do
7166 * this in content-length mode because it could present the MSG_MORE
7167 * flag with the last block of forwarded data, which would cause an
7168 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007169 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007170 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007171 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007172
Willy Tarreau87b09662015-04-03 00:22:06 +02007173 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007174 return 0;
7175
Willy Tarreau40dba092010-03-04 18:14:51 +01007176 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007177 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007178 if (objt_server(s->target))
7179 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007180
7181 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02007182 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02007183 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02007184 compressing = 0;
7185 }
7186
Willy Tarreauc623c172014-04-18 09:53:50 +02007187 /* we may have some pending data starting at res->buf->p */
7188 if (s->comp_algo == NULL) {
7189 b_adv(res->buf, msg->next);
7190 msg->next = 0;
7191 }
7192
Willy Tarreaud98cf932009-12-27 22:54:55 +01007193 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007194 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007195 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007196 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007197 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007198 if (objt_server(s->target))
7199 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007200
Willy Tarreaue7dff022015-04-03 01:14:29 +02007201 if (!(s->flags & SF_ERR_MASK))
7202 s->flags |= SF_ERR_PRXCOND;
7203 if (!(s->flags & SF_FINST_MASK))
7204 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007205 return 0;
7206
7207 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02007208 if (unlikely(compressing)) {
7209 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
7210 compressing = 0;
7211 }
7212
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007213 txn->rsp.msg_state = HTTP_MSG_ERROR;
7214 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007215 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007216 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007217 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007218
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007219 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007220 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007221 if (objt_server(s->target))
7222 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007223
Willy Tarreaue7dff022015-04-03 01:14:29 +02007224 if (!(s->flags & SF_ERR_MASK))
7225 s->flags |= SF_ERR_CLICL;
7226 if (!(s->flags & SF_FINST_MASK))
7227 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007228 return 0;
7229}
7230
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007231/* Iterate the same filter through all request headers.
7232 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007233 * Since it can manage the switch to another backend, it updates the per-proxy
7234 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007235 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007236int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007237{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007238 char *cur_ptr, *cur_end, *cur_next;
7239 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007240 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007241 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007242 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007243
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007244 last_hdr = 0;
7245
Willy Tarreau9b28e032012-10-12 23:49:43 +02007246 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007247 old_idx = 0;
7248
7249 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007250 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007251 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007252 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007253 (exp->action == ACT_ALLOW ||
7254 exp->action == ACT_DENY ||
7255 exp->action == ACT_TARPIT))
7256 return 0;
7257
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007258 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007259 if (!cur_idx)
7260 break;
7261
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007262 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007263 cur_ptr = cur_next;
7264 cur_end = cur_ptr + cur_hdr->len;
7265 cur_next = cur_end + cur_hdr->cr + 1;
7266
7267 /* Now we have one header between cur_ptr and cur_end,
7268 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007269 */
7270
Willy Tarreau15a53a42015-01-21 13:39:42 +01007271 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007272 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007273 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007274 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007275 last_hdr = 1;
7276 break;
7277
7278 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007279 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007280 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007281 break;
7282
7283 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007284 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007285 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007286 break;
7287
7288 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007289 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7290 if (trash.len < 0)
7291 return -1;
7292
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007293 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007294 /* FIXME: if the user adds a newline in the replacement, the
7295 * index will not be recalculated for now, and the new line
7296 * will not be counted as a new header.
7297 */
7298
7299 cur_end += delta;
7300 cur_next += delta;
7301 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007302 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007303 break;
7304
7305 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007306 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007307 cur_next += delta;
7308
Willy Tarreaufa355d42009-11-29 18:12:29 +01007309 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007310 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7311 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007312 cur_hdr->len = 0;
7313 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007314 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007315 break;
7316
7317 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007318 }
7319
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007320 /* keep the link from this header to next one in case of later
7321 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007322 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007323 old_idx = cur_idx;
7324 }
7325 return 0;
7326}
7327
7328
7329/* Apply the filter to the request line.
7330 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7331 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007332 * Since it can manage the switch to another backend, it updates the per-proxy
7333 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007334 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007335int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007336{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007337 char *cur_ptr, *cur_end;
7338 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007339 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007340 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007341
Willy Tarreau3d300592007-03-18 18:34:41 +01007342 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007343 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007344 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007345 (exp->action == ACT_ALLOW ||
7346 exp->action == ACT_DENY ||
7347 exp->action == ACT_TARPIT))
7348 return 0;
7349 else if (exp->action == ACT_REMOVE)
7350 return 0;
7351
7352 done = 0;
7353
Willy Tarreau9b28e032012-10-12 23:49:43 +02007354 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007355 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007356
7357 /* Now we have the request line between cur_ptr and cur_end */
7358
Willy Tarreau15a53a42015-01-21 13:39:42 +01007359 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007360 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007361 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007362 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007363 done = 1;
7364 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007365
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007366 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007367 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007368 done = 1;
7369 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007370
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007371 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007372 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007373 done = 1;
7374 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007375
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007376 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007377 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7378 if (trash.len < 0)
7379 return -1;
7380
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007381 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007382 /* FIXME: if the user adds a newline in the replacement, the
7383 * index will not be recalculated for now, and the new line
7384 * will not be counted as a new header.
7385 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007386
Willy Tarreaufa355d42009-11-29 18:12:29 +01007387 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007388 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007389 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007390 HTTP_MSG_RQMETH,
7391 cur_ptr, cur_end + 1,
7392 NULL, NULL);
7393 if (unlikely(!cur_end))
7394 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007395
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007396 /* we have a full request and we know that we have either a CR
7397 * or an LF at <ptr>.
7398 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007399 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7400 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007401 /* there is no point trying this regex on headers */
7402 return 1;
7403 }
7404 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007405 return done;
7406}
Willy Tarreau97de6242006-12-27 17:18:38 +01007407
Willy Tarreau58f10d72006-12-04 02:26:12 +01007408
Willy Tarreau58f10d72006-12-04 02:26:12 +01007409
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007410/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007411 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007412 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007413 * unparsable request. Since it can manage the switch to another backend, it
7414 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007415 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007416int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007417{
Willy Tarreau192252e2015-04-04 01:47:55 +02007418 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007419 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007420 struct hdr_exp *exp;
7421
7422 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007423 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007424
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007425 /*
7426 * The interleaving of transformations and verdicts
7427 * makes it difficult to decide to continue or stop
7428 * the evaluation.
7429 */
7430
Willy Tarreau6c123b12010-01-28 20:22:06 +01007431 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7432 break;
7433
Willy Tarreau3d300592007-03-18 18:34:41 +01007434 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007435 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007436 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007437 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007438
7439 /* if this filter had a condition, evaluate it now and skip to
7440 * next filter if the condition does not match.
7441 */
7442 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007443 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007444 ret = acl_pass(ret);
7445 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7446 ret = !ret;
7447
7448 if (!ret)
7449 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007450 }
7451
7452 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007453 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007454 if (unlikely(ret < 0))
7455 return -1;
7456
7457 if (likely(ret == 0)) {
7458 /* The filter did not match the request, it can be
7459 * iterated through all headers.
7460 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007461 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7462 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007463 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007464 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007465 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007466}
7467
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007468
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007469/* Find the end of a cookie value contained between <s> and <e>. It works the
7470 * same way as with headers above except that the semi-colon also ends a token.
7471 * See RFC2965 for more information. Note that it requires a valid header to
7472 * return a valid result.
7473 */
7474char *find_cookie_value_end(char *s, const char *e)
7475{
7476 int quoted, qdpair;
7477
7478 quoted = qdpair = 0;
7479 for (; s < e; s++) {
7480 if (qdpair) qdpair = 0;
7481 else if (quoted) {
7482 if (*s == '\\') qdpair = 1;
7483 else if (*s == '"') quoted = 0;
7484 }
7485 else if (*s == '"') quoted = 1;
7486 else if (*s == ',' || *s == ';') return s;
7487 }
7488 return s;
7489}
7490
7491/* Delete a value in a header between delimiters <from> and <next> in buffer
7492 * <buf>. The number of characters displaced is returned, and the pointer to
7493 * the first delimiter is updated if required. The function tries as much as
7494 * possible to respect the following principles :
7495 * - replace <from> delimiter by the <next> one unless <from> points to a
7496 * colon, in which case <next> is simply removed
7497 * - set exactly one space character after the new first delimiter, unless
7498 * there are not enough characters in the block being moved to do so.
7499 * - remove unneeded spaces before the previous delimiter and after the new
7500 * one.
7501 *
7502 * It is the caller's responsibility to ensure that :
7503 * - <from> points to a valid delimiter or the colon ;
7504 * - <next> points to a valid delimiter or the final CR/LF ;
7505 * - there are non-space chars before <from> ;
7506 * - there is a CR/LF at or after <next>.
7507 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007508int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007509{
7510 char *prev = *from;
7511
7512 if (*prev == ':') {
7513 /* We're removing the first value, preserve the colon and add a
7514 * space if possible.
7515 */
7516 if (!http_is_crlf[(unsigned char)*next])
7517 next++;
7518 prev++;
7519 if (prev < next)
7520 *prev++ = ' ';
7521
7522 while (http_is_spht[(unsigned char)*next])
7523 next++;
7524 } else {
7525 /* Remove useless spaces before the old delimiter. */
7526 while (http_is_spht[(unsigned char)*(prev-1)])
7527 prev--;
7528 *from = prev;
7529
7530 /* copy the delimiter and if possible a space if we're
7531 * not at the end of the line.
7532 */
7533 if (!http_is_crlf[(unsigned char)*next]) {
7534 *prev++ = *next++;
7535 if (prev + 1 < next)
7536 *prev++ = ' ';
7537 while (http_is_spht[(unsigned char)*next])
7538 next++;
7539 }
7540 }
7541 return buffer_replace2(buf, prev, next, NULL, 0);
7542}
7543
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007544/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007545 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007546 * desirable to call it only when needed. This code is quite complex because
7547 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7548 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007549 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007550void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007551{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007552 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007553 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007554 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007555 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007556 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7557 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007558
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007559 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007560 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007561 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007562
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007563 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007564 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007565 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007566
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007567 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007568 hdr_beg = hdr_next;
7569 hdr_end = hdr_beg + cur_hdr->len;
7570 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007571
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007572 /* We have one full header between hdr_beg and hdr_end, and the
7573 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007574 * "Cookie:" headers.
7575 */
7576
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007577 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007578 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007579 old_idx = cur_idx;
7580 continue;
7581 }
7582
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007583 del_from = NULL; /* nothing to be deleted */
7584 preserve_hdr = 0; /* assume we may kill the whole header */
7585
Willy Tarreau58f10d72006-12-04 02:26:12 +01007586 /* Now look for cookies. Conforming to RFC2109, we have to support
7587 * attributes whose name begin with a '$', and associate them with
7588 * the right cookie, if we want to delete this cookie.
7589 * So there are 3 cases for each cookie read :
7590 * 1) it's a special attribute, beginning with a '$' : ignore it.
7591 * 2) it's a server id cookie that we *MAY* want to delete : save
7592 * some pointers on it (last semi-colon, beginning of cookie...)
7593 * 3) it's an application cookie : we *MAY* have to delete a previous
7594 * "special" cookie.
7595 * At the end of loop, if a "special" cookie remains, we may have to
7596 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007597 * *MUST* delete it.
7598 *
7599 * Note: RFC2965 is unclear about the processing of spaces around
7600 * the equal sign in the ATTR=VALUE form. A careful inspection of
7601 * the RFC explicitly allows spaces before it, and not within the
7602 * tokens (attrs or values). An inspection of RFC2109 allows that
7603 * too but section 10.1.3 lets one think that spaces may be allowed
7604 * after the equal sign too, resulting in some (rare) buggy
7605 * implementations trying to do that. So let's do what servers do.
7606 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7607 * allowed quoted strings in values, with any possible character
7608 * after a backslash, including control chars and delimitors, which
7609 * causes parsing to become ambiguous. Browsers also allow spaces
7610 * within values even without quotes.
7611 *
7612 * We have to keep multiple pointers in order to support cookie
7613 * removal at the beginning, middle or end of header without
7614 * corrupting the header. All of these headers are valid :
7615 *
7616 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7617 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7618 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7619 * | | | | | | | | |
7620 * | | | | | | | | hdr_end <--+
7621 * | | | | | | | +--> next
7622 * | | | | | | +----> val_end
7623 * | | | | | +-----------> val_beg
7624 * | | | | +--------------> equal
7625 * | | | +----------------> att_end
7626 * | | +---------------------> att_beg
7627 * | +--------------------------> prev
7628 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007629 */
7630
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007631 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7632 /* Iterate through all cookies on this line */
7633
7634 /* find att_beg */
7635 att_beg = prev + 1;
7636 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7637 att_beg++;
7638
7639 /* find att_end : this is the first character after the last non
7640 * space before the equal. It may be equal to hdr_end.
7641 */
7642 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007643
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007644 while (equal < hdr_end) {
7645 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007646 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007647 if (http_is_spht[(unsigned char)*equal++])
7648 continue;
7649 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007650 }
7651
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007652 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7653 * is between <att_beg> and <equal>, both may be identical.
7654 */
7655
7656 /* look for end of cookie if there is an equal sign */
7657 if (equal < hdr_end && *equal == '=') {
7658 /* look for the beginning of the value */
7659 val_beg = equal + 1;
7660 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7661 val_beg++;
7662
7663 /* find the end of the value, respecting quotes */
7664 next = find_cookie_value_end(val_beg, hdr_end);
7665
7666 /* make val_end point to the first white space or delimitor after the value */
7667 val_end = next;
7668 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7669 val_end--;
7670 } else {
7671 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007672 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007673
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007674 /* We have nothing to do with attributes beginning with '$'. However,
7675 * they will automatically be removed if a header before them is removed,
7676 * since they're supposed to be linked together.
7677 */
7678 if (*att_beg == '$')
7679 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007680
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007681 /* Ignore cookies with no equal sign */
7682 if (equal == next) {
7683 /* This is not our cookie, so we must preserve it. But if we already
7684 * scheduled another cookie for removal, we cannot remove the
7685 * complete header, but we can remove the previous block itself.
7686 */
7687 preserve_hdr = 1;
7688 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007689 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007690 val_end += delta;
7691 next += delta;
7692 hdr_end += delta;
7693 hdr_next += delta;
7694 cur_hdr->len += delta;
7695 http_msg_move_end(&txn->req, delta);
7696 prev = del_from;
7697 del_from = NULL;
7698 }
7699 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007700 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007701
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007702 /* if there are spaces around the equal sign, we need to
7703 * strip them otherwise we'll get trouble for cookie captures,
7704 * or even for rewrites. Since this happens extremely rarely,
7705 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007706 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007707 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7708 int stripped_before = 0;
7709 int stripped_after = 0;
7710
7711 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007712 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007713 equal += stripped_before;
7714 val_beg += stripped_before;
7715 }
7716
7717 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007718 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007719 val_beg += stripped_after;
7720 stripped_before += stripped_after;
7721 }
7722
7723 val_end += stripped_before;
7724 next += stripped_before;
7725 hdr_end += stripped_before;
7726 hdr_next += stripped_before;
7727 cur_hdr->len += stripped_before;
7728 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007729 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007730 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007731
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007732 /* First, let's see if we want to capture this cookie. We check
7733 * that we don't already have a client side cookie, because we
7734 * can only capture one. Also as an optimisation, we ignore
7735 * cookies shorter than the declared name.
7736 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007737 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7738 (val_end - att_beg >= sess->fe->capture_namelen) &&
7739 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007740 int log_len = val_end - att_beg;
7741
7742 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7743 Alert("HTTP logging : out of memory.\n");
7744 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007745 if (log_len > sess->fe->capture_len)
7746 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007747 memcpy(txn->cli_cookie, att_beg, log_len);
7748 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007749 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007750 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007751
Willy Tarreaubca99692010-10-06 19:25:55 +02007752 /* Persistence cookies in passive, rewrite or insert mode have the
7753 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007754 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007755 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007756 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007757 * For cookies in prefix mode, the form is :
7758 *
7759 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007760 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007761 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7762 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7763 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007764 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007765
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007766 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7767 * have the server ID between val_beg and delim, and the original cookie between
7768 * delim+1 and val_end. Otherwise, delim==val_end :
7769 *
7770 * Cookie: NAME=SRV; # in all but prefix modes
7771 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7772 * | || || | |+-> next
7773 * | || || | +--> val_end
7774 * | || || +---------> delim
7775 * | || |+------------> val_beg
7776 * | || +-------------> att_end = equal
7777 * | |+-----------------> att_beg
7778 * | +------------------> prev
7779 * +-------------------------> hdr_beg
7780 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007781
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007782 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007783 for (delim = val_beg; delim < val_end; delim++)
7784 if (*delim == COOKIE_DELIM)
7785 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007786 } else {
7787 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007788 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007789 /* Now check if the cookie contains a date field, which would
7790 * appear after a vertical bar ('|') just after the server name
7791 * and before the delimiter.
7792 */
7793 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7794 if (vbar1) {
7795 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007796 * right is the last seen date. It is a base64 encoded
7797 * 30-bit value representing the UNIX date since the
7798 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007799 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007800 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007801 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007802 if (val_end - vbar1 >= 5) {
7803 val = b64tos30(vbar1);
7804 if (val > 0)
7805 txn->cookie_last_date = val << 2;
7806 }
7807 /* look for a second vertical bar */
7808 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7809 if (vbar1 && (val_end - vbar1 > 5)) {
7810 val = b64tos30(vbar1 + 1);
7811 if (val > 0)
7812 txn->cookie_first_date = val << 2;
7813 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007814 }
7815 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007816
Willy Tarreauf64d1412010-10-07 20:06:11 +02007817 /* if the cookie has an expiration date and the proxy wants to check
7818 * it, then we do that now. We first check if the cookie is too old,
7819 * then only if it has expired. We detect strict overflow because the
7820 * time resolution here is not great (4 seconds). Cookies with dates
7821 * in the future are ignored if their offset is beyond one day. This
7822 * allows an admin to fix timezone issues without expiring everyone
7823 * and at the same time avoids keeping unwanted side effects for too
7824 * long.
7825 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007826 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7827 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007828 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007829 txn->flags &= ~TX_CK_MASK;
7830 txn->flags |= TX_CK_OLD;
7831 delim = val_beg; // let's pretend we have not found the cookie
7832 txn->cookie_first_date = 0;
7833 txn->cookie_last_date = 0;
7834 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007835 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7836 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007837 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007838 txn->flags &= ~TX_CK_MASK;
7839 txn->flags |= TX_CK_EXPIRED;
7840 delim = val_beg; // let's pretend we have not found the cookie
7841 txn->cookie_first_date = 0;
7842 txn->cookie_last_date = 0;
7843 }
7844
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007845 /* Here, we'll look for the first running server which supports the cookie.
7846 * This allows to share a same cookie between several servers, for example
7847 * to dedicate backup servers to specific servers only.
7848 * However, to prevent clients from sticking to cookie-less backup server
7849 * when they have incidentely learned an empty cookie, we simply ignore
7850 * empty cookies and mark them as invalid.
7851 * The same behaviour is applied when persistence must be ignored.
7852 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007853 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007854 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007855
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007856 while (srv) {
7857 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7858 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007859 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007860 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007861 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007862 /* we found the server and we can use it */
7863 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007864 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007865 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007866 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007867 break;
7868 } else {
7869 /* we found a server, but it's down,
7870 * mark it as such and go on in case
7871 * another one is available.
7872 */
7873 txn->flags &= ~TX_CK_MASK;
7874 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007875 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007876 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007877 srv = srv->next;
7878 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007879
Willy Tarreauf64d1412010-10-07 20:06:11 +02007880 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007881 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007882 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007883 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007884 txn->flags |= TX_CK_UNUSED;
7885 else
7886 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007887 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007888
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007889 /* depending on the cookie mode, we may have to either :
7890 * - delete the complete cookie if we're in insert+indirect mode, so that
7891 * the server never sees it ;
7892 * - remove the server id from the cookie value, and tag the cookie as an
7893 * application cookie so that it does not get accidentely removed later,
7894 * if we're in cookie prefix mode
7895 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007896 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007897 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007898
Willy Tarreau9b28e032012-10-12 23:49:43 +02007899 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007900 val_end += delta;
7901 next += delta;
7902 hdr_end += delta;
7903 hdr_next += delta;
7904 cur_hdr->len += delta;
7905 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007906
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007907 del_from = NULL;
7908 preserve_hdr = 1; /* we want to keep this cookie */
7909 }
7910 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007911 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007912 del_from = prev;
7913 }
7914 } else {
7915 /* This is not our cookie, so we must preserve it. But if we already
7916 * scheduled another cookie for removal, we cannot remove the
7917 * complete header, but we can remove the previous block itself.
7918 */
7919 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007920
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007921 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007922 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007923 if (att_beg >= del_from)
7924 att_beg += delta;
7925 if (att_end >= del_from)
7926 att_end += delta;
7927 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007928 val_end += delta;
7929 next += delta;
7930 hdr_end += delta;
7931 hdr_next += delta;
7932 cur_hdr->len += delta;
7933 http_msg_move_end(&txn->req, delta);
7934 prev = del_from;
7935 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007936 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007937 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007938
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007939 /* continue with next cookie on this header line */
7940 att_beg = next;
7941 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007942
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007943 /* There are no more cookies on this line.
7944 * We may still have one (or several) marked for deletion at the
7945 * end of the line. We must do this now in two ways :
7946 * - if some cookies must be preserved, we only delete from the
7947 * mark to the end of line ;
7948 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007949 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007950 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007951 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007952 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007953 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007954 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007955 cur_hdr->len += delta;
7956 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007957 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007958
7959 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007960 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7961 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007962 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007963 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007964 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007965 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007966 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007967 }
7968
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007969 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007970 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007971 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007972}
7973
7974
Willy Tarreaua15645d2007-03-18 16:22:39 +01007975/* Iterate the same filter through all response headers contained in <rtr>.
7976 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7977 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007978int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007979{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007980 char *cur_ptr, *cur_end, *cur_next;
7981 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007982 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007983 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007984 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007985
7986 last_hdr = 0;
7987
Willy Tarreau9b28e032012-10-12 23:49:43 +02007988 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007989 old_idx = 0;
7990
7991 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007992 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007993 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007994 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007995 (exp->action == ACT_ALLOW ||
7996 exp->action == ACT_DENY))
7997 return 0;
7998
7999 cur_idx = txn->hdr_idx.v[old_idx].next;
8000 if (!cur_idx)
8001 break;
8002
8003 cur_hdr = &txn->hdr_idx.v[cur_idx];
8004 cur_ptr = cur_next;
8005 cur_end = cur_ptr + cur_hdr->len;
8006 cur_next = cur_end + cur_hdr->cr + 1;
8007
8008 /* Now we have one header between cur_ptr and cur_end,
8009 * and the next header starts at cur_next.
8010 */
8011
Willy Tarreau15a53a42015-01-21 13:39:42 +01008012 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008013 switch (exp->action) {
8014 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008015 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008016 last_hdr = 1;
8017 break;
8018
8019 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008020 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008021 last_hdr = 1;
8022 break;
8023
8024 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008025 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8026 if (trash.len < 0)
8027 return -1;
8028
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008029 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008030 /* FIXME: if the user adds a newline in the replacement, the
8031 * index will not be recalculated for now, and the new line
8032 * will not be counted as a new header.
8033 */
8034
8035 cur_end += delta;
8036 cur_next += delta;
8037 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008038 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008039 break;
8040
8041 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008042 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008043 cur_next += delta;
8044
Willy Tarreaufa355d42009-11-29 18:12:29 +01008045 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008046 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8047 txn->hdr_idx.used--;
8048 cur_hdr->len = 0;
8049 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008050 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008051 break;
8052
8053 }
8054 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008055
8056 /* keep the link from this header to next one in case of later
8057 * removal of next header.
8058 */
8059 old_idx = cur_idx;
8060 }
8061 return 0;
8062}
8063
8064
8065/* Apply the filter to the status line in the response buffer <rtr>.
8066 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8067 * or -1 if a replacement resulted in an invalid status line.
8068 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008069int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008070{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008071 char *cur_ptr, *cur_end;
8072 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008073 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008074 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008075
8076
Willy Tarreau3d300592007-03-18 18:34:41 +01008077 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008078 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008079 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008080 (exp->action == ACT_ALLOW ||
8081 exp->action == ACT_DENY))
8082 return 0;
8083 else if (exp->action == ACT_REMOVE)
8084 return 0;
8085
8086 done = 0;
8087
Willy Tarreau9b28e032012-10-12 23:49:43 +02008088 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008089 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008090
8091 /* Now we have the status line between cur_ptr and cur_end */
8092
Willy Tarreau15a53a42015-01-21 13:39:42 +01008093 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008094 switch (exp->action) {
8095 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008096 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008097 done = 1;
8098 break;
8099
8100 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008101 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008102 done = 1;
8103 break;
8104
8105 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008106 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8107 if (trash.len < 0)
8108 return -1;
8109
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008110 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008111 /* FIXME: if the user adds a newline in the replacement, the
8112 * index will not be recalculated for now, and the new line
8113 * will not be counted as a new header.
8114 */
8115
Willy Tarreaufa355d42009-11-29 18:12:29 +01008116 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008117 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008118 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008119 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008120 cur_ptr, cur_end + 1,
8121 NULL, NULL);
8122 if (unlikely(!cur_end))
8123 return -1;
8124
8125 /* we have a full respnse and we know that we have either a CR
8126 * or an LF at <ptr>.
8127 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008128 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008129 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008130 /* there is no point trying this regex on headers */
8131 return 1;
8132 }
8133 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008134 return done;
8135}
8136
8137
8138
8139/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008140 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008141 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8142 * unparsable response.
8143 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008144int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008145{
Willy Tarreau192252e2015-04-04 01:47:55 +02008146 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008147 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008148 struct hdr_exp *exp;
8149
8150 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008151 int ret;
8152
8153 /*
8154 * The interleaving of transformations and verdicts
8155 * makes it difficult to decide to continue or stop
8156 * the evaluation.
8157 */
8158
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008159 if (txn->flags & TX_SVDENY)
8160 break;
8161
Willy Tarreau3d300592007-03-18 18:34:41 +01008162 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008163 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8164 exp->action == ACT_PASS)) {
8165 exp = exp->next;
8166 continue;
8167 }
8168
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008169 /* if this filter had a condition, evaluate it now and skip to
8170 * next filter if the condition does not match.
8171 */
8172 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008173 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008174 ret = acl_pass(ret);
8175 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8176 ret = !ret;
8177 if (!ret)
8178 continue;
8179 }
8180
Willy Tarreaua15645d2007-03-18 16:22:39 +01008181 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008182 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008183 if (unlikely(ret < 0))
8184 return -1;
8185
8186 if (likely(ret == 0)) {
8187 /* The filter did not match the response, it can be
8188 * iterated through all headers.
8189 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008190 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8191 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008192 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008193 }
8194 return 0;
8195}
8196
8197
Willy Tarreaua15645d2007-03-18 16:22:39 +01008198/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008199 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008200 * desirable to call it only when needed. This function is also used when we
8201 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008202 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008203void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008204{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008205 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008206 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008207 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008208 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008209 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008210 char *hdr_beg, *hdr_end, *hdr_next;
8211 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008212
Willy Tarreaua15645d2007-03-18 16:22:39 +01008213 /* Iterate through the headers.
8214 * we start with the start line.
8215 */
8216 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008217 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008218
8219 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8220 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008221 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008222
8223 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008224 hdr_beg = hdr_next;
8225 hdr_end = hdr_beg + cur_hdr->len;
8226 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008227
Willy Tarreau24581ba2010-08-31 22:39:35 +02008228 /* We have one full header between hdr_beg and hdr_end, and the
8229 * next header starts at hdr_next. We're only interested in
8230 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008231 */
8232
Willy Tarreau24581ba2010-08-31 22:39:35 +02008233 is_cookie2 = 0;
8234 prev = hdr_beg + 10;
8235 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008236 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008237 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8238 if (!val) {
8239 old_idx = cur_idx;
8240 continue;
8241 }
8242 is_cookie2 = 1;
8243 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008244 }
8245
Willy Tarreau24581ba2010-08-31 22:39:35 +02008246 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8247 * <prev> points to the colon.
8248 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008249 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008250
Willy Tarreau24581ba2010-08-31 22:39:35 +02008251 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8252 * check-cache is enabled) and we are not interested in checking
8253 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008254 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008255 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008256 return;
8257
Willy Tarreau24581ba2010-08-31 22:39:35 +02008258 /* OK so now we know we have to process this response cookie.
8259 * The format of the Set-Cookie header is slightly different
8260 * from the format of the Cookie header in that it does not
8261 * support the comma as a cookie delimiter (thus the header
8262 * cannot be folded) because the Expires attribute described in
8263 * the original Netscape's spec may contain an unquoted date
8264 * with a comma inside. We have to live with this because
8265 * many browsers don't support Max-Age and some browsers don't
8266 * support quoted strings. However the Set-Cookie2 header is
8267 * clean.
8268 *
8269 * We have to keep multiple pointers in order to support cookie
8270 * removal at the beginning, middle or end of header without
8271 * corrupting the header (in case of set-cookie2). A special
8272 * pointer, <scav> points to the beginning of the set-cookie-av
8273 * fields after the first semi-colon. The <next> pointer points
8274 * either to the end of line (set-cookie) or next unquoted comma
8275 * (set-cookie2). All of these headers are valid :
8276 *
8277 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8278 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8279 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8280 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8281 * | | | | | | | | | |
8282 * | | | | | | | | +-> next hdr_end <--+
8283 * | | | | | | | +------------> scav
8284 * | | | | | | +--------------> val_end
8285 * | | | | | +--------------------> val_beg
8286 * | | | | +----------------------> equal
8287 * | | | +------------------------> att_end
8288 * | | +----------------------------> att_beg
8289 * | +------------------------------> prev
8290 * +-----------------------------------------> hdr_beg
8291 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008292
Willy Tarreau24581ba2010-08-31 22:39:35 +02008293 for (; prev < hdr_end; prev = next) {
8294 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008295
Willy Tarreau24581ba2010-08-31 22:39:35 +02008296 /* find att_beg */
8297 att_beg = prev + 1;
8298 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8299 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008300
Willy Tarreau24581ba2010-08-31 22:39:35 +02008301 /* find att_end : this is the first character after the last non
8302 * space before the equal. It may be equal to hdr_end.
8303 */
8304 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008305
Willy Tarreau24581ba2010-08-31 22:39:35 +02008306 while (equal < hdr_end) {
8307 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8308 break;
8309 if (http_is_spht[(unsigned char)*equal++])
8310 continue;
8311 att_end = equal;
8312 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008313
Willy Tarreau24581ba2010-08-31 22:39:35 +02008314 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8315 * is between <att_beg> and <equal>, both may be identical.
8316 */
8317
8318 /* look for end of cookie if there is an equal sign */
8319 if (equal < hdr_end && *equal == '=') {
8320 /* look for the beginning of the value */
8321 val_beg = equal + 1;
8322 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8323 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008324
Willy Tarreau24581ba2010-08-31 22:39:35 +02008325 /* find the end of the value, respecting quotes */
8326 next = find_cookie_value_end(val_beg, hdr_end);
8327
8328 /* make val_end point to the first white space or delimitor after the value */
8329 val_end = next;
8330 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8331 val_end--;
8332 } else {
8333 /* <equal> points to next comma, semi-colon or EOL */
8334 val_beg = val_end = next = equal;
8335 }
8336
8337 if (next < hdr_end) {
8338 /* Set-Cookie2 supports multiple cookies, and <next> points to
8339 * a colon or semi-colon before the end. So skip all attr-value
8340 * pairs and look for the next comma. For Set-Cookie, since
8341 * commas are permitted in values, skip to the end.
8342 */
8343 if (is_cookie2)
8344 next = find_hdr_value_end(next, hdr_end);
8345 else
8346 next = hdr_end;
8347 }
8348
8349 /* Now everything is as on the diagram above */
8350
8351 /* Ignore cookies with no equal sign */
8352 if (equal == val_end)
8353 continue;
8354
8355 /* If there are spaces around the equal sign, we need to
8356 * strip them otherwise we'll get trouble for cookie captures,
8357 * or even for rewrites. Since this happens extremely rarely,
8358 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008359 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008360 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8361 int stripped_before = 0;
8362 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008363
Willy Tarreau24581ba2010-08-31 22:39:35 +02008364 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008365 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008366 equal += stripped_before;
8367 val_beg += stripped_before;
8368 }
8369
8370 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008371 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008372 val_beg += stripped_after;
8373 stripped_before += stripped_after;
8374 }
8375
8376 val_end += stripped_before;
8377 next += stripped_before;
8378 hdr_end += stripped_before;
8379 hdr_next += stripped_before;
8380 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008381 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008382 }
8383
8384 /* First, let's see if we want to capture this cookie. We check
8385 * that we don't already have a server side cookie, because we
8386 * can only capture one. Also as an optimisation, we ignore
8387 * cookies shorter than the declared name.
8388 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008389 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008390 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008391 (val_end - att_beg >= sess->fe->capture_namelen) &&
8392 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008393 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008394 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008395 Alert("HTTP logging : out of memory.\n");
8396 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008397 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008398 if (log_len > sess->fe->capture_len)
8399 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008400 memcpy(txn->srv_cookie, att_beg, log_len);
8401 txn->srv_cookie[log_len] = 0;
8402 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008403 }
8404
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008405 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008406 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008407 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008408 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8409 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008410 /* assume passive cookie by default */
8411 txn->flags &= ~TX_SCK_MASK;
8412 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008413
8414 /* If the cookie is in insert mode on a known server, we'll delete
8415 * this occurrence because we'll insert another one later.
8416 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008417 * a direct access.
8418 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008419 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008420 /* The "preserve" flag was set, we don't want to touch the
8421 * server's cookie.
8422 */
8423 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008424 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008425 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008426 /* this cookie must be deleted */
8427 if (*prev == ':' && next == hdr_end) {
8428 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008429 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008430 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8431 txn->hdr_idx.used--;
8432 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008433 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008434 hdr_next += delta;
8435 http_msg_move_end(&txn->rsp, delta);
8436 /* note: while both invalid now, <next> and <hdr_end>
8437 * are still equal, so the for() will stop as expected.
8438 */
8439 } else {
8440 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008441 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008442 next = prev;
8443 hdr_end += delta;
8444 hdr_next += delta;
8445 cur_hdr->len += delta;
8446 http_msg_move_end(&txn->rsp, delta);
8447 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008448 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008449 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008450 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008451 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008452 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008453 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008454 * with this server since we know it.
8455 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008456 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008457 next += delta;
8458 hdr_end += delta;
8459 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008460 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008461 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008462
Willy Tarreauf1348312010-10-07 15:54:11 +02008463 txn->flags &= ~TX_SCK_MASK;
8464 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008465 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008466 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008467 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008468 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008469 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008470 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008471 next += delta;
8472 hdr_end += delta;
8473 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008474 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008475 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008476
Willy Tarreau827aee92011-03-10 16:55:02 +01008477 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008478 txn->flags &= ~TX_SCK_MASK;
8479 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008480 }
8481 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008482 /* that's done for this cookie, check the next one on the same
8483 * line when next != hdr_end (only if is_cookie2).
8484 */
8485 }
8486 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008487 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008488 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008489}
8490
8491
Willy Tarreaua15645d2007-03-18 16:22:39 +01008492/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008493 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008494 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008495void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008496{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008497 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008498 char *p1, *p2;
8499
8500 char *cur_ptr, *cur_end, *cur_next;
8501 int cur_idx;
8502
Willy Tarreau5df51872007-11-25 16:20:08 +01008503 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008504 return;
8505
8506 /* Iterate through the headers.
8507 * we start with the start line.
8508 */
8509 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008510 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008511
8512 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8513 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008514 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008515
8516 cur_hdr = &txn->hdr_idx.v[cur_idx];
8517 cur_ptr = cur_next;
8518 cur_end = cur_ptr + cur_hdr->len;
8519 cur_next = cur_end + cur_hdr->cr + 1;
8520
8521 /* We have one full header between cur_ptr and cur_end, and the
8522 * next header starts at cur_next. We're only interested in
8523 * "Cookie:" headers.
8524 */
8525
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008526 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8527 if (val) {
8528 if ((cur_end - (cur_ptr + val) >= 8) &&
8529 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8530 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8531 return;
8532 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008533 }
8534
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008535 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8536 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008537 continue;
8538
8539 /* OK, right now we know we have a cache-control header at cur_ptr */
8540
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008541 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008542
8543 if (p1 >= cur_end) /* no more info */
8544 continue;
8545
8546 /* p1 is at the beginning of the value */
8547 p2 = p1;
8548
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008549 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008550 p2++;
8551
8552 /* we have a complete value between p1 and p2 */
8553 if (p2 < cur_end && *p2 == '=') {
8554 /* we have something of the form no-cache="set-cookie" */
8555 if ((cur_end - p1 >= 21) &&
8556 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8557 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008558 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008559 continue;
8560 }
8561
8562 /* OK, so we know that either p2 points to the end of string or to a comma */
8563 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008564 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008565 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8566 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8567 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008568 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008569 return;
8570 }
8571
8572 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008573 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008574 continue;
8575 }
8576 }
8577}
8578
Willy Tarreau58f10d72006-12-04 02:26:12 +01008579
Willy Tarreaub2513902006-12-17 14:52:38 +01008580/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008581 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008582 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008583 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008584 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008585 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008586 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008587 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008588 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008589int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008590{
8591 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008592 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008593 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008594
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008595 if (!uri_auth)
8596 return 0;
8597
Cyril Bonté70be45d2010-10-12 00:14:35 +02008598 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008599 return 0;
8600
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008601 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008602 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008603 return 0;
8604
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008605 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008606 return 0;
8607
Willy Tarreaub2513902006-12-17 14:52:38 +01008608 return 1;
8609}
8610
Willy Tarreau4076a152009-04-02 15:18:36 +02008611/*
8612 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008613 * By default it tries to report the error position as msg->err_pos. However if
8614 * this one is not set, it will then report msg->next, which is the last known
8615 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008616 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008617 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008618void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008619 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008620 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008621{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008622 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008623 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008624 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008625
Willy Tarreau9b28e032012-10-12 23:49:43 +02008626 es->len = MIN(chn->buf->i, sizeof(es->buf));
8627 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008628 len1 = MIN(len1, es->len);
8629 len2 = es->len - len1; /* remaining data if buffer wraps */
8630
Willy Tarreau9b28e032012-10-12 23:49:43 +02008631 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008632 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008633 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008634
Willy Tarreau4076a152009-04-02 15:18:36 +02008635 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008636 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008637 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008638 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008639
Willy Tarreau4076a152009-04-02 15:18:36 +02008640 es->when = date; // user-visible date
8641 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008642 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008643 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008644 if (objt_conn(sess->origin))
8645 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008646 else
8647 memset(&es->src, 0, sizeof(es->src));
8648
Willy Tarreau078272e2010-12-12 12:46:33 +01008649 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008650 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008651 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008652 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008653 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008654 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008655 es->b_out = chn->buf->o;
8656 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008657 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008658 es->m_clen = msg->chunk_len;
8659 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008660}
Willy Tarreaub2513902006-12-17 14:52:38 +01008661
Willy Tarreau294c4732011-12-16 21:35:50 +01008662/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8663 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8664 * performed over the whole headers. Otherwise it must contain a valid header
8665 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8666 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8667 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8668 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008669 * -1. The value fetch stops at commas, so this function is suited for use with
8670 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008671 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008672 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008673unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008674 struct hdr_idx *idx, int occ,
8675 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008676{
Willy Tarreau294c4732011-12-16 21:35:50 +01008677 struct hdr_ctx local_ctx;
8678 char *ptr_hist[MAX_HDR_HISTORY];
8679 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008680 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008681 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008682
Willy Tarreau294c4732011-12-16 21:35:50 +01008683 if (!ctx) {
8684 local_ctx.idx = 0;
8685 ctx = &local_ctx;
8686 }
8687
Willy Tarreaubce70882009-09-07 11:51:47 +02008688 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008689 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008690 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008691 occ--;
8692 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008693 *vptr = ctx->line + ctx->val;
8694 *vlen = ctx->vlen;
8695 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008696 }
8697 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008698 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008699 }
8700
8701 /* negative occurrence, we scan all the list then walk back */
8702 if (-occ > MAX_HDR_HISTORY)
8703 return 0;
8704
Willy Tarreau294c4732011-12-16 21:35:50 +01008705 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008706 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008707 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8708 len_hist[hist_ptr] = ctx->vlen;
8709 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008710 hist_ptr = 0;
8711 found++;
8712 }
8713 if (-occ > found)
8714 return 0;
8715 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008716 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8717 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8718 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008719 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008720 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008721 if (hist_ptr >= MAX_HDR_HISTORY)
8722 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008723 *vptr = ptr_hist[hist_ptr];
8724 *vlen = len_hist[hist_ptr];
8725 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008726}
8727
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008728/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8729 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8730 * performed over the whole headers. Otherwise it must contain a valid header
8731 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8732 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8733 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8734 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8735 * -1. This function differs from http_get_hdr() in that it only returns full
8736 * line header values and does not stop at commas.
8737 * The return value is 0 if nothing was found, or non-zero otherwise.
8738 */
8739unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8740 struct hdr_idx *idx, int occ,
8741 struct hdr_ctx *ctx, char **vptr, int *vlen)
8742{
8743 struct hdr_ctx local_ctx;
8744 char *ptr_hist[MAX_HDR_HISTORY];
8745 int len_hist[MAX_HDR_HISTORY];
8746 unsigned int hist_ptr;
8747 int found;
8748
8749 if (!ctx) {
8750 local_ctx.idx = 0;
8751 ctx = &local_ctx;
8752 }
8753
8754 if (occ >= 0) {
8755 /* search from the beginning */
8756 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8757 occ--;
8758 if (occ <= 0) {
8759 *vptr = ctx->line + ctx->val;
8760 *vlen = ctx->vlen;
8761 return 1;
8762 }
8763 }
8764 return 0;
8765 }
8766
8767 /* negative occurrence, we scan all the list then walk back */
8768 if (-occ > MAX_HDR_HISTORY)
8769 return 0;
8770
8771 found = hist_ptr = 0;
8772 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8773 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8774 len_hist[hist_ptr] = ctx->vlen;
8775 if (++hist_ptr >= MAX_HDR_HISTORY)
8776 hist_ptr = 0;
8777 found++;
8778 }
8779 if (-occ > found)
8780 return 0;
8781 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8782 * find occurrence -occ, so we have to check [hist_ptr+occ].
8783 */
8784 hist_ptr += occ;
8785 if (hist_ptr >= MAX_HDR_HISTORY)
8786 hist_ptr -= MAX_HDR_HISTORY;
8787 *vptr = ptr_hist[hist_ptr];
8788 *vlen = len_hist[hist_ptr];
8789 return 1;
8790}
8791
Willy Tarreaubaaee002006-06-26 02:48:02 +02008792/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008793 * Print a debug line with a header. Always stop at the first CR or LF char,
8794 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8795 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008796 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008797void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008798{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008799 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008800 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008801
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008802 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008803 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008804 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008805 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 +02008806
8807 for (max = 0; start + max < end; max++)
8808 if (start[max] == '\r' || start[max] == '\n')
8809 break;
8810
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008811 UBOUND(max, trash.size - trash.len - 3);
8812 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8813 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008814 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008815}
8816
Willy Tarreaueee5b512015-04-03 23:46:31 +02008817
8818/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8819 * The hdr_idx is allocated as well. In case of allocation failure, everything
8820 * allocated is freed and NULL is returned. Otherwise the new transaction is
8821 * assigned to the stream and returned.
8822 */
8823struct http_txn *http_alloc_txn(struct stream *s)
8824{
8825 struct http_txn *txn = s->txn;
8826
8827 if (txn)
8828 return txn;
8829
8830 txn = pool_alloc2(pool2_http_txn);
8831 if (!txn)
8832 return txn;
8833
8834 txn->hdr_idx.size = global.tune.max_http_hdr;
8835 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8836 if (!txn->hdr_idx.v) {
8837 pool_free2(pool2_http_txn, txn);
8838 return NULL;
8839 }
8840
8841 s->txn = txn;
8842 return txn;
8843}
8844
Willy Tarreau0937bc42009-12-22 15:03:09 +01008845/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008846 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008847 * the required fields are properly allocated and that we only need to (re)init
8848 * them. This should be used before processing any new request.
8849 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008850void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008851{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008852 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008853 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008854
8855 txn->flags = 0;
8856 txn->status = -1;
8857
Willy Tarreauf64d1412010-10-07 20:06:11 +02008858 txn->cookie_first_date = 0;
8859 txn->cookie_last_date = 0;
8860
Willy Tarreaueee5b512015-04-03 23:46:31 +02008861 txn->srv_cookie = NULL;
8862 txn->cli_cookie = NULL;
8863 txn->uri = NULL;
8864
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008865 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008866 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008867 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008868 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008869 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008870 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008871 txn->req.chunk_len = 0LL;
8872 txn->req.body_len = 0LL;
8873 txn->rsp.chunk_len = 0LL;
8874 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008875 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8876 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008877 txn->req.chn = &s->req;
8878 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008879
8880 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008881
8882 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8883 if (fe->options2 & PR_O2_REQBUG_OK)
8884 txn->req.err_pos = -1; /* let buggy requests pass */
8885
Willy Tarreau0937bc42009-12-22 15:03:09 +01008886 if (txn->hdr_idx.v)
8887 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008888
8889 vars_init(&s->vars_txn, SCOPE_TXN);
8890 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008891}
8892
8893/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008894void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008895{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008896 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008897 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008898
Willy Tarreau75195602014-03-11 15:48:55 +01008899 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008900 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008901 s->comp_algo->end(&s->comp_ctx);
8902 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008903 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008904
Willy Tarreau0937bc42009-12-22 15:03:09 +01008905 /* these ones will have been dynamically allocated */
8906 pool_free2(pool2_requri, txn->uri);
8907 pool_free2(pool2_capture, txn->cli_cookie);
8908 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008909 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008910
William Lallemanda73203e2012-03-12 12:48:57 +01008911 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008912 txn->uri = NULL;
8913 txn->srv_cookie = NULL;
8914 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008915
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008916 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008917 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008918 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008919 pool_free2(h->pool, s->req_cap[h->index]);
8920 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008921 }
8922
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008923 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008924 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008925 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008926 pool_free2(h->pool, s->res_cap[h->index]);
8927 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008928 }
8929
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008930 vars_prune(&s->vars_txn, s);
8931 vars_prune(&s->vars_reqres, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008932}
8933
8934/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008935void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008936{
8937 http_end_txn(s);
8938 http_init_txn(s);
8939
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008940 /* reinitialise the current rule list pointer to NULL. We are sure that
8941 * any rulelist match the NULL pointer.
8942 */
8943 s->current_rule_list = NULL;
8944
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008945 s->be = strm_fe(s);
8946 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008947 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008948 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008949 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008950 /* re-init store persistence */
8951 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008952 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008953
Willy Tarreau0937bc42009-12-22 15:03:09 +01008954 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008955
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008956 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008957
Willy Tarreau739cfba2010-01-25 23:11:14 +01008958 /* We must trim any excess data from the response buffer, because we
8959 * may have blocked an invalid response from a server that we don't
8960 * want to accidentely forward once we disable the analysers, nor do
8961 * we want those data to come along with next response. A typical
8962 * example of such data would be from a buggy server responding to
8963 * a HEAD with some data, or sending more than the advertised
8964 * content-length.
8965 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008966 if (unlikely(s->res.buf->i))
8967 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008968
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008969 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008970 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008971
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008972 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008973 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008974
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008975 s->req.rex = TICK_ETERNITY;
8976 s->req.wex = TICK_ETERNITY;
8977 s->req.analyse_exp = TICK_ETERNITY;
8978 s->res.rex = TICK_ETERNITY;
8979 s->res.wex = TICK_ETERNITY;
8980 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008981}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008982
Sasha Pachev218f0642014-06-16 12:05:59 -06008983void free_http_res_rules(struct list *r)
8984{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008985 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008986
8987 list_for_each_entry_safe(pr, tr, r, list) {
8988 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008989 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008990 free(pr);
8991 }
8992}
8993
8994void free_http_req_rules(struct list *r)
8995{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008996 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008997
8998 list_for_each_entry_safe(pr, tr, r, list) {
8999 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009000 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009001 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009002
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009003 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009004 free(pr);
9005 }
9006}
9007
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009008/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009009struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009010{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009011 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009012 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009013 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009014 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009015
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009016 rule = (struct act_rule*)calloc(1, sizeof(struct act_rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009017 if (!rule) {
9018 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009019 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009020 }
9021
CJ Ess108b1dd2015-04-07 12:03:37 -04009022 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009023 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009024 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009025 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009026 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009027 int code;
9028 int hc;
9029
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009030 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01009031 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009032 if (strcmp(args[cur_arg], "deny_status") == 0) {
9033 cur_arg++;
9034 if (!args[cur_arg]) {
9035 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9036 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9037 goto out_err;
9038 }
9039
9040 code = atol(args[cur_arg]);
9041 cur_arg++;
9042 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9043 if (http_err_codes[hc] == code) {
9044 rule->deny_status = hc;
9045 break;
9046 }
9047 }
9048
9049 if (hc >= HTTP_ERR_SIZE) {
9050 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9051 file, linenum, code);
9052 }
9053 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009054 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009055 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009056 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009057 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009058 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009059 cur_arg = 1;
9060
9061 while(*args[cur_arg]) {
9062 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009063 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009064 cur_arg+=2;
9065 continue;
9066 } else
9067 break;
9068 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009069 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009070 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009071 cur_arg = 1;
9072
9073 if (!*args[cur_arg] ||
9074 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9075 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9076 file, linenum, args[0]);
9077 goto out_err;
9078 }
9079 rule->arg.nice = atoi(args[cur_arg]);
9080 if (rule->arg.nice < -1024)
9081 rule->arg.nice = -1024;
9082 else if (rule->arg.nice > 1024)
9083 rule->arg.nice = 1024;
9084 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009085 } else if (!strcmp(args[0], "set-tos")) {
9086#ifdef IP_TOS
9087 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009088 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009089 cur_arg = 1;
9090
9091 if (!*args[cur_arg] ||
9092 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9093 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9094 file, linenum, args[0]);
9095 goto out_err;
9096 }
9097
9098 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9099 if (err && *err != '\0') {
9100 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9101 file, linenum, err, args[0]);
9102 goto out_err;
9103 }
9104 cur_arg++;
9105#else
9106 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9107 goto out_err;
9108#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009109 } else if (!strcmp(args[0], "set-mark")) {
9110#ifdef SO_MARK
9111 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009112 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009113 cur_arg = 1;
9114
9115 if (!*args[cur_arg] ||
9116 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9117 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9118 file, linenum, args[0]);
9119 goto out_err;
9120 }
9121
9122 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9123 if (err && *err != '\0') {
9124 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9125 file, linenum, err, args[0]);
9126 goto out_err;
9127 }
9128 cur_arg++;
9129 global.last_checks |= LSTCHK_NETADM;
9130#else
9131 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9132 goto out_err;
9133#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009134 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009135 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +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 bad_log_level:
9141 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9142 file, linenum, args[0]);
9143 goto out_err;
9144 }
9145 if (strcmp(args[cur_arg], "silent") == 0)
9146 rule->arg.loglevel = -1;
9147 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9148 goto bad_log_level;
9149 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009150 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009151 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009152 cur_arg = 1;
9153
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009154 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9155 (*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 +01009156 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9157 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009158 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009159 }
9160
9161 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9162 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9163 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009164
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009165 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009166 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009167 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9168 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009169 free(proxy->conf.lfs_file);
9170 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9171 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009172 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009173 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009174 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009175 cur_arg = 1;
9176
9177 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009178 (*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 -06009179 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9180 file, linenum, args[0]);
9181 goto out_err;
9182 }
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);
9187
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009188 error = NULL;
9189 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9190 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9191 args[cur_arg + 1], error);
9192 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009193 goto out_err;
9194 }
9195
9196 proxy->conf.args.ctx = ARGC_HRQ;
9197 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9198 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9199 file, linenum);
9200
9201 free(proxy->conf.lfs_file);
9202 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9203 proxy->conf.lfs_line = proxy->conf.args.line;
9204 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009205 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009206 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009207 cur_arg = 1;
9208
9209 if (!*args[cur_arg] ||
9210 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9211 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9212 file, linenum, args[0]);
9213 goto out_err;
9214 }
9215
9216 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9217 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9218
9219 proxy->conf.args.ctx = ARGC_HRQ;
9220 free(proxy->conf.lfs_file);
9221 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9222 proxy->conf.lfs_line = proxy->conf.args.line;
9223 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009224 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9225 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009226 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009227 struct sample_expr *expr;
9228 unsigned int where;
9229 char *err = NULL;
9230
9231 cur_arg = 1;
9232 proxy->conf.args.ctx = ARGC_TRK;
9233
9234 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9235 if (!expr) {
9236 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9237 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9238 free(err);
9239 goto out_err;
9240 }
9241
9242 where = 0;
9243 if (proxy->cap & PR_CAP_FE)
9244 where |= SMP_VAL_FE_HRQ_HDR;
9245 if (proxy->cap & PR_CAP_BE)
9246 where |= SMP_VAL_BE_HRQ_HDR;
9247
9248 if (!(expr->fetch->val & where)) {
9249 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9250 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9251 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9252 args[cur_arg-1], sample_src_names(expr->fetch->use));
9253 free(expr);
9254 goto out_err;
9255 }
9256
9257 if (strcmp(args[cur_arg], "table") == 0) {
9258 cur_arg++;
9259 if (!args[cur_arg]) {
9260 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9261 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9262 free(expr);
9263 goto out_err;
9264 }
9265 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009266 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009267 cur_arg++;
9268 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009269 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009270 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009271 } else if (strcmp(args[0], "redirect") == 0) {
9272 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009273 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009274
Willy Tarreaube4653b2015-05-28 15:26:58 +02009275 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009276 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9277 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9278 goto out_err;
9279 }
9280
9281 /* this redirect rule might already contain a parsed condition which
9282 * we'll pass to the http-request rule.
9283 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009284 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009285 rule->arg.redir = redir;
9286 rule->cond = redir->cond;
9287 redir->cond = NULL;
9288 cur_arg = 2;
9289 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009290 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9291 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009292 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009293 /*
9294 * '+ 8' for 'add-acl('
9295 * '- 9' for 'add-acl(' + trailing ')'
9296 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009297 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009298
9299 cur_arg = 1;
9300
9301 if (!*args[cur_arg] ||
9302 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9303 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9304 file, linenum, args[0]);
9305 goto out_err;
9306 }
9307
9308 LIST_INIT(&rule->arg.map.key);
9309 proxy->conf.args.ctx = ARGC_HRQ;
9310 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9311 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9312 file, linenum);
9313 free(proxy->conf.lfs_file);
9314 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9315 proxy->conf.lfs_line = proxy->conf.args.line;
9316 cur_arg += 1;
9317 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9318 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009319 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009320 /*
9321 * '+ 8' for 'del-acl('
9322 * '- 9' for 'del-acl(' + trailing ')'
9323 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009324 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009325
9326 cur_arg = 1;
9327
9328 if (!*args[cur_arg] ||
9329 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9330 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9331 file, linenum, args[0]);
9332 goto out_err;
9333 }
9334
9335 LIST_INIT(&rule->arg.map.key);
9336 proxy->conf.args.ctx = ARGC_HRQ;
9337 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9338 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9339 file, linenum);
9340 free(proxy->conf.lfs_file);
9341 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9342 proxy->conf.lfs_line = proxy->conf.args.line;
9343 cur_arg += 1;
9344 } else if (strncmp(args[0], "del-map", 7) == 0) {
9345 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009346 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009347 /*
9348 * '+ 8' for 'del-map('
9349 * '- 9' for 'del-map(' + trailing ')'
9350 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009351 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009352
9353 cur_arg = 1;
9354
9355 if (!*args[cur_arg] ||
9356 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9357 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9358 file, linenum, args[0]);
9359 goto out_err;
9360 }
9361
9362 LIST_INIT(&rule->arg.map.key);
9363 proxy->conf.args.ctx = ARGC_HRQ;
9364 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9365 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9366 file, linenum);
9367 free(proxy->conf.lfs_file);
9368 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9369 proxy->conf.lfs_line = proxy->conf.args.line;
9370 cur_arg += 1;
9371 } else if (strncmp(args[0], "set-map", 7) == 0) {
9372 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009373 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009374 /*
9375 * '+ 8' for 'set-map('
9376 * '- 9' for 'set-map(' + trailing ')'
9377 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009378 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009379
9380 cur_arg = 1;
9381
9382 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9383 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9384 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9385 file, linenum, args[0]);
9386 goto out_err;
9387 }
9388
9389 LIST_INIT(&rule->arg.map.key);
9390 LIST_INIT(&rule->arg.map.value);
9391 proxy->conf.args.ctx = ARGC_HRQ;
9392
9393 /* key pattern */
9394 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9395 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9396 file, linenum);
9397
9398 /* value pattern */
9399 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9400 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9401 file, linenum);
9402 free(proxy->conf.lfs_file);
9403 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9404 proxy->conf.lfs_line = proxy->conf.args.line;
9405
9406 cur_arg += 2;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009407 } else if (strncmp(args[0], "set-src", 7) == 0) {
9408 struct sample_expr *expr;
9409 unsigned int where;
9410 char *err = NULL;
9411
9412 cur_arg = 1;
9413 proxy->conf.args.ctx = ARGC_HRQ;
9414
9415 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9416 if (!expr) {
9417 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9418 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9419 free(err);
9420 goto out_err;
9421 }
9422
9423 where = 0;
9424 if (proxy->cap & PR_CAP_FE)
9425 where |= SMP_VAL_FE_HRQ_HDR;
9426 if (proxy->cap & PR_CAP_BE)
9427 where |= SMP_VAL_BE_HRQ_HDR;
9428
9429 if (!(expr->fetch->val & where)) {
9430 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9431 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9432 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9433 args[cur_arg-1], sample_src_names(expr->fetch->use));
9434 free(expr);
9435 goto out_err;
9436 }
9437
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02009438 rule->arg.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009439 rule->action = ACT_HTTP_REQ_SET_SRC;
William Lallemand73025dd2014-04-24 14:38:37 +02009440 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9441 char *errmsg = NULL;
9442 cur_arg = 1;
9443 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009444 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009445 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009446 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009447 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9448 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9449 free(errmsg);
9450 goto out_err;
9451 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009452 } else {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009453 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'set-var', 'set-src', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009454 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009455 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009456 }
9457
9458 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9459 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009460 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009461
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009462 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9463 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9464 file, linenum, args[0], errmsg);
9465 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009466 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009467 }
9468 rule->cond = cond;
9469 }
9470 else if (*args[cur_arg]) {
9471 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9472 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9473 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009474 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009475 }
9476
9477 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009478 out_err:
9479 free(rule);
9480 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009481}
9482
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009483/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009484struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009485{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009486 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009487 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009488 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009489 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009490
9491 rule = calloc(1, sizeof(*rule));
9492 if (!rule) {
9493 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9494 goto out_err;
9495 }
9496
9497 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009498 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009499 cur_arg = 1;
9500 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009501 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009502 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009503 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009504 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009505 cur_arg = 1;
9506
9507 if (!*args[cur_arg] ||
9508 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9509 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9510 file, linenum, args[0]);
9511 goto out_err;
9512 }
9513 rule->arg.nice = atoi(args[cur_arg]);
9514 if (rule->arg.nice < -1024)
9515 rule->arg.nice = -1024;
9516 else if (rule->arg.nice > 1024)
9517 rule->arg.nice = 1024;
9518 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009519 } else if (!strcmp(args[0], "set-tos")) {
9520#ifdef IP_TOS
9521 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009522 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009523 cur_arg = 1;
9524
9525 if (!*args[cur_arg] ||
9526 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9527 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9528 file, linenum, args[0]);
9529 goto out_err;
9530 }
9531
9532 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9533 if (err && *err != '\0') {
9534 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9535 file, linenum, err, args[0]);
9536 goto out_err;
9537 }
9538 cur_arg++;
9539#else
9540 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9541 goto out_err;
9542#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009543 } else if (!strcmp(args[0], "set-mark")) {
9544#ifdef SO_MARK
9545 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009546 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009547 cur_arg = 1;
9548
9549 if (!*args[cur_arg] ||
9550 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9551 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9552 file, linenum, args[0]);
9553 goto out_err;
9554 }
9555
9556 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9557 if (err && *err != '\0') {
9558 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9559 file, linenum, err, args[0]);
9560 goto out_err;
9561 }
9562 cur_arg++;
9563 global.last_checks |= LSTCHK_NETADM;
9564#else
9565 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9566 goto out_err;
9567#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009568 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009569 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009570 cur_arg = 1;
9571
9572 if (!*args[cur_arg] ||
9573 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9574 bad_log_level:
9575 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9576 file, linenum, args[0]);
9577 goto out_err;
9578 }
9579 if (strcmp(args[cur_arg], "silent") == 0)
9580 rule->arg.loglevel = -1;
9581 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9582 goto bad_log_level;
9583 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009584 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009585 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009586 cur_arg = 1;
9587
9588 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9589 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9590 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9591 file, linenum, args[0]);
9592 goto out_err;
9593 }
9594
9595 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9596 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9597 LIST_INIT(&rule->arg.hdr_add.fmt);
9598
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009599 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009600 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009601 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9602 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009603 free(proxy->conf.lfs_file);
9604 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9605 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009606 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009607 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009608 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009609 cur_arg = 1;
9610
9611 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009612 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9613 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009614 file, linenum, args[0]);
9615 goto out_err;
9616 }
9617
9618 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9619 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9620 LIST_INIT(&rule->arg.hdr_add.fmt);
9621
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009622 error = NULL;
9623 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9624 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9625 args[cur_arg + 1], error);
9626 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009627 goto out_err;
9628 }
9629
9630 proxy->conf.args.ctx = ARGC_HRQ;
9631 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9632 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9633 file, linenum);
9634
9635 free(proxy->conf.lfs_file);
9636 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9637 proxy->conf.lfs_line = proxy->conf.args.line;
9638 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009639 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009640 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009641 cur_arg = 1;
9642
9643 if (!*args[cur_arg] ||
9644 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9645 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9646 file, linenum, args[0]);
9647 goto out_err;
9648 }
9649
9650 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9651 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9652
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009653 proxy->conf.args.ctx = ARGC_HRS;
9654 free(proxy->conf.lfs_file);
9655 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9656 proxy->conf.lfs_line = proxy->conf.args.line;
9657 cur_arg += 1;
9658 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9659 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009660 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009661 /*
9662 * '+ 8' for 'add-acl('
9663 * '- 9' for 'add-acl(' + trailing ')'
9664 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009665 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009666
9667 cur_arg = 1;
9668
9669 if (!*args[cur_arg] ||
9670 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9671 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9672 file, linenum, args[0]);
9673 goto out_err;
9674 }
9675
9676 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009677 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009678 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9679 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9680 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009681 free(proxy->conf.lfs_file);
9682 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9683 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009684
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009685 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009686 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9687 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009688 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009689 /*
9690 * '+ 8' for 'del-acl('
9691 * '- 9' for 'del-acl(' + trailing ')'
9692 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009693 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009694
9695 cur_arg = 1;
9696
9697 if (!*args[cur_arg] ||
9698 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9699 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9700 file, linenum, args[0]);
9701 goto out_err;
9702 }
9703
9704 LIST_INIT(&rule->arg.map.key);
9705 proxy->conf.args.ctx = ARGC_HRS;
9706 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9707 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9708 file, linenum);
9709 free(proxy->conf.lfs_file);
9710 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9711 proxy->conf.lfs_line = proxy->conf.args.line;
9712 cur_arg += 1;
9713 } else if (strncmp(args[0], "del-map", 7) == 0) {
9714 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009715 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009716 /*
9717 * '+ 8' for 'del-map('
9718 * '- 9' for 'del-map(' + 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], "set-map", 7) == 0) {
9741 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009742 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009743 /*
9744 * '+ 8' for 'set-map('
9745 * '- 9' for 'set-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] || !*args[cur_arg+1] ||
9752 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9753 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9754 file, linenum, args[0]);
9755 goto out_err;
9756 }
9757
9758 LIST_INIT(&rule->arg.map.key);
9759 LIST_INIT(&rule->arg.map.value);
9760
9761 proxy->conf.args.ctx = ARGC_HRS;
9762
9763 /* key pattern */
9764 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9765 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9766 file, linenum);
9767
9768 /* value pattern */
9769 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9770 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9771 file, linenum);
9772
9773 free(proxy->conf.lfs_file);
9774 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9775 proxy->conf.lfs_line = proxy->conf.args.line;
9776
9777 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009778 } else if (strcmp(args[0], "redirect") == 0) {
9779 struct redirect_rule *redir;
9780 char *errmsg = NULL;
9781
9782 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9783 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9784 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9785 goto out_err;
9786 }
9787
9788 /* this redirect rule might already contain a parsed condition which
9789 * we'll pass to the http-request rule.
9790 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009791 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009792 rule->arg.redir = redir;
9793 rule->cond = redir->cond;
9794 redir->cond = NULL;
9795 cur_arg = 2;
9796 return rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009797 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9798 char *errmsg = NULL;
9799 cur_arg = 1;
9800 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009801 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009802 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009803 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009804 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9805 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9806 free(errmsg);
9807 goto out_err;
9808 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009809 } else {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02009810 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'del-acl', 'add-acl', 'del-map', 'set-map', 'set-var' but got '%s'%s.\n",
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009811 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9812 goto out_err;
9813 }
9814
9815 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9816 struct acl_cond *cond;
9817 char *errmsg = NULL;
9818
9819 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9820 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9821 file, linenum, args[0], errmsg);
9822 free(errmsg);
9823 goto out_err;
9824 }
9825 rule->cond = cond;
9826 }
9827 else if (*args[cur_arg]) {
9828 Alert("parsing [%s:%d]: 'http-response %s' expects"
9829 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9830 file, linenum, args[0], args[cur_arg]);
9831 goto out_err;
9832 }
9833
9834 return rule;
9835 out_err:
9836 free(rule);
9837 return NULL;
9838}
9839
Willy Tarreau4baae242012-12-27 12:00:31 +01009840/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009841 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009842 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9843 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009844 */
9845struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009846 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009847{
9848 struct redirect_rule *rule;
9849 int cur_arg;
9850 int type = REDIRECT_TYPE_NONE;
9851 int code = 302;
9852 const char *destination = NULL;
9853 const char *cookie = NULL;
9854 int cookie_set = 0;
9855 unsigned int flags = REDIRECT_FLAG_NONE;
9856 struct acl_cond *cond = NULL;
9857
9858 cur_arg = 0;
9859 while (*(args[cur_arg])) {
9860 if (strcmp(args[cur_arg], "location") == 0) {
9861 if (!*args[cur_arg + 1])
9862 goto missing_arg;
9863
9864 type = REDIRECT_TYPE_LOCATION;
9865 cur_arg++;
9866 destination = args[cur_arg];
9867 }
9868 else if (strcmp(args[cur_arg], "prefix") == 0) {
9869 if (!*args[cur_arg + 1])
9870 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009871 type = REDIRECT_TYPE_PREFIX;
9872 cur_arg++;
9873 destination = args[cur_arg];
9874 }
9875 else if (strcmp(args[cur_arg], "scheme") == 0) {
9876 if (!*args[cur_arg + 1])
9877 goto missing_arg;
9878
9879 type = REDIRECT_TYPE_SCHEME;
9880 cur_arg++;
9881 destination = args[cur_arg];
9882 }
9883 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9884 if (!*args[cur_arg + 1])
9885 goto missing_arg;
9886
9887 cur_arg++;
9888 cookie = args[cur_arg];
9889 cookie_set = 1;
9890 }
9891 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9892 if (!*args[cur_arg + 1])
9893 goto missing_arg;
9894
9895 cur_arg++;
9896 cookie = args[cur_arg];
9897 cookie_set = 0;
9898 }
9899 else if (strcmp(args[cur_arg], "code") == 0) {
9900 if (!*args[cur_arg + 1])
9901 goto missing_arg;
9902
9903 cur_arg++;
9904 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009905 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009906 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009907 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009908 args[cur_arg - 1], args[cur_arg]);
9909 return NULL;
9910 }
9911 }
9912 else if (!strcmp(args[cur_arg],"drop-query")) {
9913 flags |= REDIRECT_FLAG_DROP_QS;
9914 }
9915 else if (!strcmp(args[cur_arg],"append-slash")) {
9916 flags |= REDIRECT_FLAG_APPEND_SLASH;
9917 }
9918 else if (strcmp(args[cur_arg], "if") == 0 ||
9919 strcmp(args[cur_arg], "unless") == 0) {
9920 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9921 if (!cond) {
9922 memprintf(errmsg, "error in condition: %s", *errmsg);
9923 return NULL;
9924 }
9925 break;
9926 }
9927 else {
9928 memprintf(errmsg,
9929 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9930 args[cur_arg]);
9931 return NULL;
9932 }
9933 cur_arg++;
9934 }
9935
9936 if (type == REDIRECT_TYPE_NONE) {
9937 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9938 return NULL;
9939 }
9940
Willy Tarreaube4653b2015-05-28 15:26:58 +02009941 if (dir && type != REDIRECT_TYPE_LOCATION) {
9942 memprintf(errmsg, "response only supports redirect type 'location'");
9943 return NULL;
9944 }
9945
Willy Tarreau4baae242012-12-27 12:00:31 +01009946 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9947 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009948 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009949
9950 if (!use_fmt) {
9951 /* old-style static redirect rule */
9952 rule->rdr_str = strdup(destination);
9953 rule->rdr_len = strlen(destination);
9954 }
9955 else {
9956 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009957
9958 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9959 * if prefix == "/", we don't want to add anything, otherwise it
9960 * makes it hard for the user to configure a self-redirection.
9961 */
Godbachd9722032014-12-18 15:44:58 +08009962 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009963 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009964 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009965 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
9966 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009967 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009968 free(curproxy->conf.lfs_file);
9969 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9970 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009971 }
9972 }
9973
Willy Tarreau4baae242012-12-27 12:00:31 +01009974 if (cookie) {
9975 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9976 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9977 */
9978 rule->cookie_len = strlen(cookie);
9979 if (cookie_set) {
9980 rule->cookie_str = malloc(rule->cookie_len + 10);
9981 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9982 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9983 rule->cookie_len += 9;
9984 } else {
9985 rule->cookie_str = malloc(rule->cookie_len + 21);
9986 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9987 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9988 rule->cookie_len += 20;
9989 }
9990 }
9991 rule->type = type;
9992 rule->code = code;
9993 rule->flags = flags;
9994 LIST_INIT(&rule->list);
9995 return rule;
9996
9997 missing_arg:
9998 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9999 return NULL;
10000}
10001
Willy Tarreau8797c062007-05-07 00:55:35 +020010002/************************************************************************/
10003/* The code below is dedicated to ACL parsing and matching */
10004/************************************************************************/
10005
10006
Willy Tarreau14174bc2012-04-16 14:34:04 +020010007/* This function ensures that the prerequisites for an L7 fetch are ready,
10008 * which means that a request or response is ready. If some data is missing,
10009 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010010 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10011 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010012 *
10013 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010014 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10015 * decide whether or not an HTTP message is present ;
10016 * 0 if the requested data cannot be fetched or if it is certain that
10017 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010018 * 1 if an HTTP message is ready
10019 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010020int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010021 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010022{
Willy Tarreau192252e2015-04-04 01:47:55 +020010023 struct http_txn *txn;
10024 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010025
Willy Tarreaueee5b512015-04-03 23:46:31 +020010026 /* Note: this function may only be used from places where
10027 * http_init_txn() has already been done, and implies that <s>,
10028 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
10029 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010030 */
10031
Willy Tarreau192252e2015-04-04 01:47:55 +020010032 if (!s)
10033 return 0;
10034 txn = s->txn;
10035
10036 if (!txn)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010037 return 0;
Willy Tarreau192252e2015-04-04 01:47:55 +020010038 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010039
10040 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010041 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010042
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010043 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010044 /* If the buffer does not leave enough free space at the end,
10045 * we must first realign it.
10046 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010047 if (s->req.buf->p > s->req.buf->data &&
10048 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10049 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010050
Willy Tarreau14174bc2012-04-16 14:34:04 +020010051 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010052 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010053 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010054
10055 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010056 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010057 http_msg_analyzer(msg, &txn->hdr_idx);
10058
10059 /* Still no valid request ? */
10060 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010061 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010062 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010063 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010064 }
10065 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010066 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010067 return 0;
10068 }
10069
10070 /* OK we just got a valid HTTP request. We have some minor
10071 * preparation to perform so that further checks can rely
10072 * on HTTP tests.
10073 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010074
10075 /* If the request was parsed but was too large, we must absolutely
10076 * return an error so that it is not processed. At the moment this
10077 * cannot happen, but if the parsers are to change in the future,
10078 * we want this check to be maintained.
10079 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010080 if (unlikely(s->req.buf->i + s->req.buf->p >
10081 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010082 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010083 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010084 return 1;
10085 }
10086
Willy Tarreau9b28e032012-10-12 23:49:43 +020010087 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010088 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010089 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010090
Willy Tarreau506d0502013-07-06 13:29:24 +020010091 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10092 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010093 }
10094
Willy Tarreau506d0502013-07-06 13:29:24 +020010095 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010096 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010097 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010098
10099 /* otherwise everything's ready for the request */
10100 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010101 else {
10102 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010103 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10104 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010105 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010106 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010107 }
10108
10109 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010110 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010111 return 1;
10112}
Willy Tarreau8797c062007-05-07 00:55:35 +020010113
Willy Tarreau8797c062007-05-07 00:55:35 +020010114/* 1. Check on METHOD
10115 * We use the pre-parsed method if it is known, and store its number as an
10116 * integer. If it is unknown, we use the pointer and the length.
10117 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010118static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010119{
10120 int len, meth;
10121
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010122 len = strlen(text);
10123 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010124
10125 pattern->val.i = meth;
10126 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010127 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010128 pattern->len = len;
10129 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010130 else {
10131 pattern->ptr.str = NULL;
10132 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010133 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010134 return 1;
10135}
10136
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010137/* This function fetches the method of current HTTP request and stores
10138 * it in the global pattern struct as a chunk. There are two possibilities :
10139 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10140 * in <len> and <ptr> is NULL ;
10141 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10142 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010143 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010144 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010145static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010146smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010147{
10148 int meth;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010149 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010150
Willy Tarreau24e32d82012-04-23 23:55:44 +020010151 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010152
Willy Tarreau8797c062007-05-07 00:55:35 +020010153 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010154 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010155 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010156 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010157 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10158 /* ensure the indexes are not affected */
10159 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010160 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010161 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10162 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010163 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010164 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010165 return 1;
10166}
10167
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010168/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010169static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010170{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010171 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010172 struct pattern_list *lst;
10173 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010174
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010175 list_for_each_entry(lst, &expr->patterns, list) {
10176 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010177
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010178 /* well-known method */
10179 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010180 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010181 return pattern;
10182 else
10183 continue;
10184 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010185
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010186 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010187 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010188 continue;
10189
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010190 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010191 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10192 (!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 +010010193 return pattern;
10194 }
10195 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010196}
10197
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010198static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010199smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010200{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010201 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010202 char *ptr;
10203 int len;
10204
Willy Tarreauc0239e02012-04-16 14:42:55 +020010205 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010206
Willy Tarreau8797c062007-05-07 00:55:35 +020010207 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010208 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010209
10210 while ((len-- > 0) && (*ptr++ != '/'));
10211 if (len <= 0)
10212 return 0;
10213
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010214 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010215 smp->data.u.str.str = ptr;
10216 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010217
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010218 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010219 return 1;
10220}
10221
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010222static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010223smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010224{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010225 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010226 char *ptr;
10227 int len;
10228
Willy Tarreauc0239e02012-04-16 14:42:55 +020010229 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010230
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010231 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010232 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10233 return 0;
10234
Willy Tarreau8797c062007-05-07 00:55:35 +020010235 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010236 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010237
10238 while ((len-- > 0) && (*ptr++ != '/'));
10239 if (len <= 0)
10240 return 0;
10241
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010242 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010243 smp->data.u.str.str = ptr;
10244 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010245
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010246 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010247 return 1;
10248}
10249
10250/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010251static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010252smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010253{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010254 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010255 char *ptr;
10256 int len;
10257
Willy Tarreauc0239e02012-04-16 14:42:55 +020010258 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010259
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010260 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010261 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10262 return 0;
10263
Willy Tarreau8797c062007-05-07 00:55:35 +020010264 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010265 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010266
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010267 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010268 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010269 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010270 return 1;
10271}
10272
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010273/* returns the longest available part of the body. This requires that the body
10274 * has been waited for using http-buffer-request.
10275 */
10276static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010277smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010278{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010279 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010280 struct http_msg *msg;
10281 unsigned long len;
10282 unsigned long block1;
10283 char *body;
10284 struct chunk *temp;
10285
10286 CHECK_HTTP_MESSAGE_FIRST();
10287
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010288 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010289 msg = &txn->req;
10290 else
10291 msg = &txn->rsp;
10292
10293 len = http_body_bytes(msg);
10294 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10295
10296 block1 = len;
10297 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10298 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10299
10300 if (block1 == len) {
10301 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010302 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010303 smp->data.u.str.str = body;
10304 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010305 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10306 }
10307 else {
10308 /* buffer is wrapped, we need to defragment it */
10309 temp = get_trash_chunk();
10310 memcpy(temp->str, body, block1);
10311 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010312 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010313 smp->data.u.str.str = temp->str;
10314 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010315 smp->flags = SMP_F_VOL_TEST;
10316 }
10317 return 1;
10318}
10319
10320
10321/* returns the available length of the body. This requires that the body
10322 * has been waited for using http-buffer-request.
10323 */
10324static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010325smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010326{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010327 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010328 struct http_msg *msg;
10329
10330 CHECK_HTTP_MESSAGE_FIRST();
10331
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010332 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010333 msg = &txn->req;
10334 else
10335 msg = &txn->rsp;
10336
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010337 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010338 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010339
10340 smp->flags = SMP_F_VOL_TEST;
10341 return 1;
10342}
10343
10344
10345/* returns the advertised length of the body, or the advertised size of the
10346 * chunks available in the buffer. This requires that the body has been waited
10347 * for using http-buffer-request.
10348 */
10349static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010350smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010351{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010352 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010353 struct http_msg *msg;
10354
10355 CHECK_HTTP_MESSAGE_FIRST();
10356
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010357 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010358 msg = &txn->req;
10359 else
10360 msg = &txn->rsp;
10361
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010362 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010363 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010364
10365 smp->flags = SMP_F_VOL_TEST;
10366 return 1;
10367}
10368
10369
Willy Tarreau8797c062007-05-07 00:55:35 +020010370/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010371static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010372smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010373{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010374 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010375
Willy Tarreauc0239e02012-04-16 14:42:55 +020010376 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010377
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010378 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010379 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010380 smp->data.u.str.len = txn->req.sl.rq.u_l;
10381 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010382 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010383 return 1;
10384}
10385
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010386static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010387smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010388{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010389 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010390 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010391
Willy Tarreauc0239e02012-04-16 14:42:55 +020010392 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010393
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010394 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010395 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 +020010396 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010397 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010398
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010399 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010400 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010401 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010402 return 1;
10403}
10404
10405static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010406smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010407{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010408 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010409 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010410
Willy Tarreauc0239e02012-04-16 14:42:55 +020010411 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010412
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010413 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010414 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 +020010415 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10416 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010417
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010418 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010419 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010420 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010421 return 1;
10422}
10423
Willy Tarreau185b5c42012-04-26 15:11:51 +020010424/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10425 * Accepts an optional argument of type string containing the header field name,
10426 * and an optional argument of type signed or unsigned integer to request an
10427 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010428 * headers are considered from the first one. It does not stop on commas and
10429 * returns full lines instead (useful for User-Agent or Date for example).
10430 */
10431static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010432smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010433{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010434 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010435 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010436 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010437 int occ = 0;
10438 const char *name_str = NULL;
10439 int name_len = 0;
10440
10441 if (!ctx) {
10442 /* first call */
10443 ctx = &static_hdr_ctx;
10444 ctx->idx = 0;
10445 smp->ctx.a[0] = ctx;
10446 }
10447
10448 if (args) {
10449 if (args[0].type != ARGT_STR)
10450 return 0;
10451 name_str = args[0].data.str.str;
10452 name_len = args[0].data.str.len;
10453
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010454 if (args[1].type == ARGT_SINT)
10455 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010456 }
10457
10458 CHECK_HTTP_MESSAGE_FIRST();
10459
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010460 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010461 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 +020010462
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010463 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10464 /* search for header from the beginning */
10465 ctx->idx = 0;
10466
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010467 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010468 /* no explicit occurrence and single fetch => last header by default */
10469 occ = -1;
10470
10471 if (!occ)
10472 /* prepare to report multiple occurrences for ACL fetches */
10473 smp->flags |= SMP_F_NOT_LAST;
10474
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010475 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010476 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010477 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 +020010478 return 1;
10479
10480 smp->flags &= ~SMP_F_NOT_LAST;
10481 return 0;
10482}
10483
10484/* 6. Check on HTTP header count. The number of occurrences is returned.
10485 * Accepts exactly 1 argument of type string. It does not stop on commas and
10486 * returns full lines instead (useful for User-Agent or Date for example).
10487 */
10488static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010489smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010490{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010491 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010492 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010493 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010494 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010495 const char *name = NULL;
10496 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010497
Willy Tarreau601a4d12015-04-01 19:16:09 +020010498 if (args && args->type == ARGT_STR) {
10499 name = args->data.str.str;
10500 len = args->data.str.len;
10501 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010502
10503 CHECK_HTTP_MESSAGE_FIRST();
10504
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010505 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010506 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 +020010507
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010508 ctx.idx = 0;
10509 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010510 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010511 cnt++;
10512
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010513 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010514 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010515 smp->flags = SMP_F_VOL_HDR;
10516 return 1;
10517}
10518
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010519static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010520smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010521{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010522 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010523 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010524 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010525 struct chunk *temp;
10526 char del = ',';
10527
10528 if (args && args->type == ARGT_STR)
10529 del = *args[0].data.str.str;
10530
10531 CHECK_HTTP_MESSAGE_FIRST();
10532
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010533 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010534 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 +020010535
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010536 temp = get_trash_chunk();
10537
10538 ctx.idx = 0;
10539 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10540 if (temp->len)
10541 temp->str[temp->len++] = del;
10542 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10543 temp->len += ctx.del;
10544 }
10545
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010546 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010547 smp->data.u.str.str = temp->str;
10548 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010549 smp->flags = SMP_F_VOL_HDR;
10550 return 1;
10551}
10552
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010553/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10554 * Accepts an optional argument of type string containing the header field name,
10555 * and an optional argument of type signed or unsigned integer to request an
10556 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010557 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010558 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010559static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010560smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010561{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010562 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010563 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010564 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010565 int occ = 0;
10566 const char *name_str = NULL;
10567 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010568
Willy Tarreaua890d072013-04-02 12:01:06 +020010569 if (!ctx) {
10570 /* first call */
10571 ctx = &static_hdr_ctx;
10572 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010573 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010574 }
10575
Willy Tarreau185b5c42012-04-26 15:11:51 +020010576 if (args) {
10577 if (args[0].type != ARGT_STR)
10578 return 0;
10579 name_str = args[0].data.str.str;
10580 name_len = args[0].data.str.len;
10581
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010582 if (args[1].type == ARGT_SINT)
10583 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010584 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010585
Willy Tarreaue333ec92012-04-16 16:26:40 +020010586 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010587
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010588 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010589 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 +020010590
Willy Tarreau185b5c42012-04-26 15:11:51 +020010591 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010592 /* search for header from the beginning */
10593 ctx->idx = 0;
10594
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010595 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010596 /* no explicit occurrence and single fetch => last header by default */
10597 occ = -1;
10598
10599 if (!occ)
10600 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010601 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010602
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010603 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010604 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010605 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 +020010606 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010607
Willy Tarreau37406352012-04-23 16:16:37 +020010608 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010609 return 0;
10610}
10611
Willy Tarreauc11416f2007-06-17 16:58:38 +020010612/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010613 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010614 */
10615static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010616smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010617{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010618 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010619 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010620 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010621 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010622 const char *name = NULL;
10623 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010624
Willy Tarreau601a4d12015-04-01 19:16:09 +020010625 if (args && args->type == ARGT_STR) {
10626 name = args->data.str.str;
10627 len = args->data.str.len;
10628 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010629
Willy Tarreaue333ec92012-04-16 16:26:40 +020010630 CHECK_HTTP_MESSAGE_FIRST();
10631
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010632 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010633 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 +020010634
Willy Tarreau33a7e692007-06-10 19:45:56 +020010635 ctx.idx = 0;
10636 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010637 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010638 cnt++;
10639
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010640 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010641 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010642 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010643 return 1;
10644}
10645
Willy Tarreau185b5c42012-04-26 15:11:51 +020010646/* Fetch an HTTP header's integer value. The integer value is returned. It
10647 * takes a mandatory argument of type string and an optional one of type int
10648 * to designate a specific occurrence. It returns an unsigned integer, which
10649 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010650 */
10651static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010652smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010653{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010654 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010655
Willy Tarreauf853c462012-04-23 18:53:56 +020010656 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010657 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010658 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010659 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010660
Willy Tarreaud53e2422012-04-16 17:21:11 +020010661 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010662}
10663
Cyril Bonté69fa9922012-10-25 00:01:06 +020010664/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10665 * and an optional one of type int to designate a specific occurrence.
10666 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010667 */
10668static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010669smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010670{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010671 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010672
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010673 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010674 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010675 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010676 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010677 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010678 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010679 if (smp->data.u.str.len < temp->size - 1) {
10680 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10681 temp->str[smp->data.u.str.len] = '\0';
10682 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010683 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010684 break;
10685 }
10686 }
10687 }
10688
Willy Tarreaud53e2422012-04-16 17:21:11 +020010689 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010690 if (!(smp->flags & SMP_F_NOT_LAST))
10691 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010692 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010693 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010694}
10695
Willy Tarreau737b0c12007-06-10 21:28:46 +020010696/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10697 * the first '/' after the possible hostname, and ends before the possible '?'.
10698 */
10699static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010700smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010701{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010702 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010703 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010704
Willy Tarreauc0239e02012-04-16 14:42:55 +020010705 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010706
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010707 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010708 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010709 ptr = http_get_path(txn);
10710 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010711 return 0;
10712
10713 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010714 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010715 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010716
10717 while (ptr < end && *ptr != '?')
10718 ptr++;
10719
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010720 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010721 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010722 return 1;
10723}
10724
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010725/* This produces a concatenation of the first occurrence of the Host header
10726 * followed by the path component if it begins with a slash ('/'). This means
10727 * that '*' will not be added, resulting in exactly the first Host entry.
10728 * If no Host header is found, then the path is returned as-is. The returned
10729 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010730 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010731 */
10732static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010733smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010734{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010735 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010736 char *ptr, *end, *beg;
10737 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010738 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010739
10740 CHECK_HTTP_MESSAGE_FIRST();
10741
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010742 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010743 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010744 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010745 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010746
10747 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010748 temp = get_trash_chunk();
10749 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010750 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010751 smp->data.u.str.str = temp->str;
10752 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010753
10754 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010755 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010756 beg = http_get_path(txn);
10757 if (!beg)
10758 beg = end;
10759
10760 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10761
10762 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010763 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10764 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010765 }
10766
10767 smp->flags = SMP_F_VOL_1ST;
10768 return 1;
10769}
10770
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010771/* This produces a 32-bit hash of the concatenation of the first occurrence of
10772 * the Host header followed by the path component if it begins with a slash ('/').
10773 * This means that '*' will not be added, resulting in exactly the first Host
10774 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010775 * is hashed using the path hash followed by a full avalanche hash and provides a
10776 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010777 * high-traffic sites without having to store whole paths.
10778 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010779int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010780smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010781{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010782 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010783 struct hdr_ctx ctx;
10784 unsigned int hash = 0;
10785 char *ptr, *beg, *end;
10786 int len;
10787
10788 CHECK_HTTP_MESSAGE_FIRST();
10789
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010790 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010791 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010792 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010793 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10794 ptr = ctx.line + ctx.val;
10795 len = ctx.vlen;
10796 while (len--)
10797 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10798 }
10799
10800 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010801 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010802 beg = http_get_path(txn);
10803 if (!beg)
10804 beg = end;
10805
10806 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10807
10808 if (beg < ptr && *beg == '/') {
10809 while (beg < ptr)
10810 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10811 }
10812 hash = full_hash(hash);
10813
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010814 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010815 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010816 smp->flags = SMP_F_VOL_1ST;
10817 return 1;
10818}
10819
Willy Tarreau4a550602012-12-09 14:53:32 +010010820/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010821 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10822 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10823 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010824 * that in environments where IPv6 is insignificant, truncating the output to
10825 * 8 bytes would still work.
10826 */
10827static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010828smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010829{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010830 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010831 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010832
10833 if (!cli_conn)
10834 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010835
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010836 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010837 return 0;
10838
Willy Tarreau47ca5452012-12-23 20:22:19 +010010839 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010840 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010841 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010842
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010843 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010844 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010845 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010846 temp->len += 4;
10847 break;
10848 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010849 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010850 temp->len += 16;
10851 break;
10852 default:
10853 return 0;
10854 }
10855
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010856 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010857 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010858 return 1;
10859}
10860
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010861/* Extracts the query string, which comes after the question mark '?'. If no
10862 * question mark is found, nothing is returned. Otherwise it returns a sample
10863 * of type string carrying the whole query string.
10864 */
10865static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010866smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010867{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010868 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010869 char *ptr, *end;
10870
10871 CHECK_HTTP_MESSAGE_FIRST();
10872
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010873 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010874 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10875 end = ptr + txn->req.sl.rq.u_l;
10876
10877 /* look up the '?' */
10878 do {
10879 if (ptr == end)
10880 return 0;
10881 } while (*ptr++ != '?');
10882
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010883 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010884 smp->data.u.str.str = ptr;
10885 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010886 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10887 return 1;
10888}
10889
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010890static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010891smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010892{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010893 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10894 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10895 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010896
Willy Tarreau24e32d82012-04-23 23:55:44 +020010897 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010898
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010899 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010900 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010901 return 1;
10902}
10903
Willy Tarreau7f18e522010-10-22 20:04:13 +020010904/* return a valid test if the current request is the first one on the connection */
10905static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010906smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010907{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010908 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010909 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010910 return 1;
10911}
10912
Willy Tarreau34db1082012-04-19 17:16:54 +020010913/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010914static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010915smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010916{
10917
Willy Tarreau24e32d82012-04-23 23:55:44 +020010918 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010919 return 0;
10920
Willy Tarreauc0239e02012-04-16 14:42:55 +020010921 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010922
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010923 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010924 return 0;
10925
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010926 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010927 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010928 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010929 return 1;
10930}
Willy Tarreau8797c062007-05-07 00:55:35 +020010931
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010932/* Accepts exactly 1 argument of type userlist */
10933static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010934smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010935{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010936 if (!args || args->type != ARGT_USR)
10937 return 0;
10938
10939 CHECK_HTTP_MESSAGE_FIRST();
10940
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010941 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010942 return 0;
10943
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010944 /* if the user does not belong to the userlist or has a wrong password,
10945 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010946 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010947 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010948 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10949 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010950 return 0;
10951
10952 /* pat_match_auth() will need the user list */
10953 smp->ctx.a[0] = args->data.usr;
10954
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010955 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010956 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010957 smp->data.u.str.str = smp->strm->txn->auth.user;
10958 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010959
10960 return 1;
10961}
10962
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010963/* Try to find the next occurrence of a cookie name in a cookie header value.
10964 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10965 * the cookie value is returned into *value and *value_l, and the function
10966 * returns a pointer to the next pointer to search from if the value was found.
10967 * Otherwise if the cookie was not found, NULL is returned and neither value
10968 * nor value_l are touched. The input <hdr> string should first point to the
10969 * header's value, and the <hdr_end> pointer must point to the first character
10970 * not part of the value. <list> must be non-zero if value may represent a list
10971 * of values (cookie headers). This makes it faster to abort parsing when no
10972 * list is expected.
10973 */
10974static char *
10975extract_cookie_value(char *hdr, const char *hdr_end,
10976 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010977 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010978{
10979 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10980 char *next;
10981
10982 /* we search at least a cookie name followed by an equal, and more
10983 * generally something like this :
10984 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10985 */
10986 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10987 /* Iterate through all cookies on this line */
10988
10989 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10990 att_beg++;
10991
10992 /* find att_end : this is the first character after the last non
10993 * space before the equal. It may be equal to hdr_end.
10994 */
10995 equal = att_end = att_beg;
10996
10997 while (equal < hdr_end) {
10998 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10999 break;
11000 if (http_is_spht[(unsigned char)*equal++])
11001 continue;
11002 att_end = equal;
11003 }
11004
11005 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11006 * is between <att_beg> and <equal>, both may be identical.
11007 */
11008
11009 /* look for end of cookie if there is an equal sign */
11010 if (equal < hdr_end && *equal == '=') {
11011 /* look for the beginning of the value */
11012 val_beg = equal + 1;
11013 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
11014 val_beg++;
11015
11016 /* find the end of the value, respecting quotes */
11017 next = find_cookie_value_end(val_beg, hdr_end);
11018
11019 /* make val_end point to the first white space or delimitor after the value */
11020 val_end = next;
11021 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
11022 val_end--;
11023 } else {
11024 val_beg = val_end = next = equal;
11025 }
11026
11027 /* We have nothing to do with attributes beginning with '$'. However,
11028 * they will automatically be removed if a header before them is removed,
11029 * since they're supposed to be linked together.
11030 */
11031 if (*att_beg == '$')
11032 continue;
11033
11034 /* Ignore cookies with no equal sign */
11035 if (equal == next)
11036 continue;
11037
11038 /* Now we have the cookie name between att_beg and att_end, and
11039 * its value between val_beg and val_end.
11040 */
11041
11042 if (att_end - att_beg == cookie_name_l &&
11043 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11044 /* let's return this value and indicate where to go on from */
11045 *value = val_beg;
11046 *value_l = val_end - val_beg;
11047 return next + 1;
11048 }
11049
11050 /* Set-Cookie headers only have the name in the first attr=value part */
11051 if (!list)
11052 break;
11053 }
11054
11055 return NULL;
11056}
11057
William Lallemanda43ba4e2014-01-28 18:14:25 +010011058/* Fetch a captured HTTP request header. The index is the position of
11059 * the "capture" option in the configuration file
11060 */
11061static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011062smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011063{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011064 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011065 int idx;
11066
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011067 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011068 return 0;
11069
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011070 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011071
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011072 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 +010011073 return 0;
11074
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011075 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011076 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011077 smp->data.u.str.str = smp->strm->req_cap[idx];
11078 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011079
11080 return 1;
11081}
11082
11083/* Fetch a captured HTTP response header. The index is the position of
11084 * the "capture" option in the configuration file
11085 */
11086static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011087smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011088{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011089 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011090 int idx;
11091
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011092 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011093 return 0;
11094
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011095 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011096
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011097 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 +010011098 return 0;
11099
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011100 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011101 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011102 smp->data.u.str.str = smp->strm->res_cap[idx];
11103 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011104
11105 return 1;
11106}
11107
William Lallemand65ad6e12014-01-31 15:08:02 +010011108/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11109static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011110smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011111{
11112 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011113 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011114 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011115
Willy Tarreau15e91e12015-04-04 00:52:09 +020011116 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011117 return 0;
11118
William Lallemand96a77852014-02-05 00:30:02 +010011119 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011120
William Lallemand96a77852014-02-05 00:30:02 +010011121 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11122 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011123
William Lallemand96a77852014-02-05 00:30:02 +010011124 temp = get_trash_chunk();
11125 temp->str = txn->uri;
11126 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011127 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011128 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011129 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011130
11131 return 1;
11132
11133}
11134
11135/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11136static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011137smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011138{
11139 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011140 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011141 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011142
Willy Tarreau15e91e12015-04-04 00:52:09 +020011143 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011144 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011145
William Lallemand65ad6e12014-01-31 15:08:02 +010011146 ptr = txn->uri;
11147
11148 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11149 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011150
William Lallemand65ad6e12014-01-31 15:08:02 +010011151 if (!*ptr)
11152 return 0;
11153
11154 ptr++; /* skip the space */
11155
11156 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011157 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011158 if (!ptr)
11159 return 0;
11160 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11161 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011162
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011163 smp->data.u.str = *temp;
11164 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011165 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011166 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011167
11168 return 1;
11169}
11170
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011171/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11172 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11173 */
11174static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011175smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011176{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011177 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011178
Willy Tarreau15e91e12015-04-04 00:52:09 +020011179 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011180 return 0;
11181
11182 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011183 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011184 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011185 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011186
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011187 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011188 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011189 smp->flags = SMP_F_CONST;
11190 return 1;
11191
11192}
11193
11194/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11195 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11196 */
11197static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011198smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011199{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011200 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011201
Willy Tarreau15e91e12015-04-04 00:52:09 +020011202 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011203 return 0;
11204
11205 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011206 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011207 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011208 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011209
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011210 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011211 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011212 smp->flags = SMP_F_CONST;
11213 return 1;
11214
11215}
11216
William Lallemand65ad6e12014-01-31 15:08:02 +010011217
Willy Tarreaue333ec92012-04-16 16:26:40 +020011218/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011219 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011220 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011221 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011222 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011223 * Accepts exactly 1 argument of type string. If the input options indicate
11224 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011225 * The returned sample is of type CSTR. Can be used to parse cookies in other
11226 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011227 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011228int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011229{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011230 struct http_txn *txn;
11231 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011232 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011233 const struct http_msg *msg;
11234 const char *hdr_name;
11235 int hdr_name_len;
11236 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011237 int occ = 0;
11238 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011239
Willy Tarreau24e32d82012-04-23 23:55:44 +020011240 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011241 return 0;
11242
Willy Tarreaua890d072013-04-02 12:01:06 +020011243 if (!ctx) {
11244 /* first call */
11245 ctx = &static_hdr_ctx;
11246 ctx->idx = 0;
11247 smp->ctx.a[2] = ctx;
11248 }
11249
Willy Tarreaue333ec92012-04-16 16:26:40 +020011250 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011251
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011252 txn = smp->strm->txn;
11253 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011254
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011255 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011256 msg = &txn->req;
11257 hdr_name = "Cookie";
11258 hdr_name_len = 6;
11259 } else {
11260 msg = &txn->rsp;
11261 hdr_name = "Set-Cookie";
11262 hdr_name_len = 10;
11263 }
11264
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011265 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011266 /* no explicit occurrence and single fetch => last cookie by default */
11267 occ = -1;
11268
11269 /* OK so basically here, either we want only one value and it's the
11270 * last one, or we want to iterate over all of them and we fetch the
11271 * next one.
11272 */
11273
Willy Tarreau9b28e032012-10-12 23:49:43 +020011274 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011275 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011276 /* search for the header from the beginning, we must first initialize
11277 * the search parameters.
11278 */
Willy Tarreau37406352012-04-23 16:16:37 +020011279 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011280 ctx->idx = 0;
11281 }
11282
Willy Tarreau28376d62012-04-26 21:26:10 +020011283 smp->flags |= SMP_F_VOL_HDR;
11284
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011285 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011286 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11287 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011288 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11289 goto out;
11290
Willy Tarreau24e32d82012-04-23 23:55:44 +020011291 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011292 continue;
11293
Willy Tarreau37406352012-04-23 16:16:37 +020011294 smp->ctx.a[0] = ctx->line + ctx->val;
11295 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011296 }
11297
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011298 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011299 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011300 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011301 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011302 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011303 &smp->data.u.str.str,
11304 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011305 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011306 found = 1;
11307 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011308 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011309 smp->flags |= SMP_F_NOT_LAST;
11310 return 1;
11311 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011312 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011313 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011314 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011315 /* all cookie headers and values were scanned. If we're looking for the
11316 * last occurrence, we may return it now.
11317 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011318 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011319 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011320 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011321}
11322
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011323/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011324 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011325 * multiple cookies may be parsed on the same line. The returned sample is of
11326 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011327 */
11328static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011329smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011330{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011331 struct http_txn *txn;
11332 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011333 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011334 const struct http_msg *msg;
11335 const char *hdr_name;
11336 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011337 int cnt;
11338 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011339 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011340
Willy Tarreau24e32d82012-04-23 23:55:44 +020011341 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011342 return 0;
11343
Willy Tarreaue333ec92012-04-16 16:26:40 +020011344 CHECK_HTTP_MESSAGE_FIRST();
11345
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011346 txn = smp->strm->txn;
11347 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011348
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011349 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011350 msg = &txn->req;
11351 hdr_name = "Cookie";
11352 hdr_name_len = 6;
11353 } else {
11354 msg = &txn->rsp;
11355 hdr_name = "Set-Cookie";
11356 hdr_name_len = 10;
11357 }
11358
Willy Tarreau9b28e032012-10-12 23:49:43 +020011359 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011360 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011361 ctx.idx = 0;
11362 cnt = 0;
11363
11364 while (1) {
11365 /* Note: val_beg == NULL every time we need to fetch a new header */
11366 if (!val_beg) {
11367 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11368 break;
11369
Willy Tarreau24e32d82012-04-23 23:55:44 +020011370 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011371 continue;
11372
11373 val_beg = ctx.line + ctx.val;
11374 val_end = val_beg + ctx.vlen;
11375 }
11376
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011377 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011378 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011379 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011380 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011381 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011382 &smp->data.u.str.str,
11383 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011384 cnt++;
11385 }
11386 }
11387
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011388 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011389 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011390 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011391 return 1;
11392}
11393
Willy Tarreau51539362012-05-08 12:46:28 +020011394/* Fetch an cookie's integer value. The integer value is returned. It
11395 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11396 */
11397static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011398smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011399{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011400 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011401
11402 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011403 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011404 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011405 }
11406
11407 return ret;
11408}
11409
Willy Tarreau8797c062007-05-07 00:55:35 +020011410/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011411/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011412/************************************************************************/
11413
David Cournapeau16023ee2010-12-23 20:55:41 +090011414/*
11415 * Given a path string and its length, find the position of beginning of the
11416 * query string. Returns NULL if no query string is found in the path.
11417 *
11418 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11419 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011420 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011421 */
bedis4c75cca2012-10-05 08:38:24 +020011422static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011423{
11424 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011425
bedis4c75cca2012-10-05 08:38:24 +020011426 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011427 return p ? p + 1 : NULL;
11428}
11429
bedis4c75cca2012-10-05 08:38:24 +020011430static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011431{
bedis4c75cca2012-10-05 08:38:24 +020011432 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011433}
11434
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011435/* after increasing a pointer value, it can exceed the first buffer
11436 * size. This function transform the value of <ptr> according with
11437 * the expected position. <chunks> is an array of the one or two
11438 * avalaible chunks. The first value is the start of the first chunk,
11439 * the second value if the end+1 of the first chunks. The third value
11440 * is NULL or the start of the second chunk and the fourth value is
11441 * the end+1 of the second chunk. The function returns 1 if does a
11442 * wrap, else returns 0.
11443 */
11444static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11445{
11446 if (*ptr < chunks[1])
11447 return 0;
11448 if (!chunks[2])
11449 return 0;
11450 *ptr = chunks[2] + ( *ptr - chunks[1] );
11451 return 1;
11452}
11453
David Cournapeau16023ee2010-12-23 20:55:41 +090011454/*
11455 * Given a url parameter, find the starting position of the first occurence,
11456 * or NULL if the parameter is not found.
11457 *
11458 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11459 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011460 *
11461 * Warning:this function returns a pointer that can be point to the first chunk
11462 * or the second chunk. The caller must be check the position before using the
11463 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011464 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011465static const char *
11466find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011467 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011468 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011469{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011470 const char *pos, *last, *equal;
11471 const char **bufs = chunks;
11472 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011473
David Cournapeau16023ee2010-12-23 20:55:41 +090011474
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011475 pos = bufs[0];
11476 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011477 while (pos <= last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011478 /* Check the equal. */
11479 equal = pos + url_param_name_l;
11480 if (fix_pointer_if_wrap(chunks, &equal)) {
11481 if (equal >= chunks[3])
11482 return NULL;
11483 } else {
11484 if (equal >= chunks[1])
11485 return NULL;
11486 }
11487 if (*equal == '=') {
11488 if (pos + url_param_name_l > last) {
11489 /* process wrap case, we detect a wrap. In this case, the
11490 * comparison is performed in two parts.
11491 */
11492
11493 /* This is the end, we dont have any other chunk. */
11494 if (bufs != chunks || !bufs[2])
11495 return NULL;
11496
11497 /* Compute the length of each part of the comparison. */
11498 l1 = last - pos;
11499 l2 = url_param_name_l - l1;
11500
11501 /* The second buffer is too short to contain the compared string. */
11502 if (bufs[2] + l2 > bufs[3])
11503 return NULL;
11504
11505 if (memcmp(pos, url_param_name, l1) == 0 &&
11506 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11507 return pos;
11508
11509 /* Perform wrapping and jump the string who fail the comparison. */
11510 bufs += 2;
11511 pos = bufs[0] + l2;
11512 last = bufs[1];
11513
11514 } else {
11515 /* process a simple comparison. */
11516 if (memcmp(pos, url_param_name, url_param_name_l) == 0) {
11517 return pos; }
11518 pos += url_param_name_l + 1;
11519 if (fix_pointer_if_wrap(chunks, &pos))
11520 last = bufs[2];
11521 }
11522 }
11523
11524 while (1) {
11525 /* Look for the next delimiter. */
11526 while (pos <= last && !is_param_delimiter(*pos, delim))
11527 pos++;
11528 if (pos < last)
11529 break;
11530 /* process buffer wrapping. */
11531 if (bufs != chunks || !bufs[2])
11532 return NULL;
11533 bufs += 2;
11534 pos = bufs[0];
11535 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011536 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011537 pos++;
11538 }
11539 return NULL;
11540}
11541
11542/*
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011543 * Given a url parameter name and a query string, returns its value and size
11544 * into *value and *value_l respectively, and returns non-zero. An empty
11545 * url_param_name matches the first available parameter. If the parameter is
11546 * not found, zero is returned and value/value_l are not touched.
David Cournapeau16023ee2010-12-23 20:55:41 +090011547 */
11548static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011549find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011550 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011551 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011552{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011553 const char *arg_start, *qs_end;
11554 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011555
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011556 arg_start = chunks[0];
11557 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011558 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011559 /* Looks for an argument name. */
11560 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011561 url_param_name, url_param_name_l,
11562 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011563 /* Check for wrapping. */
11564 if (arg_start > qs_end)
11565 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011566 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011567 if (!arg_start)
11568 return 0;
11569
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011570 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011571 while (1) {
11572 /* looks for the first argument. */
11573 value_start = memchr(arg_start, '=', qs_end - arg_start);
11574 if (!value_start) {
11575
11576 /* Check for wrapping. */
11577 if (arg_start >= chunks[0] &&
11578 arg_start <= chunks[1] &&
11579 chunks[2]) {
11580 arg_start = chunks[2];
11581 qs_end = chunks[3];
11582 continue;
11583 }
11584 return 0;
11585 }
11586 break;
11587 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011588 value_start++;
11589 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011590 else {
11591 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011592 value_start = arg_start + url_param_name_l + 1;
11593
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011594 /* Check for pointer wrapping. */
11595 if (fix_pointer_if_wrap(chunks, &value_start)) {
11596 /* Update the end pointer. */
11597 qs_end = chunks[3];
11598
11599 /* Check for overflow. */
11600 if (value_start > qs_end)
11601 return 0;
11602 }
11603 }
11604
David Cournapeau16023ee2010-12-23 20:55:41 +090011605 value_end = value_start;
11606
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011607 while (1) {
11608 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11609 value_end++;
11610 if (value_end < qs_end)
11611 break;
11612 /* process buffer wrapping. */
11613 if (value_end >= chunks[0] &&
11614 value_end <= chunks[1] &&
11615 chunks[2]) {
11616 value_end = chunks[2];
11617 qs_end = chunks[3];
11618 continue;
11619 }
11620 break;
11621 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011622
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011623 *vstart = value_start;
11624 *vend = value_end;
Willy Tarreau00134332011-01-04 14:57:34 +010011625 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011626}
11627
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011628/* This scans a URL-encoded query string. It takes an optionally wrapping
11629 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11630 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11631 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011632 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011633static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011634smp_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 +090011635{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011636 const char *vstart, *vend;
11637 struct chunk *temp;
11638 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011639
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011640 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011641 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011642 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011643 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011644 return 0;
11645
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011646 /* Create sample. If the value is contiguous, return the pointer as CONST,
11647 * if the value is wrapped, copy-it in a buffer.
11648 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011649 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011650 if (chunks[2] &&
11651 vstart >= chunks[0] && vstart <= chunks[1] &&
11652 vend >= chunks[2] && vend <= chunks[3]) {
11653 /* Wrapped case. */
11654 temp = get_trash_chunk();
11655 memcpy(temp->str, vstart, chunks[1] - vstart);
11656 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011657 smp->data.u.str.str = temp->str;
11658 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011659 } else {
11660 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011661 smp->data.u.str.str = (char *)vstart;
11662 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011663 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11664 }
11665
11666 /* Update context, check wrapping. */
11667 chunks[0] = vend;
11668 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11669 chunks[1] = chunks[3];
11670 chunks[2] = NULL;
11671 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011672
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011673 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011674 smp->flags |= SMP_F_NOT_LAST;
11675
David Cournapeau16023ee2010-12-23 20:55:41 +090011676 return 1;
11677}
11678
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011679/* This function iterates over each parameter of the query string. It uses
11680 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011681 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11682 * An optional parameter name is passed in args[0], otherwise any parameter is
11683 * considered. It supports an optional delimiter argument for the beginning of
11684 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011685 */
11686static int
11687smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11688{
11689 struct http_msg *msg;
11690 char delim = '?';
11691 const char *name;
11692 int name_len;
11693
Dragan Dosen26f77e52015-05-25 10:02:11 +020011694 if (!args ||
11695 (args[0].type && args[0].type != ARGT_STR) ||
11696 (args[1].type && args[1].type != ARGT_STR))
11697 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011698
Dragan Dosen26f77e52015-05-25 10:02:11 +020011699 name = "";
11700 name_len = 0;
11701 if (args->type == ARGT_STR) {
11702 name = args->data.str.str;
11703 name_len = args->data.str.len;
11704 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011705
Dragan Dosen26f77e52015-05-25 10:02:11 +020011706 if (args[1].type)
11707 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011708
Dragan Dosen26f77e52015-05-25 10:02:11 +020011709 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011710 CHECK_HTTP_MESSAGE_FIRST();
11711
11712 msg = &smp->strm->txn->req;
11713
11714 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11715 msg->sl.rq.u_l, delim);
11716 if (!smp->ctx.a[0])
11717 return 0;
11718
11719 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011720
11721 /* Assume that the context is filled with NULL pointer
11722 * before the first call.
11723 * smp->ctx.a[2] = NULL;
11724 * smp->ctx.a[3] = NULL;
11725 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011726 }
11727
11728 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11729}
11730
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011731/* This function iterates over each parameter of the body. This requires
11732 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011733 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11734 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11735 * optional second part if the body wraps at the end of the buffer. An optional
11736 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011737 */
11738static int
11739smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11740{
11741 struct http_txn *txn = smp->strm->txn;
11742 struct http_msg *msg;
11743 unsigned long len;
11744 unsigned long block1;
11745 char *body;
11746 const char *name;
11747 int name_len;
11748
11749 if (!args || (args[0].type && args[0].type != ARGT_STR))
11750 return 0;
11751
11752 name = "";
11753 name_len = 0;
11754 if (args[0].type == ARGT_STR) {
11755 name = args[0].data.str.str;
11756 name_len = args[0].data.str.len;
11757 }
11758
11759 if (!smp->ctx.a[0]) { // first call, find the query string
11760 CHECK_HTTP_MESSAGE_FIRST();
11761
11762 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
11763 msg = &txn->req;
11764 else
11765 msg = &txn->rsp;
11766
11767 len = http_body_bytes(msg);
11768 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11769
11770 block1 = len;
11771 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11772 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11773
11774 if (block1 == len) {
11775 /* buffer is not wrapped (or empty) */
11776 smp->ctx.a[0] = body;
11777 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011778
11779 /* Assume that the context is filled with NULL pointer
11780 * before the first call.
11781 * smp->ctx.a[2] = NULL;
11782 * smp->ctx.a[3] = NULL;
11783 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011784 }
11785 else {
11786 /* buffer is wrapped, we need to defragment it */
11787 smp->ctx.a[0] = body;
11788 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011789 smp->ctx.a[2] = msg->chn->buf->data;
11790 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011791 }
11792 }
11793 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11794}
11795
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011796/* Return the signed integer value for the specified url parameter (see url_param
11797 * above).
11798 */
11799static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011800smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011801{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011802 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011803
11804 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011805 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011806 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011807 }
11808
11809 return ret;
11810}
11811
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011812/* This produces a 32-bit hash of the concatenation of the first occurrence of
11813 * the Host header followed by the path component if it begins with a slash ('/').
11814 * This means that '*' will not be added, resulting in exactly the first Host
11815 * entry. If no Host header is found, then the path is used. The resulting value
11816 * is hashed using the url hash followed by a full avalanche hash and provides a
11817 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11818 * high-traffic sites without having to store whole paths.
11819 * this differs from the base32 functions in that it includes the url parameters
11820 * as well as the path
11821 */
11822static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011823smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011824{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011825 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011826 struct hdr_ctx ctx;
11827 unsigned int hash = 0;
11828 char *ptr, *beg, *end;
11829 int len;
11830
11831 CHECK_HTTP_MESSAGE_FIRST();
11832
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011833 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011834 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011835 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011836 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11837 ptr = ctx.line + ctx.val;
11838 len = ctx.vlen;
11839 while (len--)
11840 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11841 }
11842
11843 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011844 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 +000011845 beg = http_get_path(txn);
11846 if (!beg)
11847 beg = end;
11848
11849 for (ptr = beg; ptr < end ; ptr++);
11850
11851 if (beg < ptr && *beg == '/') {
11852 while (beg < ptr)
11853 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11854 }
11855 hash = full_hash(hash);
11856
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011857 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011858 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011859 smp->flags = SMP_F_VOL_1ST;
11860 return 1;
11861}
11862
11863/* This concatenates the source address with the 32-bit hash of the Host and
11864 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11865 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11866 * on the source address length. The URL hash is stored before the address so
11867 * that in environments where IPv6 is insignificant, truncating the output to
11868 * 8 bytes would still work.
11869 */
11870static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011871smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011872{
11873 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011874 struct connection *cli_conn = objt_conn(smp->sess->origin);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011875 unsigned int hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011876
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011877 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011878 return 0;
11879
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011880 /* The returned hash is a 32 bytes integer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011881 hash = smp->data.u.sint;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011882
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011883 temp = get_trash_chunk();
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011884 memcpy(temp->str + temp->len, &hash, sizeof(hash));
11885 temp->len += sizeof(hash);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011886
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011887 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011888 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011889 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011890 temp->len += 4;
11891 break;
11892 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011893 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011894 temp->len += 16;
11895 break;
11896 default:
11897 return 0;
11898 }
11899
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011900 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011901 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011902 return 1;
11903}
11904
Willy Tarreau185b5c42012-04-26 15:11:51 +020011905/* This function is used to validate the arguments passed to any "hdr" fetch
11906 * keyword. These keywords support an optional positive or negative occurrence
11907 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11908 * is assumed that the types are already the correct ones. Returns 0 on error,
11909 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11910 * error message in case of error, that the caller is responsible for freeing.
11911 * The initial location must either be freeable or NULL.
11912 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011913int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011914{
11915 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011916 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011917 return 0;
11918 }
11919 return 1;
11920}
11921
Willy Tarreau276fae92013-07-25 14:36:01 +020011922/* takes an UINT value on input supposed to represent the time since EPOCH,
11923 * adds an optional offset found in args[0] and emits a string representing
11924 * the date in RFC-1123/5322 format.
11925 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011926static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011927{
11928 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11929 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11930 struct chunk *temp;
11931 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011932 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011933 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011934
11935 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011936 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011937 curr_date += args[0].data.sint;
11938
11939 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011940 if (!tm)
11941 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011942
11943 temp = get_trash_chunk();
11944 temp->len = snprintf(temp->str, temp->size - temp->len,
11945 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11946 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11947 tm->tm_hour, tm->tm_min, tm->tm_sec);
11948
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011949 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011950 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011951 return 1;
11952}
11953
Thierry FOURNIERad903512014-04-11 17:51:01 +020011954/* Match language range with language tag. RFC2616 14.4:
11955 *
11956 * A language-range matches a language-tag if it exactly equals
11957 * the tag, or if it exactly equals a prefix of the tag such
11958 * that the first tag character following the prefix is "-".
11959 *
11960 * Return 1 if the strings match, else return 0.
11961 */
11962static inline int language_range_match(const char *range, int range_len,
11963 const char *tag, int tag_len)
11964{
11965 const char *end = range + range_len;
11966 const char *tend = tag + tag_len;
11967 while (range < end) {
11968 if (*range == '-' && tag == tend)
11969 return 1;
11970 if (*range != *tag || tag == tend)
11971 return 0;
11972 range++;
11973 tag++;
11974 }
11975 /* Return true only if the last char of the tag is matched. */
11976 return tag == tend;
11977}
11978
11979/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011980static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011981{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011982 const char *al = smp->data.u.str.str;
11983 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011984 const char *token;
11985 int toklen;
11986 int qvalue;
11987 const char *str;
11988 const char *w;
11989 int best_q = 0;
11990
11991 /* Set the constant to the sample, because the output of the
11992 * function will be peek in the constant configuration string.
11993 */
11994 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011995 smp->data.u.str.size = 0;
11996 smp->data.u.str.str = "";
11997 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011998
11999 /* Parse the accept language */
12000 while (1) {
12001
12002 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012003 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012004 al++;
12005 if (al >= end)
12006 break;
12007
12008 /* Start of the fisrt word. */
12009 token = al;
12010
12011 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012012 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012013 al++;
12014 if (al == token)
12015 goto expect_comma;
12016
12017 /* Length of the token. */
12018 toklen = al - token;
12019 qvalue = 1000;
12020
12021 /* Check if the token exists in the list. If the token not exists,
12022 * jump to the next token.
12023 */
12024 str = args[0].data.str.str;
12025 w = str;
12026 while (1) {
12027 if (*str == ';' || *str == '\0') {
12028 if (language_range_match(token, toklen, w, str-w))
12029 goto look_for_q;
12030 if (*str == '\0')
12031 goto expect_comma;
12032 w = str + 1;
12033 }
12034 str++;
12035 }
12036 goto expect_comma;
12037
12038look_for_q:
12039
12040 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012041 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012042 al++;
12043 if (al >= end)
12044 goto process_value;
12045
12046 /* If ',' is found, process the result */
12047 if (*al == ',')
12048 goto process_value;
12049
12050 /* If the character is different from ';', look
12051 * for the end of the header part in best effort.
12052 */
12053 if (*al != ';')
12054 goto expect_comma;
12055
12056 /* Assumes that the char is ';', now expect "q=". */
12057 al++;
12058
12059 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012060 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012061 al++;
12062 if (al >= end)
12063 goto process_value;
12064
12065 /* Expect 'q'. If no 'q', continue in best effort */
12066 if (*al != 'q')
12067 goto process_value;
12068 al++;
12069
12070 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012071 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012072 al++;
12073 if (al >= end)
12074 goto process_value;
12075
12076 /* Expect '='. If no '=', continue in best effort */
12077 if (*al != '=')
12078 goto process_value;
12079 al++;
12080
12081 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012082 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012083 al++;
12084 if (al >= end)
12085 goto process_value;
12086
12087 /* Parse the q value. */
12088 qvalue = parse_qvalue(al, &al);
12089
12090process_value:
12091
12092 /* If the new q value is the best q value, then store the associated
12093 * language in the response. If qvalue is the biggest value (1000),
12094 * break the process.
12095 */
12096 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012097 smp->data.u.str.str = (char *)w;
12098 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012099 if (qvalue >= 1000)
12100 break;
12101 best_q = qvalue;
12102 }
12103
12104expect_comma:
12105
12106 /* Expect comma or end. If the end is detected, quit the loop. */
12107 while (al < end && *al != ',')
12108 al++;
12109 if (al >= end)
12110 break;
12111
12112 /* Comma is found, jump it and restart the analyzer. */
12113 al++;
12114 }
12115
12116 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012117 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12118 smp->data.u.str.str = args[1].data.str.str;
12119 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012120 }
12121
12122 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012123 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012124}
12125
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012126/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012127static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012128{
12129 /* If the constant flag is set or if not size is avalaible at
12130 * the end of the buffer, copy the string in other buffer
12131 * before decoding.
12132 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012133 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012134 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012135 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12136 smp->data.u.str.str = str->str;
12137 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012138 smp->flags &= ~SMP_F_CONST;
12139 }
12140
12141 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012142 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12143 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012144 return 1;
12145}
12146
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012147static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12148{
12149 struct proxy *fe = strm_fe(smp->strm);
12150 int idx, i;
12151 struct cap_hdr *hdr;
12152 int len;
12153
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012154 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012155 return 0;
12156
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012157 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012158
12159 /* Check the availibity of the capture id. */
12160 if (idx > fe->nb_req_cap - 1)
12161 return 0;
12162
12163 /* Look for the original configuration. */
12164 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12165 hdr != NULL && i != idx ;
12166 i--, hdr = hdr->next);
12167 if (!hdr)
12168 return 0;
12169
12170 /* check for the memory allocation */
12171 if (smp->strm->req_cap[hdr->index] == NULL)
12172 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12173 if (smp->strm->req_cap[hdr->index] == NULL)
12174 return 0;
12175
12176 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012177 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012178 if (len > hdr->len)
12179 len = hdr->len;
12180
12181 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012182 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012183 smp->strm->req_cap[idx][len] = '\0';
12184
12185 return 1;
12186}
12187
12188static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12189{
12190 struct proxy *fe = strm_fe(smp->strm);
12191 int idx, i;
12192 struct cap_hdr *hdr;
12193 int len;
12194
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012195 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012196 return 0;
12197
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012198 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012199
12200 /* Check the availibity of the capture id. */
12201 if (idx > fe->nb_rsp_cap - 1)
12202 return 0;
12203
12204 /* Look for the original configuration. */
12205 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12206 hdr != NULL && i != idx ;
12207 i--, hdr = hdr->next);
12208 if (!hdr)
12209 return 0;
12210
12211 /* check for the memory allocation */
12212 if (smp->strm->res_cap[hdr->index] == NULL)
12213 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12214 if (smp->strm->res_cap[hdr->index] == NULL)
12215 return 0;
12216
12217 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012218 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012219 if (len > hdr->len)
12220 len = hdr->len;
12221
12222 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012223 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012224 smp->strm->res_cap[idx][len] = '\0';
12225
12226 return 1;
12227}
12228
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012229/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012230 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012231 * the relevant part of the request line accordingly. Then it updates various
12232 * pointers to the next elements which were moved, and the total buffer length.
12233 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012234 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12235 * error, though this can be revisited when this code is finally exploited.
12236 *
12237 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12238 * query string and 3 to replace uri.
12239 *
12240 * In query string case, the mark question '?' must be set at the start of the
12241 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012242 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012243int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012244 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012245{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012246 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012247 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012248 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012249 int delta;
12250
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012251 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012252 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012253 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012254 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12255
12256 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012257 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012258 txn->req.sl.rq.m_l += delta;
12259 txn->req.sl.rq.u += delta;
12260 txn->req.sl.rq.v += delta;
12261 break;
12262
12263 case 1: // path
12264 cur_ptr = http_get_path(txn);
12265 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012266 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012267
12268 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012269 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 +010012270 cur_end++;
12271
12272 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012273 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012274 txn->req.sl.rq.u_l += delta;
12275 txn->req.sl.rq.v += delta;
12276 break;
12277
12278 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012279 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012280 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012281 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12282 while (cur_ptr < cur_end && *cur_ptr != '?')
12283 cur_ptr++;
12284
12285 /* skip the question mark or indicate that we must insert it
12286 * (but only if the format string is not empty then).
12287 */
12288 if (cur_ptr < cur_end)
12289 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012290 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012291 offset = 0;
12292
12293 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012294 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012295 txn->req.sl.rq.u_l += delta;
12296 txn->req.sl.rq.v += delta;
12297 break;
12298
12299 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012300 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012301 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12302
12303 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012304 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012305 txn->req.sl.rq.u_l += delta;
12306 txn->req.sl.rq.v += delta;
12307 break;
12308
12309 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012310 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012311 }
12312
12313 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012314 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012315 txn->req.sl.rq.l += delta;
12316 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012317 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012318 return 0;
12319}
12320
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012321/* This function replace the HTTP status code and the associated message. The
12322 * variable <status> contains the new status code. This function never fails.
12323 */
12324void http_set_status(unsigned int status, struct stream *s)
12325{
12326 struct http_txn *txn = s->txn;
12327 char *cur_ptr, *cur_end;
12328 int delta;
12329 char *res;
12330 int c_l;
12331 const char *msg;
12332 int msg_len;
12333
12334 chunk_reset(&trash);
12335
12336 res = ultoa_o(status, trash.str, trash.size);
12337 c_l = res - trash.str;
12338
12339 trash.str[c_l] = ' ';
12340 trash.len = c_l + 1;
12341
12342 msg = get_reason(status);
12343 msg_len = strlen(msg);
12344
12345 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12346 trash.len += msg_len;
12347
12348 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12349 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12350
12351 /* commit changes and adjust message */
12352 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12353
12354 /* adjust res line offsets and lengths */
12355 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12356 txn->rsp.sl.st.c_l = c_l;
12357 txn->rsp.sl.st.r_l = msg_len;
12358
12359 delta = trash.len - (cur_end - cur_ptr);
12360 txn->rsp.sl.st.l += delta;
12361 txn->hdr_idx.v[0].len += delta;
12362 http_msg_move_end(&txn->rsp, delta);
12363}
12364
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012365/* This function executes one of the set-{method,path,query,uri} actions. It
12366 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012367 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012368 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012369 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12370 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012371 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012372enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
12373 struct session *sess, struct stream *s)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012374{
12375 chunk_reset(&trash);
12376
12377 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012378 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012379 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012380 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012381
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012382 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012383 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012384}
12385
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012386/* This function is just a compliant action wrapper for "set-status". */
12387enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
12388 struct session *sess, struct stream *s)
12389{
12390 http_set_status(rule->arg.status.code, s);
12391 return ACT_RET_CONT;
12392}
12393
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012394/* parse an http-request action among :
12395 * set-method
12396 * set-path
12397 * set-query
12398 * set-uri
12399 *
12400 * All of them accept a single argument of type string representing a log-format.
12401 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12402 * 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 +020012403 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012404 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012405enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12406 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012407{
12408 int cur_arg = *orig_arg;
12409
Thierry FOURNIER42148732015-09-02 17:17:33 +020012410 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012411
12412 switch (args[0][4]) {
12413 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012414 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012415 rule->action_ptr = http_action_set_req_line;
12416 break;
12417 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012418 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012419 rule->action_ptr = http_action_set_req_line;
12420 break;
12421 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012422 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012423 rule->action_ptr = http_action_set_req_line;
12424 break;
12425 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012426 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012427 rule->action_ptr = http_action_set_req_line;
12428 break;
12429 default:
12430 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012431 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012432 }
12433
12434 if (!*args[cur_arg] ||
12435 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12436 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012437 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012438 }
12439
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012440 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012441 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012442 parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012443 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12444 proxy->conf.args.file, proxy->conf.args.line);
12445
12446 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012447 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012448}
12449
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012450/* parse set-status action:
12451 * This action accepts a single argument of type int representing
12452 * an http status code. It returns ACT_RET_PRS_OK on success,
12453 * ACT_RET_PRS_ERR on error.
12454 */
12455enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12456 struct act_rule *rule, char **err)
12457{
12458 char *error;
12459
Thierry FOURNIER42148732015-09-02 17:17:33 +020012460 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012461 rule->action_ptr = action_http_set_status;
12462
12463 /* Check if an argument is available */
12464 if (!*args[*orig_arg]) {
12465 memprintf(err, "expects exactly 1 argument <status>");
12466 return ACT_RET_PRS_ERR;
12467 }
12468
12469 /* convert status code as integer */
12470 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12471 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12472 memprintf(err, "expects an integer status code between 100 and 999");
12473 return ACT_RET_PRS_ERR;
12474 }
12475
12476 (*orig_arg)++;
12477 return ACT_RET_PRS_OK;
12478}
12479
Willy Tarreaua9083d02015-05-08 15:27:59 +020012480/* This function executes the "capture" action. It executes a fetch expression,
12481 * turns the result into a string and puts it in a capture slot. It always
12482 * returns 1. If an error occurs the action is cancelled, but the rule
12483 * processing continues.
12484 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012485enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
12486 struct session *sess, struct stream *s)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012487{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012488 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012489 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012490 char **cap = s->req_cap;
12491 int len;
12492
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012493 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 +020012494 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012495 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012496
12497 if (cap[h->index] == NULL)
12498 cap[h->index] = pool_alloc2(h->pool);
12499
12500 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012501 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012502
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012503 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012504 if (len > h->len)
12505 len = h->len;
12506
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012507 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012508 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012509 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012510}
12511
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012512/* This function executes the "capture" action and store the result in a
12513 * capture slot if exists. It executes a fetch expression, turns the result
12514 * into a string and puts it in a capture slot. It always returns 1. If an
12515 * error occurs the action is cancelled, but the rule processing continues.
12516 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012517enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
12518 struct session *sess, struct stream *s)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012519{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012520 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012521 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012522 char **cap = s->req_cap;
12523 struct proxy *fe = strm_fe(s);
12524 int len;
12525 int i;
12526
12527 /* Look for the original configuration. */
12528 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012529 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012530 i--, h = h->next);
12531 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012532 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012533
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012534 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 +020012535 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012536 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012537
12538 if (cap[h->index] == NULL)
12539 cap[h->index] = pool_alloc2(h->pool);
12540
12541 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012542 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012543
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012544 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012545 if (len > h->len)
12546 len = h->len;
12547
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012548 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012549 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012550 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012551}
12552
Willy Tarreaua9083d02015-05-08 15:27:59 +020012553/* parse an "http-request capture" action. It takes a single argument which is
12554 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012555 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012556 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012557 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012558enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12559 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012560{
12561 struct sample_expr *expr;
12562 struct cap_hdr *hdr;
12563 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012564 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012565
12566 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12567 if (strcmp(args[cur_arg], "if") == 0 ||
12568 strcmp(args[cur_arg], "unless") == 0)
12569 break;
12570
12571 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012572 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012573 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012574 }
12575
Willy Tarreaua9083d02015-05-08 15:27:59 +020012576 cur_arg = *orig_arg;
12577 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12578 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012579 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012580
12581 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12582 memprintf(err,
12583 "fetch method '%s' extracts information from '%s', none of which is available here",
12584 args[cur_arg-1], sample_src_names(expr->fetch->use));
12585 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012586 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012587 }
12588
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012589 if (!args[cur_arg] || !*args[cur_arg]) {
12590 memprintf(err, "expects 'len or 'id'");
12591 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012592 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012593 }
12594
Willy Tarreaua9083d02015-05-08 15:27:59 +020012595 if (strcmp(args[cur_arg], "len") == 0) {
12596 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012597
12598 if (!(px->cap & PR_CAP_FE)) {
12599 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012600 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012601 }
12602
12603 proxy->conf.args.ctx = ARGC_CAP;
12604
Willy Tarreaua9083d02015-05-08 15:27:59 +020012605 if (!args[cur_arg]) {
12606 memprintf(err, "missing length value");
12607 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012608 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012609 }
12610 /* we copy the table name for now, it will be resolved later */
12611 len = atoi(args[cur_arg]);
12612 if (len <= 0) {
12613 memprintf(err, "length must be > 0");
12614 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012615 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012616 }
12617 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012618
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012619 if (!len) {
12620 memprintf(err, "a positive 'len' argument is mandatory");
12621 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012622 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012623 }
12624
12625 hdr = calloc(sizeof(struct cap_hdr), 1);
12626 hdr->next = px->req_cap;
12627 hdr->name = NULL; /* not a header capture */
12628 hdr->namelen = 0;
12629 hdr->len = len;
12630 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12631 hdr->index = px->nb_req_cap++;
12632
12633 px->req_cap = hdr;
12634 px->to_log |= LW_REQHDR;
12635
Thierry FOURNIER42148732015-09-02 17:17:33 +020012636 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012637 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012638 rule->arg.cap.expr = expr;
12639 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012640 }
12641
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012642 else if (strcmp(args[cur_arg], "id") == 0) {
12643 int id;
12644 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012645
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012646 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012647
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012648 if (!args[cur_arg]) {
12649 memprintf(err, "missing id value");
12650 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012651 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012652 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012653
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012654 id = strtol(args[cur_arg], &error, 10);
12655 if (*error != '\0') {
12656 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12657 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012658 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012659 }
12660 cur_arg++;
12661
12662 proxy->conf.args.ctx = ARGC_CAP;
12663
Thierry FOURNIER42148732015-09-02 17:17:33 +020012664 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012665 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012666 rule->arg.capid.expr = expr;
12667 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012668 }
12669
12670 else {
12671 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12672 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012673 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012674 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012675
12676 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012677 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012678}
12679
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012680/* This function executes the "capture" action and store the result in a
12681 * capture slot if exists. It executes a fetch expression, turns the result
12682 * into a string and puts it in a capture slot. It always returns 1. If an
12683 * error occurs the action is cancelled, but the rule processing continues.
12684 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012685enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
12686 struct session *sess, struct stream *s)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012687{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012688 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012689 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012690 char **cap = s->res_cap;
12691 struct proxy *fe = strm_fe(s);
12692 int len;
12693 int i;
12694
12695 /* Look for the original configuration. */
12696 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012697 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012698 i--, h = h->next);
12699 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012700 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012701
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012702 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 +020012703 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012704 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012705
12706 if (cap[h->index] == NULL)
12707 cap[h->index] = pool_alloc2(h->pool);
12708
12709 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012710 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012711
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012712 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012713 if (len > h->len)
12714 len = h->len;
12715
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012716 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012717 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012718 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012719}
12720
12721/* parse an "http-response capture" action. It takes a single argument which is
12722 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12723 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012724 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012725 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012726enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12727 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012728{
12729 struct sample_expr *expr;
12730 int cur_arg;
12731 int id;
12732 char *error;
12733
12734 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12735 if (strcmp(args[cur_arg], "if") == 0 ||
12736 strcmp(args[cur_arg], "unless") == 0)
12737 break;
12738
12739 if (cur_arg < *orig_arg + 3) {
12740 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012741 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012742 }
12743
12744 cur_arg = *orig_arg;
12745 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12746 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012747 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012748
12749 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12750 memprintf(err,
12751 "fetch method '%s' extracts information from '%s', none of which is available here",
12752 args[cur_arg-1], sample_src_names(expr->fetch->use));
12753 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012754 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012755 }
12756
12757 if (!args[cur_arg] || !*args[cur_arg]) {
12758 memprintf(err, "expects 'len or 'id'");
12759 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012760 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012761 }
12762
12763 if (strcmp(args[cur_arg], "id") != 0) {
12764 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12765 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012766 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012767 }
12768
12769 cur_arg++;
12770
12771 if (!args[cur_arg]) {
12772 memprintf(err, "missing id value");
12773 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012774 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012775 }
12776
12777 id = strtol(args[cur_arg], &error, 10);
12778 if (*error != '\0') {
12779 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12780 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012781 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012782 }
12783 cur_arg++;
12784
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012785 proxy->conf.args.ctx = ARGC_CAP;
12786
Thierry FOURNIER42148732015-09-02 17:17:33 +020012787 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012788 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012789 rule->arg.capid.expr = expr;
12790 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012791
12792 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012793 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012794}
12795
William Lallemand73025dd2014-04-24 14:38:37 +020012796/*
12797 * Return the struct http_req_action_kw associated to a keyword.
12798 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012799struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012800{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012801 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012802}
12803
12804/*
12805 * Return the struct http_res_action_kw associated to a keyword.
12806 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012807struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012808{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012809 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012810}
12811
Willy Tarreau4a568972010-05-12 08:08:50 +020012812/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012813/* All supported ACL keywords must be declared here. */
12814/************************************************************************/
12815
12816/* Note: must not be declared <const> as its list will be overwritten.
12817 * Please take care of keeping this list alphabetically sorted.
12818 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012819static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012820 { "base", "base", PAT_MATCH_STR },
12821 { "base_beg", "base", PAT_MATCH_BEG },
12822 { "base_dir", "base", PAT_MATCH_DIR },
12823 { "base_dom", "base", PAT_MATCH_DOM },
12824 { "base_end", "base", PAT_MATCH_END },
12825 { "base_len", "base", PAT_MATCH_LEN },
12826 { "base_reg", "base", PAT_MATCH_REG },
12827 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012828
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012829 { "cook", "req.cook", PAT_MATCH_STR },
12830 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12831 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12832 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12833 { "cook_end", "req.cook", PAT_MATCH_END },
12834 { "cook_len", "req.cook", PAT_MATCH_LEN },
12835 { "cook_reg", "req.cook", PAT_MATCH_REG },
12836 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012837
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012838 { "hdr", "req.hdr", PAT_MATCH_STR },
12839 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12840 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12841 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12842 { "hdr_end", "req.hdr", PAT_MATCH_END },
12843 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12844 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12845 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012846
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012847 /* these two declarations uses strings with list storage (in place
12848 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12849 * and delete functions are relative to the list management. The parse
12850 * and match method are related to the corresponding fetch methods. This
12851 * is very particular ACL declaration mode.
12852 */
12853 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12854 { "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 +020012855
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012856 { "path", "path", PAT_MATCH_STR },
12857 { "path_beg", "path", PAT_MATCH_BEG },
12858 { "path_dir", "path", PAT_MATCH_DIR },
12859 { "path_dom", "path", PAT_MATCH_DOM },
12860 { "path_end", "path", PAT_MATCH_END },
12861 { "path_len", "path", PAT_MATCH_LEN },
12862 { "path_reg", "path", PAT_MATCH_REG },
12863 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012864
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012865 { "req_ver", "req.ver", PAT_MATCH_STR },
12866 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012867
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012868 { "scook", "res.cook", PAT_MATCH_STR },
12869 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12870 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12871 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12872 { "scook_end", "res.cook", PAT_MATCH_END },
12873 { "scook_len", "res.cook", PAT_MATCH_LEN },
12874 { "scook_reg", "res.cook", PAT_MATCH_REG },
12875 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012876
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012877 { "shdr", "res.hdr", PAT_MATCH_STR },
12878 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12879 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12880 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12881 { "shdr_end", "res.hdr", PAT_MATCH_END },
12882 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12883 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12884 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012885
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012886 { "url", "url", PAT_MATCH_STR },
12887 { "url_beg", "url", PAT_MATCH_BEG },
12888 { "url_dir", "url", PAT_MATCH_DIR },
12889 { "url_dom", "url", PAT_MATCH_DOM },
12890 { "url_end", "url", PAT_MATCH_END },
12891 { "url_len", "url", PAT_MATCH_LEN },
12892 { "url_reg", "url", PAT_MATCH_REG },
12893 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012894
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012895 { "urlp", "urlp", PAT_MATCH_STR },
12896 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12897 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12898 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12899 { "urlp_end", "urlp", PAT_MATCH_END },
12900 { "urlp_len", "urlp", PAT_MATCH_LEN },
12901 { "urlp_reg", "urlp", PAT_MATCH_REG },
12902 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012903
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012904 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012905}};
12906
12907/************************************************************************/
12908/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012909/************************************************************************/
12910/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012911static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012912 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012913 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012914 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12915
Willy Tarreau87b09662015-04-03 00:22:06 +020012916 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012917 { "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 +020012918
12919 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012920 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12921 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12922 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012923
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012924 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12925 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012926
Willy Tarreau409bcde2013-01-08 00:31:00 +010012927 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12928 * are only here to match the ACL's name, are request-only and are used
12929 * for ACL compatibility only.
12930 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012931 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12932 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012933 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12934 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012935
12936 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12937 * only here to match the ACL's name, are request-only and are used for
12938 * ACL compatibility only.
12939 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012940 { "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 +020012941 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012942 { "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 +020012943 { "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 +010012944
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012945 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012946 { "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 +010012947 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012948 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012949 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012950 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012951
12952 /* HTTP protocol on the request path */
12953 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012954 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012955
12956 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012957 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12958 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012959
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012960 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012961 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12962 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012963 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012964
Willy Tarreau18ed2562013-01-14 15:56:36 +010012965 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012966 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12967 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012968
Willy Tarreau18ed2562013-01-14 15:56:36 +010012969 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012970 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012971 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12972 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012973
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012974 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012975 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012976 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012977 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012978 { "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 +010012979 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012980 { "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 +010012981
12982 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012983 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012984 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12985 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012986
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012987 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012988 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012989 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012990 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012991 { "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 +010012992 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012993 { "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 +010012994
Willy Tarreau409bcde2013-01-08 00:31:00 +010012995 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012996 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012997 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12998 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012999 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013000
13001 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013002 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013003 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013004 { "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 +020013005 { "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 +010013006
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013007 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013008 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013009 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013010 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013011 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013012 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013013 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13014 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013015 { "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 +010013016 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013017}};
13018
Willy Tarreau8797c062007-05-07 00:55:35 +020013019
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013020/************************************************************************/
13021/* All supported converter keywords must be declared here. */
13022/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013023/* Note: must not be declared <const> as its list will be overwritten */
13024static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013025 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013026 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013027 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13028 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013029 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013030 { NULL, NULL, 0, 0, 0 },
13031}};
13032
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013033
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013034/************************************************************************/
13035/* All supported http-request action keywords must be declared here. */
13036/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013037struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013038 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013039 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013040 { "set-method", parse_set_req_line },
13041 { "set-path", parse_set_req_line },
13042 { "set-query", parse_set_req_line },
13043 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013044 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013045 }
13046};
13047
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013048struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013049 .kw = {
13050 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013051 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013052 { NULL, NULL }
13053 }
13054};
13055
Willy Tarreau8797c062007-05-07 00:55:35 +020013056__attribute__((constructor))
13057static void __http_protocol_init(void)
13058{
13059 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013060 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013061 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013062 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013063 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020013064}
13065
13066
Willy Tarreau58f10d72006-12-04 02:26:12 +010013067/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013068 * Local variables:
13069 * c-indent-level: 8
13070 * c-basic-offset: 8
13071 * End:
13072 */