blob: 0f18c009686a08aff990d9eacd8ca9f5ecaa61f7 [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 :
469 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
470 */
471};
472
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100473const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
474 [HTTP_METH_NONE] = { "", 0 },
475 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
476 [HTTP_METH_GET] = { "GET", 3 },
477 [HTTP_METH_HEAD] = { "HEAD", 4 },
478 [HTTP_METH_POST] = { "POST", 4 },
479 [HTTP_METH_PUT] = { "PUT", 3 },
480 [HTTP_METH_DELETE] = { "DELETE", 6 },
481 [HTTP_METH_TRACE] = { "TRACE", 5 },
482 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
483};
484
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100485/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200486 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100487 * RFC2616 for those chars.
488 */
489
490const char http_is_spht[256] = {
491 [' '] = 1, ['\t'] = 1,
492};
493
494const char http_is_crlf[256] = {
495 ['\r'] = 1, ['\n'] = 1,
496};
497
498const char http_is_lws[256] = {
499 [' '] = 1, ['\t'] = 1,
500 ['\r'] = 1, ['\n'] = 1,
501};
502
503const char http_is_sep[256] = {
504 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
505 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
506 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
507 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
508 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
509};
510
511const char http_is_ctl[256] = {
512 [0 ... 31] = 1,
513 [127] = 1,
514};
515
516/*
517 * A token is any ASCII char that is neither a separator nor a CTL char.
518 * Do not overwrite values in assignment since gcc-2.95 will not handle
519 * them correctly. Instead, define every non-CTL char's status.
520 */
521const char http_is_token[256] = {
522 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
523 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
524 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
525 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
526 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
527 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
528 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
529 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
530 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
531 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
532 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
533 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
534 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
535 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
536 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
537 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
538 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
539 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
540 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
541 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
542 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
543 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
544 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
545 ['|'] = 1, ['}'] = 0, ['~'] = 1,
546};
547
548
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100549/*
550 * An http ver_token is any ASCII which can be found in an HTTP version,
551 * which includes 'H', 'T', 'P', '/', '.' and any digit.
552 */
553const char http_is_ver_token[256] = {
554 ['.'] = 1, ['/'] = 1,
555 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
556 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
Thierry FOURNIER63d692c2015-02-28 19:03:56 +0100557 ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1,
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100558};
559
560
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100561/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100562 * Adds a header and its CRLF at the tail of the message's buffer, just before
563 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100564 * The header is also automatically added to the index <hdr_idx>, and the end
565 * of headers is automatically adjusted. The number of bytes added is returned
566 * on success, otherwise <0 is returned indicating an error.
567 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100568int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100569{
570 int bytes, len;
571
572 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200573 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100574 if (!bytes)
575 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100576 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100577 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
578}
579
580/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100581 * Adds a header and its CRLF at the tail of the message's buffer, just before
582 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100583 * the buffer is only opened and the space reserved, but nothing is copied.
584 * The header is also automatically added to the index <hdr_idx>, and the end
585 * of headers is automatically adjusted. The number of bytes added is returned
586 * on success, otherwise <0 is returned indicating an error.
587 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100588int http_header_add_tail2(struct http_msg *msg,
589 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100590{
591 int bytes;
592
Willy Tarreau9b28e032012-10-12 23:49:43 +0200593 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100594 if (!bytes)
595 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100596 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100597 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
598}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200599
600/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100601 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
602 * If so, returns the position of the first non-space character relative to
603 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
604 * to return a pointer to the place after the first space. Returns 0 if the
605 * header name does not match. Checks are case-insensitive.
606 */
607int http_header_match2(const char *hdr, const char *end,
608 const char *name, int len)
609{
610 const char *val;
611
612 if (hdr + len >= end)
613 return 0;
614 if (hdr[len] != ':')
615 return 0;
616 if (strncasecmp(hdr, name, len) != 0)
617 return 0;
618 val = hdr + len + 1;
619 while (val < end && HTTP_IS_SPHT(*val))
620 val++;
621 if ((val >= end) && (len + 2 <= end - hdr))
622 return len + 2; /* we may replace starting from second space */
623 return val - hdr;
624}
625
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200626/* Find the first or next occurrence of header <name> in message buffer <sol>
627 * using headers index <idx>, and return it in the <ctx> structure. This
628 * structure holds everything necessary to use the header and find next
629 * occurrence. If its <idx> member is 0, the header is searched from the
630 * beginning. Otherwise, the next occurrence is returned. The function returns
631 * 1 when it finds a value, and 0 when there is no more. It is very similar to
632 * http_find_header2() except that it is designed to work with full-line headers
633 * whose comma is not a delimiter but is part of the syntax. As a special case,
634 * if ctx->val is NULL when searching for a new values of a header, the current
635 * header is rescanned. This allows rescanning after a header deletion.
636 */
637int http_find_full_header2(const char *name, int len,
638 char *sol, struct hdr_idx *idx,
639 struct hdr_ctx *ctx)
640{
641 char *eol, *sov;
642 int cur_idx, old_idx;
643
644 cur_idx = ctx->idx;
645 if (cur_idx) {
646 /* We have previously returned a header, let's search another one */
647 sol = ctx->line;
648 eol = sol + idx->v[cur_idx].len;
649 goto next_hdr;
650 }
651
652 /* first request for this header */
653 sol += hdr_idx_first_pos(idx);
654 old_idx = 0;
655 cur_idx = hdr_idx_first_idx(idx);
656 while (cur_idx) {
657 eol = sol + idx->v[cur_idx].len;
658
659 if (len == 0) {
660 /* No argument was passed, we want any header.
661 * To achieve this, we simply build a fake request. */
662 while (sol + len < eol && sol[len] != ':')
663 len++;
664 name = sol;
665 }
666
667 if ((len < eol - sol) &&
668 (sol[len] == ':') &&
669 (strncasecmp(sol, name, len) == 0)) {
670 ctx->del = len;
671 sov = sol + len + 1;
672 while (sov < eol && http_is_lws[(unsigned char)*sov])
673 sov++;
674
675 ctx->line = sol;
676 ctx->prev = old_idx;
677 ctx->idx = cur_idx;
678 ctx->val = sov - sol;
679 ctx->tws = 0;
680 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
681 eol--;
682 ctx->tws++;
683 }
684 ctx->vlen = eol - sov;
685 return 1;
686 }
687 next_hdr:
688 sol = eol + idx->v[cur_idx].cr + 1;
689 old_idx = cur_idx;
690 cur_idx = idx->v[cur_idx].next;
691 }
692 return 0;
693}
694
Willy Tarreauc90dc232015-02-20 13:51:36 +0100695/* Find the first or next header field in message buffer <sol> using headers
696 * index <idx>, and return it in the <ctx> structure. This structure holds
697 * everything necessary to use the header and find next occurrence. If its
698 * <idx> member is 0, the first header is retrieved. Otherwise, the next
699 * occurrence is returned. The function returns 1 when it finds a value, and
700 * 0 when there is no more. It is equivalent to http_find_full_header2() with
701 * no header name.
702 */
703int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
704{
705 char *eol, *sov;
706 int cur_idx, old_idx;
707 int len;
708
709 cur_idx = ctx->idx;
710 if (cur_idx) {
711 /* We have previously returned a header, let's search another one */
712 sol = ctx->line;
713 eol = sol + idx->v[cur_idx].len;
714 goto next_hdr;
715 }
716
717 /* first request for this header */
718 sol += hdr_idx_first_pos(idx);
719 old_idx = 0;
720 cur_idx = hdr_idx_first_idx(idx);
721 while (cur_idx) {
722 eol = sol + idx->v[cur_idx].len;
723
724 len = 0;
725 while (1) {
726 if (len >= eol - sol)
727 goto next_hdr;
728 if (sol[len] == ':')
729 break;
730 len++;
731 }
732
733 ctx->del = len;
734 sov = sol + len + 1;
735 while (sov < eol && http_is_lws[(unsigned char)*sov])
736 sov++;
737
738 ctx->line = sol;
739 ctx->prev = old_idx;
740 ctx->idx = cur_idx;
741 ctx->val = sov - sol;
742 ctx->tws = 0;
743
744 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
745 eol--;
746 ctx->tws++;
747 }
748 ctx->vlen = eol - sov;
749 return 1;
750
751 next_hdr:
752 sol = eol + idx->v[cur_idx].cr + 1;
753 old_idx = cur_idx;
754 cur_idx = idx->v[cur_idx].next;
755 }
756 return 0;
757}
758
Willy Tarreau68085d82010-01-18 14:54:04 +0100759/* Find the end of the header value contained between <s> and <e>. See RFC2616,
760 * par 2.2 for more information. Note that it requires a valid header to return
761 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200762 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100763char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200764{
765 int quoted, qdpair;
766
767 quoted = qdpair = 0;
768 for (; s < e; s++) {
769 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200770 else if (quoted) {
771 if (*s == '\\') qdpair = 1;
772 else if (*s == '"') quoted = 0;
773 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200774 else if (*s == '"') quoted = 1;
775 else if (*s == ',') return s;
776 }
777 return s;
778}
779
780/* Find the first or next occurrence of header <name> in message buffer <sol>
781 * using headers index <idx>, and return it in the <ctx> structure. This
782 * structure holds everything necessary to use the header and find next
783 * occurrence. If its <idx> member is 0, the header is searched from the
784 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100785 * 1 when it finds a value, and 0 when there is no more. It is designed to work
786 * with headers defined as comma-separated lists. As a special case, if ctx->val
787 * is NULL when searching for a new values of a header, the current header is
788 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200789 */
790int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100791 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200792 struct hdr_ctx *ctx)
793{
Willy Tarreau68085d82010-01-18 14:54:04 +0100794 char *eol, *sov;
795 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200796
Willy Tarreau68085d82010-01-18 14:54:04 +0100797 cur_idx = ctx->idx;
798 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200799 /* We have previously returned a value, let's search
800 * another one on the same line.
801 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200802 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200803 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100804 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200805 eol = sol + idx->v[cur_idx].len;
806
807 if (sov >= eol)
808 /* no more values in this header */
809 goto next_hdr;
810
Willy Tarreau68085d82010-01-18 14:54:04 +0100811 /* values remaining for this header, skip the comma but save it
812 * for later use (eg: for header deletion).
813 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200814 sov++;
815 while (sov < eol && http_is_lws[(unsigned char)*sov])
816 sov++;
817
818 goto return_hdr;
819 }
820
821 /* first request for this header */
822 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100823 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200824 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200825 while (cur_idx) {
826 eol = sol + idx->v[cur_idx].len;
827
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200828 if (len == 0) {
829 /* No argument was passed, we want any header.
830 * To achieve this, we simply build a fake request. */
831 while (sol + len < eol && sol[len] != ':')
832 len++;
833 name = sol;
834 }
835
Willy Tarreau33a7e692007-06-10 19:45:56 +0200836 if ((len < eol - sol) &&
837 (sol[len] == ':') &&
838 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100839 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200840 sov = sol + len + 1;
841 while (sov < eol && http_is_lws[(unsigned char)*sov])
842 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100843
Willy Tarreau33a7e692007-06-10 19:45:56 +0200844 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100845 ctx->prev = old_idx;
846 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200847 ctx->idx = cur_idx;
848 ctx->val = sov - sol;
849
850 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200851 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200852 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200853 eol--;
854 ctx->tws++;
855 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200856 ctx->vlen = eol - sov;
857 return 1;
858 }
859 next_hdr:
860 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100861 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200862 cur_idx = idx->v[cur_idx].next;
863 }
864 return 0;
865}
866
867int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100868 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200869 struct hdr_ctx *ctx)
870{
871 return http_find_header2(name, strlen(name), sol, idx, ctx);
872}
873
Willy Tarreau68085d82010-01-18 14:54:04 +0100874/* Remove one value of a header. This only works on a <ctx> returned by one of
875 * the http_find_header functions. The value is removed, as well as surrounding
876 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100877 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100878 * message <msg>. The new index is returned. If it is zero, it means there is
879 * no more header, so any processing may stop. The ctx is always left in a form
880 * that can be handled by http_find_header2() to find next occurrence.
881 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100882int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100883{
884 int cur_idx = ctx->idx;
885 char *sol = ctx->line;
886 struct hdr_idx_elem *hdr;
887 int delta, skip_comma;
888
889 if (!cur_idx)
890 return 0;
891
892 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200893 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100894 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200895 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100896 http_msg_move_end(msg, delta);
897 idx->used--;
898 hdr->len = 0; /* unused entry */
899 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100900 if (idx->tail == ctx->idx)
901 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100902 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100903 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100904 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200905 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100906 return ctx->idx;
907 }
908
909 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200910 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
911 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100912 */
913
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200914 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200915 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200916 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100917 NULL, 0);
918 hdr->len += delta;
919 http_msg_move_end(msg, delta);
920 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200921 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100922 return ctx->idx;
923}
924
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100925/* This function handles a server error at the stream interface level. The
926 * stream interface is assumed to be already in a closed state. An optional
927 * message is copied into the input buffer, and an HTTP status code stored.
928 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100929 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200930 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200931static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100932 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200933{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100934 channel_auto_read(si_oc(si));
935 channel_abort(si_oc(si));
936 channel_auto_close(si_oc(si));
937 channel_erase(si_oc(si));
938 channel_auto_close(si_ic(si));
939 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100940 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200941 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100942 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200943 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200944 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200945 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200946 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200947 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200948}
949
Willy Tarreau87b09662015-04-03 00:22:06 +0200950/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100951 * and message.
952 */
953
Willy Tarreau87b09662015-04-03 00:22:06 +0200954struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100955{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200956 if (s->be->errmsg[msgnum].str)
957 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200958 else if (strm_fe(s)->errmsg[msgnum].str)
959 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100960 else
961 return &http_err_chunks[msgnum];
962}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200963
Willy Tarreau53b6c742006-12-17 13:37:46 +0100964/*
965 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
966 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
967 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100968enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100969{
970 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100971 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100972
973 m = ((unsigned)*str - 'A');
974
975 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100976 for (h = http_methods[m]; h->len > 0; h++) {
977 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100978 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100979 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100980 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100981 };
982 return HTTP_METH_OTHER;
983 }
984 return HTTP_METH_NONE;
985
986}
987
Willy Tarreau21d2af32008-02-14 20:25:24 +0100988/* Parse the URI from the given transaction (which is assumed to be in request
989 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
990 * It is returned otherwise.
991 */
992static char *
993http_get_path(struct http_txn *txn)
994{
995 char *ptr, *end;
996
Willy Tarreau9b28e032012-10-12 23:49:43 +0200997 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100998 end = ptr + txn->req.sl.rq.u_l;
999
1000 if (ptr >= end)
1001 return NULL;
1002
1003 /* RFC2616, par. 5.1.2 :
1004 * Request-URI = "*" | absuri | abspath | authority
1005 */
1006
1007 if (*ptr == '*')
1008 return NULL;
1009
1010 if (isalpha((unsigned char)*ptr)) {
1011 /* this is a scheme as described by RFC3986, par. 3.1 */
1012 ptr++;
1013 while (ptr < end &&
1014 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1015 ptr++;
1016 /* skip '://' */
1017 if (ptr == end || *ptr++ != ':')
1018 return NULL;
1019 if (ptr == end || *ptr++ != '/')
1020 return NULL;
1021 if (ptr == end || *ptr++ != '/')
1022 return NULL;
1023 }
1024 /* skip [user[:passwd]@]host[:[port]] */
1025
1026 while (ptr < end && *ptr != '/')
1027 ptr++;
1028
1029 if (ptr == end)
1030 return NULL;
1031
1032 /* OK, we got the '/' ! */
1033 return ptr;
1034}
1035
William Lallemand65ad6e12014-01-31 15:08:02 +01001036/* Parse the URI from the given string and look for the "/" beginning the PATH.
1037 * If not found, return NULL. It is returned otherwise.
1038 */
1039static char *
1040http_get_path_from_string(char *str)
1041{
1042 char *ptr = str;
1043
1044 /* RFC2616, par. 5.1.2 :
1045 * Request-URI = "*" | absuri | abspath | authority
1046 */
1047
1048 if (*ptr == '*')
1049 return NULL;
1050
1051 if (isalpha((unsigned char)*ptr)) {
1052 /* this is a scheme as described by RFC3986, par. 3.1 */
1053 ptr++;
1054 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1055 ptr++;
1056 /* skip '://' */
1057 if (*ptr == '\0' || *ptr++ != ':')
1058 return NULL;
1059 if (*ptr == '\0' || *ptr++ != '/')
1060 return NULL;
1061 if (*ptr == '\0' || *ptr++ != '/')
1062 return NULL;
1063 }
1064 /* skip [user[:passwd]@]host[:[port]] */
1065
1066 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1067 ptr++;
1068
1069 if (*ptr == '\0' || *ptr == ' ')
1070 return NULL;
1071
1072 /* OK, we got the '/' ! */
1073 return ptr;
1074}
1075
Willy Tarreau71241ab2012-12-27 11:30:54 +01001076/* Returns a 302 for a redirectable request that reaches a server working in
1077 * in redirect mode. This may only be called just after the stream interface
1078 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1079 * follow normal proxy processing. NOTE: this function is designed to support
1080 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001081 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001082void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001083{
1084 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001085 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001086 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001087 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001088
1089 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001090 trash.len = strlen(HTTP_302);
1091 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001092
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001093 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001094
Willy Tarreauefb453c2008-10-26 20:49:47 +01001095 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001096 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001097 return;
1098
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001099 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001100 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001101 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1102 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001103 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001104
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001105 /* 3: add the request URI. Since it was already forwarded, we need
1106 * to temporarily rewind the buffer.
1107 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001108 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001109 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001110
Willy Tarreauefb453c2008-10-26 20:49:47 +01001111 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001112 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 +02001113
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001114 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001115
Willy Tarreauefb453c2008-10-26 20:49:47 +01001116 if (!path)
1117 return;
1118
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001119 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001120 return;
1121
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001122 memcpy(trash.str + trash.len, path, len);
1123 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001124
1125 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001126 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1127 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001128 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001129 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1130 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001131 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001132
1133 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001134 si_shutr(si);
1135 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001136 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001137 si->state = SI_ST_CLO;
1138
1139 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001140 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001141
1142 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001143 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001144 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001145}
1146
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001147/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001148 * that the server side is closed. Note that err_type is actually a
1149 * bitmask, where almost only aborts may be cumulated with other
1150 * values. We consider that aborted operations are more important
1151 * than timeouts or errors due to the fact that nobody else in the
1152 * logs might explain incomplete retries. All others should avoid
1153 * being cumulated. It should normally not be possible to have multiple
1154 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001155 * Note that connection errors appearing on the second request of a keep-alive
1156 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001157 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001158void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001159{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001160 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001161
1162 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001163 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001164 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001165 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001166 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001167 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001168 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001169 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001170 http_server_error(s, si, SF_ERR_SRVTO, 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_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001173 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001174 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001175 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001176 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001177 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001178 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001179 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001180 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1181 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001182 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001183 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001184 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001185 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001186 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001187 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001188 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001189 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001190}
1191
Willy Tarreau42250582007-04-01 01:30:43 +02001192extern const char sess_term_cond[8];
1193extern const char sess_fin_state[8];
1194extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001195struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001196struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001197struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001198struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001199
Willy Tarreau117f59e2007-03-04 18:17:17 +01001200/*
1201 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001202 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001203 */
1204void capture_headers(char *som, struct hdr_idx *idx,
1205 char **cap, struct cap_hdr *cap_hdr)
1206{
1207 char *eol, *sol, *col, *sov;
1208 int cur_idx;
1209 struct cap_hdr *h;
1210 int len;
1211
1212 sol = som + hdr_idx_first_pos(idx);
1213 cur_idx = hdr_idx_first_idx(idx);
1214
1215 while (cur_idx) {
1216 eol = sol + idx->v[cur_idx].len;
1217
1218 col = sol;
1219 while (col < eol && *col != ':')
1220 col++;
1221
1222 sov = col + 1;
1223 while (sov < eol && http_is_lws[(unsigned char)*sov])
1224 sov++;
1225
1226 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001227 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001228 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1229 if (cap[h->index] == NULL)
1230 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001231 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001232
1233 if (cap[h->index] == NULL) {
1234 Alert("HTTP capture : out of memory.\n");
1235 continue;
1236 }
1237
1238 len = eol - sov;
1239 if (len > h->len)
1240 len = h->len;
1241
1242 memcpy(cap[h->index], sov, len);
1243 cap[h->index][len]=0;
1244 }
1245 }
1246 sol = eol + idx->v[cur_idx].cr + 1;
1247 cur_idx = idx->v[cur_idx].next;
1248 }
1249}
1250
1251
Willy Tarreau42250582007-04-01 01:30:43 +02001252/* either we find an LF at <ptr> or we jump to <bad>.
1253 */
1254#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1255
1256/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1257 * otherwise to <http_msg_ood> with <state> set to <st>.
1258 */
1259#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1260 ptr++; \
1261 if (likely(ptr < end)) \
1262 goto good; \
1263 else { \
1264 state = (st); \
1265 goto http_msg_ood; \
1266 } \
1267 } while (0)
1268
1269
Willy Tarreaubaaee002006-06-26 02:48:02 +02001270/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001271 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001272 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1273 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1274 * will give undefined results.
1275 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1276 * and that msg->sol points to the beginning of the response.
1277 * If a complete line is found (which implies that at least one CR or LF is
1278 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1279 * returned indicating an incomplete line (which does not mean that parts have
1280 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1281 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1282 * upon next call.
1283 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001284 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001285 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1286 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001287 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001288 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001289const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001290 enum ht_state state, const char *ptr, const char *end,
1291 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001292{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001293 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001294
Willy Tarreau8973c702007-01-21 23:58:29 +01001295 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001296 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001297 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001298 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001299 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1300
1301 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001302 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001303 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1304 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001305 state = HTTP_MSG_ERROR;
1306 break;
1307
Willy Tarreau8973c702007-01-21 23:58:29 +01001308 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001309 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001310 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001311 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001312 goto http_msg_rpcode;
1313 }
1314 if (likely(HTTP_IS_SPHT(*ptr)))
1315 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1316 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001317 state = HTTP_MSG_ERROR;
1318 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001319
Willy Tarreau8973c702007-01-21 23:58:29 +01001320 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001321 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001322 if (likely(!HTTP_IS_LWS(*ptr)))
1323 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1324
1325 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001326 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001327 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1328 }
1329
1330 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001331 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001332 http_msg_rsp_reason:
1333 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001334 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001335 msg->sl.st.r_l = 0;
1336 goto http_msg_rpline_eol;
1337
Willy Tarreau8973c702007-01-21 23:58:29 +01001338 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001339 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001340 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001341 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001342 goto http_msg_rpreason;
1343 }
1344 if (likely(HTTP_IS_SPHT(*ptr)))
1345 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1346 /* so it's a CR/LF, so there is no reason phrase */
1347 goto http_msg_rsp_reason;
1348
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001350 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001351 if (likely(!HTTP_IS_CRLF(*ptr)))
1352 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001353 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001354 http_msg_rpline_eol:
1355 /* We have seen the end of line. Note that we do not
1356 * necessarily have the \n yet, but at least we know that we
1357 * have EITHER \r OR \n, otherwise the response would not be
1358 * complete. We can then record the response length and return
1359 * to the caller which will be able to register it.
1360 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001361 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001362 return ptr;
1363
Willy Tarreau8973c702007-01-21 23:58:29 +01001364 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001365#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001366 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1367 exit(1);
1368#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001369 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001370 }
1371
1372 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001373 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001374 if (ret_state)
1375 *ret_state = state;
1376 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001377 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001378 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001379}
1380
Willy Tarreau8973c702007-01-21 23:58:29 +01001381/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001382 * This function parses a request line between <ptr> and <end>, starting with
1383 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1384 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1385 * will give undefined results.
1386 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1387 * and that msg->sol points to the beginning of the request.
1388 * If a complete line is found (which implies that at least one CR or LF is
1389 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1390 * returned indicating an incomplete line (which does not mean that parts have
1391 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1392 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1393 * upon next call.
1394 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001395 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1397 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001398 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001399 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001400const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001401 enum ht_state state, const char *ptr, const char *end,
1402 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001403{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001404 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001405
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001408 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 if (likely(HTTP_IS_TOKEN(*ptr)))
1410 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001411
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001413 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1415 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001416
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 if (likely(HTTP_IS_CRLF(*ptr))) {
1418 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001419 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001421 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001423 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001425 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 msg->sl.rq.v_l = 0;
1427 goto http_msg_rqline_eol;
1428 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001429 state = HTTP_MSG_ERROR;
1430 break;
1431
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001433 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001435 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001436 goto http_msg_rquri;
1437 }
1438 if (likely(HTTP_IS_SPHT(*ptr)))
1439 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1440 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1441 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001442
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001444 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001445 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001446 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001449 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1451 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001452
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001453 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001454 /* non-ASCII chars are forbidden unless option
1455 * accept-invalid-http-request is enabled in the frontend.
1456 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001457 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001458 if (msg->err_pos < -1)
1459 goto invalid_char;
1460 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001461 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001462 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1463 }
1464
1465 if (likely(HTTP_IS_CRLF(*ptr))) {
1466 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1467 goto http_msg_req09_uri_e;
1468 }
1469
1470 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001471 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001472 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001473 state = HTTP_MSG_ERROR;
1474 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001477 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001479 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001480 goto http_msg_rqver;
1481 }
1482 if (likely(HTTP_IS_SPHT(*ptr)))
1483 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1484 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1485 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001488 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001489 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001491
1492 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001493 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001494 http_msg_rqline_eol:
1495 /* We have seen the end of line. Note that we do not
1496 * necessarily have the \n yet, but at least we know that we
1497 * have EITHER \r OR \n, otherwise the request would not be
1498 * complete. We can then record the request length and return
1499 * to the caller which will be able to register it.
1500 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001501 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001502 return ptr;
1503 }
1504
1505 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001506 state = HTTP_MSG_ERROR;
1507 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001508
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001510#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1512 exit(1);
1513#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001514 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001515 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001516
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001518 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 if (ret_state)
1520 *ret_state = state;
1521 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001522 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001525
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001526/*
1527 * Returns the data from Authorization header. Function may be called more
1528 * than once so data is stored in txn->auth_data. When no header is found
1529 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001530 * searching again for something we are unable to find anyway. However, if
1531 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001532 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001533 */
1534
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001535/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1536 * set according to global.tune.bufsize.
1537 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001538char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001539
1540int
Willy Tarreau87b09662015-04-03 00:22:06 +02001541get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001542{
1543
Willy Tarreaueee5b512015-04-03 23:46:31 +02001544 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001545 struct chunk auth_method;
1546 struct hdr_ctx ctx;
1547 char *h, *p;
1548 int len;
1549
1550#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001551 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001552#endif
1553
1554 if (txn->auth.method == HTTP_AUTH_WRONG)
1555 return 0;
1556
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001557 txn->auth.method = HTTP_AUTH_WRONG;
1558
1559 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001560
1561 if (txn->flags & TX_USE_PX_CONN) {
1562 h = "Proxy-Authorization";
1563 len = strlen(h);
1564 } else {
1565 h = "Authorization";
1566 len = strlen(h);
1567 }
1568
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001569 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001570 return 0;
1571
1572 h = ctx.line + ctx.val;
1573
1574 p = memchr(h, ' ', ctx.vlen);
1575 if (!p || p == h)
1576 return 0;
1577
1578 chunk_initlen(&auth_method, h, 0, p-h);
1579 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1580
1581 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1582
1583 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001584 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001585
1586 if (len < 0)
1587 return 0;
1588
1589
1590 get_http_auth_buff[len] = '\0';
1591
1592 p = strchr(get_http_auth_buff, ':');
1593
1594 if (!p)
1595 return 0;
1596
1597 txn->auth.user = get_http_auth_buff;
1598 *p = '\0';
1599 txn->auth.pass = p+1;
1600
1601 txn->auth.method = HTTP_AUTH_BASIC;
1602 return 1;
1603 }
1604
1605 return 0;
1606}
1607
Willy Tarreau58f10d72006-12-04 02:26:12 +01001608
Willy Tarreau8973c702007-01-21 23:58:29 +01001609/*
1610 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001611 * depending on the initial msg->msg_state. The caller is responsible for
1612 * ensuring that the message does not wrap. The function can be preempted
1613 * everywhere when data are missing and recalled at the exact same location
1614 * with no information loss. The message may even be realigned between two
1615 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001616 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001617 * fields. Note that msg->sol will be initialized after completing the first
1618 * state, so that none of the msg pointers has to be initialized prior to the
1619 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001620 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001621void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001622{
Willy Tarreau3770f232013-12-07 00:01:53 +01001623 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001624 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001625 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001626
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001627 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001628 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001629 ptr = buf->p + msg->next;
1630 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001631
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 if (unlikely(ptr >= end))
1633 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001634
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001635 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001636 /*
1637 * First, states that are specific to the response only.
1638 * We check them first so that request and headers are
1639 * closer to each other (accessed more often).
1640 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001641 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001642 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001643 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001644 /* we have a start of message, but we have to check
1645 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001646 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001647 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001648 if (unlikely(ptr != buf->p)) {
1649 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001650 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001651 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001652 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001653 }
Willy Tarreau26927362012-05-18 23:22:52 +02001654 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001655 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001656 hdr_idx_init(idx);
1657 state = HTTP_MSG_RPVER;
1658 goto http_msg_rpver;
1659 }
1660
1661 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1662 goto http_msg_invalid;
1663
1664 if (unlikely(*ptr == '\n'))
1665 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1666 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1667 /* stop here */
1668
Willy Tarreau8973c702007-01-21 23:58:29 +01001669 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001670 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001671 EXPECT_LF_HERE(ptr, http_msg_invalid);
1672 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1673 /* stop here */
1674
Willy Tarreau8973c702007-01-21 23:58:29 +01001675 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001676 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001677 case HTTP_MSG_RPVER_SP:
1678 case HTTP_MSG_RPCODE:
1679 case HTTP_MSG_RPCODE_SP:
1680 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001681 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001682 state, ptr, end,
1683 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001684 if (unlikely(!ptr))
1685 return;
1686
1687 /* we have a full response and we know that we have either a CR
1688 * or an LF at <ptr>.
1689 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001690 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1691
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001692 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001693 if (likely(*ptr == '\r'))
1694 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1695 goto http_msg_rpline_end;
1696
Willy Tarreau8973c702007-01-21 23:58:29 +01001697 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001698 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001699 /* msg->sol must point to the first of CR or LF. */
1700 EXPECT_LF_HERE(ptr, http_msg_invalid);
1701 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1702 /* stop here */
1703
1704 /*
1705 * Second, states that are specific to the request only
1706 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001707 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001708 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001710 /* we have a start of message, but we have to check
1711 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001712 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001713 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001714 if (likely(ptr != buf->p)) {
1715 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001716 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001717 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001718 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719 }
Willy Tarreau26927362012-05-18 23:22:52 +02001720 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001721 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001722 state = HTTP_MSG_RQMETH;
1723 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001725
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001726 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1727 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001728
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 if (unlikely(*ptr == '\n'))
1730 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1731 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001732 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001733
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001735 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001736 EXPECT_LF_HERE(ptr, http_msg_invalid);
1737 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001738 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001739
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001741 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 case HTTP_MSG_RQMETH_SP:
1743 case HTTP_MSG_RQURI:
1744 case HTTP_MSG_RQURI_SP:
1745 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001746 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001747 state, ptr, end,
1748 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001749 if (unlikely(!ptr))
1750 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001751
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001752 /* we have a full request and we know that we have either a CR
1753 * or an LF at <ptr>.
1754 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001755 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001756
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001757 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 if (likely(*ptr == '\r'))
1759 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001760 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001761
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001763 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001764 /* check for HTTP/0.9 request : no version information available.
1765 * msg->sol must point to the first of CR or LF.
1766 */
1767 if (unlikely(msg->sl.rq.v_l == 0))
1768 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001769
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001770 EXPECT_LF_HERE(ptr, http_msg_invalid);
1771 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001772 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001773
Willy Tarreau8973c702007-01-21 23:58:29 +01001774 /*
1775 * Common states below
1776 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001777 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001778 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001779 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001780 if (likely(!HTTP_IS_CRLF(*ptr))) {
1781 goto http_msg_hdr_name;
1782 }
1783
1784 if (likely(*ptr == '\r'))
1785 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1786 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001787
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001788 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001789 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 /* assumes msg->sol points to the first char */
1791 if (likely(HTTP_IS_TOKEN(*ptr)))
1792 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001793
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001794 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001795 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001796
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001797 if (likely(msg->err_pos < -1) || *ptr == '\n')
1798 goto http_msg_invalid;
1799
1800 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001801 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001802
1803 /* and we still accept this non-token character */
1804 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001805
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001807 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001808 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001809 if (likely(HTTP_IS_SPHT(*ptr)))
1810 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001811
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001812 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001813 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001814
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001815 if (likely(!HTTP_IS_CRLF(*ptr))) {
1816 goto http_msg_hdr_val;
1817 }
1818
1819 if (likely(*ptr == '\r'))
1820 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1821 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001822
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001823 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001824 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001825 EXPECT_LF_HERE(ptr, http_msg_invalid);
1826 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001827
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001828 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001829 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001830 if (likely(HTTP_IS_SPHT(*ptr))) {
1831 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001832 for (; buf->p + msg->sov < ptr; msg->sov++)
1833 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001834 goto http_msg_hdr_l1_sp;
1835 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001836 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001837 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001838 goto http_msg_complete_header;
1839
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001840 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001841 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001842 /* assumes msg->sol points to the first char, and msg->sov
1843 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001844 */
1845 if (likely(!HTTP_IS_CRLF(*ptr)))
1846 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001847
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001848 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001849 /* Note: we could also copy eol into ->eoh so that we have the
1850 * real header end in case it ends with lots of LWS, but is this
1851 * really needed ?
1852 */
1853 if (likely(*ptr == '\r'))
1854 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1855 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001856
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001857 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001858 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001859 EXPECT_LF_HERE(ptr, http_msg_invalid);
1860 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001861
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001862 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001863 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001864 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1865 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001866 for (; buf->p + msg->eol < ptr; msg->eol++)
1867 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001868 goto http_msg_hdr_val;
1869 }
1870 http_msg_complete_header:
1871 /*
1872 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001873 * Assumes msg->sol points to the first char, msg->sov points
1874 * to the first character of the value and msg->eol to the
1875 * first CR or LF so we know how the line ends. We insert last
1876 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001877 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001878 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001879 idx, idx->tail) < 0))
1880 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001881
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001882 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 if (likely(!HTTP_IS_CRLF(*ptr))) {
1884 goto http_msg_hdr_name;
1885 }
1886
1887 if (likely(*ptr == '\r'))
1888 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1889 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001890
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001891 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001892 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001893 /* Assumes msg->sol points to the first of either CR or LF.
1894 * Sets ->sov and ->next to the total header length, ->eoh to
1895 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1896 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001897 EXPECT_LF_HERE(ptr, http_msg_invalid);
1898 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001899 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001900 msg->eoh = msg->sol;
1901 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001902 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001903 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001904 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001905
1906 case HTTP_MSG_ERROR:
1907 /* this may only happen if we call http_msg_analyser() twice with an error */
1908 break;
1909
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001910 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001911#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001912 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1913 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001914#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001915 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001916 }
1917 http_msg_ood:
1918 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001919 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001920 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001921 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001922
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001923 http_msg_invalid:
1924 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001925 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001926 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001927 return;
1928}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001929
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001930/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1931 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1932 * nothing is done and 1 is returned.
1933 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001934static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001935{
1936 int delta;
1937 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001938 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001939
1940 if (msg->sl.rq.v_l != 0)
1941 return 1;
1942
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001943 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1944 if (txn->meth != HTTP_METH_GET)
1945 return 0;
1946
Willy Tarreau9b28e032012-10-12 23:49:43 +02001947 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001948 delta = 0;
1949
1950 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001951 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1952 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001953 }
1954 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001955 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001956 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001957 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001958 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001959 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001960 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001961 NULL, NULL);
1962 if (unlikely(!cur_end))
1963 return 0;
1964
1965 /* we have a full HTTP/1.0 request now and we know that
1966 * we have either a CR or an LF at <ptr>.
1967 */
1968 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1969 return 1;
1970}
1971
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001972/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001973 * and "keep-alive" values. If we already know that some headers may safely
1974 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001975 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1976 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001977 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001978 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1979 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1980 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001981 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001982 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001983void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001984{
Willy Tarreau5b154472009-12-21 20:11:07 +01001985 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001986 const char *hdr_val = "Connection";
1987 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001988
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001989 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001990 return;
1991
Willy Tarreau88d349d2010-01-25 12:15:43 +01001992 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1993 hdr_val = "Proxy-Connection";
1994 hdr_len = 16;
1995 }
1996
Willy Tarreau5b154472009-12-21 20:11:07 +01001997 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001998 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001999 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002000 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2001 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002002 if (to_del & 2)
2003 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002004 else
2005 txn->flags |= TX_CON_KAL_SET;
2006 }
2007 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2008 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002009 if (to_del & 1)
2010 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002011 else
2012 txn->flags |= TX_CON_CLO_SET;
2013 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002014 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2015 txn->flags |= TX_HDR_CONN_UPG;
2016 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002017 }
2018
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002019 txn->flags |= TX_HDR_CONN_PRS;
2020 return;
2021}
Willy Tarreau5b154472009-12-21 20:11:07 +01002022
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002023/* Apply desired changes on the Connection: header. Values may be removed and/or
2024 * added depending on the <wanted> flags, which are exclusively composed of
2025 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2026 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2027 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002028void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002029{
2030 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002031 const char *hdr_val = "Connection";
2032 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002033
2034 ctx.idx = 0;
2035
Willy Tarreau88d349d2010-01-25 12:15:43 +01002036
2037 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2038 hdr_val = "Proxy-Connection";
2039 hdr_len = 16;
2040 }
2041
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002042 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002043 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002044 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2045 if (wanted & TX_CON_KAL_SET)
2046 txn->flags |= TX_CON_KAL_SET;
2047 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002048 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002049 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002050 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2051 if (wanted & TX_CON_CLO_SET)
2052 txn->flags |= TX_CON_CLO_SET;
2053 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002054 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002055 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002056 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002057
2058 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2059 return;
2060
2061 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2062 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002063 hdr_val = "Connection: close";
2064 hdr_len = 17;
2065 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2066 hdr_val = "Proxy-Connection: close";
2067 hdr_len = 23;
2068 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002069 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002070 }
2071
2072 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2073 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002074 hdr_val = "Connection: keep-alive";
2075 hdr_len = 22;
2076 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2077 hdr_val = "Proxy-Connection: keep-alive";
2078 hdr_len = 28;
2079 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002080 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002081 }
2082 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002083}
2084
Willy Tarreauc24715e2014-04-17 15:21:20 +02002085/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
2086 * the first byte of data after the chunk size, so that we know we can forward
2087 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
2088 * the chunk size. That way it is always possible to differentiate between the
2089 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01002090 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002091 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002092 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002093static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002094{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002095 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002096 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002097 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002098 const char *end = buf->data + buf->size;
2099 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002100 unsigned int chunk = 0;
2101
2102 /* The chunk size is in the following form, though we are only
2103 * interested in the size and CRLF :
2104 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2105 */
2106 while (1) {
2107 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002108 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002109 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002110 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002111 if (c < 0) /* not a hex digit anymore */
2112 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002113 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002114 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002115 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002116 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002117 chunk = (chunk << 4) + c;
2118 }
2119
Willy Tarreaud98cf932009-12-27 22:54:55 +01002120 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002121 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002122 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002123
2124 while (http_is_spht[(unsigned char)*ptr]) {
2125 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002126 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002127 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002128 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002129 }
2130
Willy Tarreaud98cf932009-12-27 22:54:55 +01002131 /* Up to there, we know that at least one byte is present at *ptr. Check
2132 * for the end of chunk size.
2133 */
2134 while (1) {
2135 if (likely(HTTP_IS_CRLF(*ptr))) {
2136 /* we now have a CR or an LF at ptr */
2137 if (likely(*ptr == '\r')) {
2138 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002139 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002140 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002141 return 0;
2142 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002143
Willy Tarreaud98cf932009-12-27 22:54:55 +01002144 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002145 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002146 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002147 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002148 /* done */
2149 break;
2150 }
2151 else if (*ptr == ';') {
2152 /* chunk extension, ends at next CRLF */
2153 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002154 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002155 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002156 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002157
2158 while (!HTTP_IS_CRLF(*ptr)) {
2159 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002160 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002161 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002162 return 0;
2163 }
2164 /* we have a CRLF now, loop above */
2165 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002166 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002167 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002168 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002169 }
2170
Willy Tarreaud98cf932009-12-27 22:54:55 +01002171 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002172 * which may or may not be present. We save that into ->next,
2173 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002174 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002175 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002176 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002177 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002178 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002179 msg->chunk_len = chunk;
2180 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002181 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002182 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002183 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002184 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002185 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002186}
2187
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002188/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002189 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002190 * the trailers is found, it is automatically scheduled to be forwarded,
2191 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2192 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002193 * except maybe msg->next if it could parse some lines, and returns zero.
2194 * If a parse error is encountered, the function returns < 0 and does not
2195 * change anything except maybe msg->next. Note that the message must
2196 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002197 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002198 * forwarding, and that msg->next exactly matches the length of trailers
2199 * already parsed and not forwarded. It is also important to note that this
2200 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002201 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002202static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002203{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002204 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002205
Willy Tarreaua458b672012-03-05 11:17:50 +01002206 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002207 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002208 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002209 const char *ptr = b_ptr(buf, msg->next);
2210 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002211 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002212
2213 /* scan current line and stop at LF or CRLF */
2214 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002215 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002216 return 0;
2217
2218 if (*ptr == '\n') {
2219 if (!p1)
2220 p1 = ptr;
2221 p2 = ptr;
2222 break;
2223 }
2224
2225 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002226 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002227 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002228 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002229 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002230 p1 = ptr;
2231 }
2232
2233 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002234 if (ptr >= buf->data + buf->size)
2235 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002236 }
2237
2238 /* after LF; point to beginning of next line */
2239 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002240 if (p2 >= buf->data + buf->size)
2241 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002242
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002243 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002244 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002245 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002246
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002247 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002248 /* LF/CRLF at beginning of line => end of trailers at p2.
2249 * Everything was scheduled for forwarding, there's nothing
2250 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002251 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002252 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002253 msg->msg_state = HTTP_MSG_DONE;
2254 return 1;
2255 }
2256 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002257 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002258 }
2259}
2260
Willy Tarreauc24715e2014-04-17 15:21:20 +02002261/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2262 * or a possible LF alone at the end of a chunk. It automatically adjusts
2263 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002264 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002265 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2266 * not enough data are available, the function does not change anything and
2267 * returns zero. If a parse error is encountered, the function returns < 0 and
2268 * does not change anything. Note: this function is designed to parse wrapped
2269 * CRLF at the end of the buffer.
2270 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002271static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002272{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002273 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002274 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002275 int bytes;
2276
2277 /* NB: we'll check data availabilty at the end. It's not a
2278 * problem because whatever we match first will be checked
2279 * against the correct length.
2280 */
2281 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002282 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002283 if (*ptr == '\r') {
2284 bytes++;
2285 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002286 if (ptr >= buf->data + buf->size)
2287 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002288 }
2289
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002290 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002291 return 0;
2292
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002293 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002294 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002295 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002296 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002297
2298 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002299 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002300 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002301 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002302 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002303 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2304 return 1;
2305}
Willy Tarreau5b154472009-12-21 20:11:07 +01002306
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002307/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2308 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2309 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2310 * Unparsable qvalues return 1000 as "q=1.000".
2311 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002312int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002313{
2314 int q = 1000;
2315
Willy Tarreau506c69a2014-07-08 00:59:48 +02002316 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002317 goto out;
2318 q = (*qvalue++ - '0') * 1000;
2319
2320 if (*qvalue++ != '.')
2321 goto out;
2322
Willy Tarreau506c69a2014-07-08 00:59:48 +02002323 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002324 goto out;
2325 q += (*qvalue++ - '0') * 100;
2326
Willy Tarreau506c69a2014-07-08 00:59:48 +02002327 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002328 goto out;
2329 q += (*qvalue++ - '0') * 10;
2330
Willy Tarreau506c69a2014-07-08 00:59:48 +02002331 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002332 goto out;
2333 q += (*qvalue++ - '0') * 1;
2334 out:
2335 if (q > 1000)
2336 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002337 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002338 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002339 return q;
2340}
William Lallemand82fe75c2012-10-23 10:25:10 +02002341
2342/*
2343 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002344 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002345int select_compression_request_header(struct stream *s, struct buffer *req)
William Lallemand82fe75c2012-10-23 10:25:10 +02002346{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002347 struct http_txn *txn = s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002348 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002349 struct hdr_ctx ctx;
2350 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002351 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002352
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002353 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2354 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002355 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2356 */
2357 ctx.idx = 0;
2358 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2359 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002360 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2361 (ctx.vlen < 31 ||
2362 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2363 ctx.line[ctx.val + 30] < '6' ||
2364 (ctx.line[ctx.val + 30] == '6' &&
2365 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2366 s->comp_algo = NULL;
2367 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002368 }
2369
William Lallemand82fe75c2012-10-23 10:25:10 +02002370 /* search for the algo in the backend in priority or the frontend */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002371 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 +01002372 int best_q = 0;
2373
William Lallemand82fe75c2012-10-23 10:25:10 +02002374 ctx.idx = 0;
2375 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002376 const char *qval;
2377 int q;
2378 int toklen;
2379
2380 /* try to isolate the token from the optional q-value */
2381 toklen = 0;
2382 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2383 toklen++;
2384
2385 qval = ctx.line + ctx.val + toklen;
2386 while (1) {
2387 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2388 qval++;
2389
2390 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2391 qval = NULL;
2392 break;
2393 }
2394 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002395
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002396 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2397 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002398
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002399 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2400 qval = NULL;
2401 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002402 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002403 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2404 break;
2405
2406 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2407 qval++;
2408 }
2409
2410 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002411 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002412
2413 if (q <= best_q)
2414 continue;
2415
2416 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2417 if (*(ctx.line + ctx.val) == '*' ||
Willy Tarreau615105e2015-03-28 16:40:46 +01002418 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002419 s->comp_algo = comp_algo;
2420 best_q = q;
2421 break;
2422 }
2423 }
2424 }
2425 }
2426
2427 /* remove all occurrences of the header when "compression offload" is set */
2428 if (s->comp_algo) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002429 if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002430 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2431 ctx.idx = 0;
2432 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2433 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002434 }
2435 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002436 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002437 }
2438
2439 /* identity is implicit does not require headers */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002440 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 +02002441 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
Willy Tarreau615105e2015-03-28 16:40:46 +01002442 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002443 s->comp_algo = comp_algo;
2444 return 1;
2445 }
2446 }
2447 }
2448
2449 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002450 return 0;
2451}
2452
2453/*
2454 * Selects a comression algorithm depending of the server response.
2455 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002456int select_compression_response_header(struct stream *s, struct buffer *res)
William Lallemand82fe75c2012-10-23 10:25:10 +02002457{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002458 struct http_txn *txn = s->txn;
William Lallemand82fe75c2012-10-23 10:25:10 +02002459 struct http_msg *msg = &txn->rsp;
2460 struct hdr_ctx ctx;
2461 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002462
2463 /* no common compression algorithm was found in request header */
2464 if (s->comp_algo == NULL)
2465 goto fail;
2466
2467 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002468 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002469 goto fail;
2470
Jesse Hathaway2468d4e2015-03-06 20:16:15 +00002471 /* compress 200,201,202,203 responses only */
2472 if ((txn->status != 200) &&
2473 (txn->status != 201) &&
2474 (txn->status != 202) &&
2475 (txn->status != 203))
William Lallemandd3002612012-11-26 14:34:47 +01002476 goto fail;
2477
William Lallemand82fe75c2012-10-23 10:25:10 +02002478 /* Content-Length is null */
2479 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2480 goto fail;
2481
2482 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002483 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002484 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2485 goto fail;
2486
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002487 /* no compression when Cache-Control: no-transform is present in the message */
2488 ctx.idx = 0;
2489 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2490 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2491 goto fail;
2492 }
2493
William Lallemand82fe75c2012-10-23 10:25:10 +02002494 comp_type = NULL;
2495
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002496 /* we don't want to compress multipart content-types, nor content-types that are
2497 * not listed in the "compression type" directive if any. If no content-type was
2498 * found but configuration requires one, we don't compress either. Backend has
2499 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002500 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002501 ctx.idx = 0;
2502 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2503 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2504 goto fail;
2505
2506 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002507 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002508 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002509 if (ctx.vlen >= comp_type->name_len &&
2510 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002511 /* this Content-Type should be compressed */
2512 break;
2513 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002514 /* this Content-Type should not be compressed */
2515 if (comp_type == NULL)
2516 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002517 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002518 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002519 else { /* no content-type header */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002520 if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->comp->types))
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002521 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002522 }
2523
William Lallemandd85f9172012-11-09 17:05:39 +01002524 /* limit compression rate */
2525 if (global.comp_rate_lim > 0)
2526 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2527 goto fail;
2528
William Lallemand072a2bf2012-11-20 17:01:01 +01002529 /* limit cpu usage */
2530 if (idle_pct < compress_min_idle)
2531 goto fail;
2532
William Lallemand4c49fae2012-11-07 15:00:23 +01002533 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002534 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002535 goto fail;
2536
Willy Tarreaue7dff022015-04-03 01:14:29 +02002537 s->flags |= SF_COMP_READY;
William Lallemandec3e3892012-11-12 17:02:18 +01002538
William Lallemand82fe75c2012-10-23 10:25:10 +02002539 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002540 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002541 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2542 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2543
2544 /* add Transfer-Encoding header */
2545 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2546 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2547
2548 /*
2549 * Add Content-Encoding header when it's not identity encoding.
2550 * RFC 2616 : Identity encoding: This content-coding is used only in the
2551 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2552 * header.
2553 */
Willy Tarreau615105e2015-03-28 16:40:46 +01002554 if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002555 trash.len = 18;
2556 memcpy(trash.str, "Content-Encoding: ", trash.len);
Willy Tarreau615105e2015-03-28 16:40:46 +01002557 memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len);
2558 trash.len += s->comp_algo->ua_name_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002559 trash.str[trash.len] = '\0';
2560 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002561 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002562 return 1;
2563
2564fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002565 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002566 return 0;
2567}
2568
Willy Tarreau87b09662015-04-03 00:22:06 +02002569void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002570{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002571 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002572 int tmp = TX_CON_WANT_KAL;
2573
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002574 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2575 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002576 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2577 tmp = TX_CON_WANT_TUN;
2578
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002579 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002580 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2581 tmp = TX_CON_WANT_TUN;
2582 }
2583
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002584 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002585 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2586 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002587 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002588 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2589 tmp = TX_CON_WANT_CLO;
2590 else
2591 tmp = TX_CON_WANT_SCL;
2592 }
2593
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002594 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002595 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2596 tmp = TX_CON_WANT_CLO;
2597
2598 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2599 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2600
2601 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2602 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2603 /* parse the Connection header and possibly clean it */
2604 int to_del = 0;
2605 if ((msg->flags & HTTP_MSGF_VER_11) ||
2606 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002607 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002608 to_del |= 2; /* remove "keep-alive" */
2609 if (!(msg->flags & HTTP_MSGF_VER_11))
2610 to_del |= 1; /* remove "close" */
2611 http_parse_connection_header(txn, msg, to_del);
2612 }
2613
2614 /* check if client or config asks for explicit close in KAL/SCL */
2615 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2616 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2617 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2618 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2619 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002620 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002621 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2622}
William Lallemand82fe75c2012-10-23 10:25:10 +02002623
Willy Tarreaud787e662009-07-07 10:14:51 +02002624/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2625 * processing can continue on next analysers, or zero if it either needs more
2626 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002627 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002628 * when it has nothing left to do, and may remove any analyser when it wants to
2629 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002630 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002631int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002632{
Willy Tarreau59234e92008-11-30 23:51:27 +01002633 /*
2634 * We will parse the partial (or complete) lines.
2635 * We will check the request syntax, and also join multi-line
2636 * headers. An index of all the lines will be elaborated while
2637 * parsing.
2638 *
2639 * For the parsing, we use a 28 states FSM.
2640 *
2641 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002642 * req->buf->p = beginning of request
2643 * req->buf->p + msg->eoh = end of processed headers / start of current one
2644 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002645 * msg->eol = end of current header or line (LF or CRLF)
2646 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002647 *
2648 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002649 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002650 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2651 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002652 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002653
Willy Tarreau59234e92008-11-30 23:51:27 +01002654 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002655 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002656 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002658 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002659
Willy Tarreau87b09662015-04-03 00:22:06 +02002660 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 +01002661 now_ms, __FUNCTION__,
2662 s,
2663 req,
2664 req->rex, req->wex,
2665 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002666 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002667 req->analysers);
2668
Willy Tarreau52a0c602009-08-16 22:45:38 +02002669 /* we're speaking HTTP here, so let's speak HTTP to the client */
2670 s->srv_error = http_return_srv_error;
2671
Willy Tarreau83e3af02009-12-28 17:39:57 +01002672 /* There's a protected area at the end of the buffer for rewriting
2673 * purposes. We don't want to start to parse the request if the
2674 * protected area is affected, because we may have to move processed
2675 * data later, which is much more complicated.
2676 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002677 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002678 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002679 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002680 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002681 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002682 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002683 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002684 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002685 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002686 return 0;
2687 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002688 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2689 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2690 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002691 }
2692
Willy Tarreau065e8332010-01-08 00:30:20 +01002693 /* Note that we have the same problem with the response ; we
2694 * may want to send a redirect, error or anything which requires
2695 * some spare space. So we'll ensure that we have at least
2696 * maxrewrite bytes available in the response buffer before
2697 * processing that one. This will only affect pipelined
2698 * keep-alive requests.
2699 */
2700 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002701 unlikely(!channel_is_rewritable(&s->res) ||
2702 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2703 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2704 if (s->res.buf->o) {
2705 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002706 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002707 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002708 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002709 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2710 s->res.flags |= CF_WAKE_WRITE;
2711 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002712 return 0;
2713 }
2714 }
2715
Willy Tarreau9b28e032012-10-12 23:49:43 +02002716 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002717 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002718 }
2719
Willy Tarreau59234e92008-11-30 23:51:27 +01002720 /* 1: we might have to print this header in debug mode */
2721 if (unlikely((global.mode & MODE_DEBUG) &&
2722 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002723 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002724 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002725
Willy Tarreau9b28e032012-10-12 23:49:43 +02002726 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002727 /* this is a bit complex : in case of error on the request line,
2728 * we know that rq.l is still zero, so we display only the part
2729 * up to the end of the line (truncated by debug_hdr).
2730 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002731 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002732 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002733
Willy Tarreau59234e92008-11-30 23:51:27 +01002734 sol += hdr_idx_first_pos(&txn->hdr_idx);
2735 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002736
Willy Tarreau59234e92008-11-30 23:51:27 +01002737 while (cur_idx) {
2738 eol = sol + txn->hdr_idx.v[cur_idx].len;
2739 debug_hdr("clihdr", s, sol, eol);
2740 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2741 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002742 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002743 }
2744
Willy Tarreau58f10d72006-12-04 02:26:12 +01002745
Willy Tarreau59234e92008-11-30 23:51:27 +01002746 /*
2747 * Now we quickly check if we have found a full valid request.
2748 * If not so, we check the FD and buffer states before leaving.
2749 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002750 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002751 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002752 * on a keep-alive stream, if we encounter and error, close, t/o,
2753 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002754 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002755 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002756 * Last, we may increase some tracked counters' http request errors on
2757 * the cases that are deliberately the client's fault. For instance,
2758 * a timeout or connection reset is not counted as an error. However
2759 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002760 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002761
Willy Tarreau655dce92009-11-08 13:10:58 +01002762 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002763 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002764 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002765 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002766 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002767 stream_inc_http_req_ctr(s);
2768 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002769 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002770 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002771 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002772
Willy Tarreau59234e92008-11-30 23:51:27 +01002773 /* 1: Since we are in header mode, if there's no space
2774 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002775 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002776 * must terminate it now.
2777 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002778 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002779 /* FIXME: check if URI is set and return Status
2780 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002781 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002782 stream_inc_http_req_ctr(s);
2783 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002784 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002785 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002786 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002787 goto return_bad_req;
2788 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002789
Willy Tarreau59234e92008-11-30 23:51:27 +01002790 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002791 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002792 if (!(s->flags & SF_ERR_MASK))
2793 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002794
Willy Tarreaufcffa692010-01-10 14:21:19 +01002795 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002796 goto failed_keep_alive;
2797
Willy Tarreau0f228a02015-05-01 15:37:53 +02002798 if (sess->fe->options & PR_O_IGNORE_PRB)
2799 goto failed_keep_alive;
2800
Willy Tarreau59234e92008-11-30 23:51:27 +01002801 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002802 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002803 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002804 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002805 }
2806
Willy Tarreaudc979f22012-12-04 10:39:01 +01002807 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002808 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002809 msg->msg_state = HTTP_MSG_ERROR;
2810 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002811
Willy Tarreau87b09662015-04-03 00:22:06 +02002812 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002813 proxy_inc_fe_req_ctr(sess->fe);
2814 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002815 if (sess->listener->counters)
2816 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002817
Willy Tarreaue7dff022015-04-03 01:14:29 +02002818 if (!(s->flags & SF_FINST_MASK))
2819 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002820 return 0;
2821 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002822
Willy Tarreau59234e92008-11-30 23:51:27 +01002823 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002824 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002825 if (!(s->flags & SF_ERR_MASK))
2826 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002827
Willy Tarreaufcffa692010-01-10 14:21:19 +01002828 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002829 goto failed_keep_alive;
2830
Willy Tarreau0f228a02015-05-01 15:37:53 +02002831 if (sess->fe->options & PR_O_IGNORE_PRB)
2832 goto failed_keep_alive;
2833
Willy Tarreau59234e92008-11-30 23:51:27 +01002834 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002835 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002836 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002837 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002838 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002839 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01002840 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002841 msg->msg_state = HTTP_MSG_ERROR;
2842 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002843
Willy Tarreau87b09662015-04-03 00:22:06 +02002844 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002845 proxy_inc_fe_req_ctr(sess->fe);
2846 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002847 if (sess->listener->counters)
2848 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002849
Willy Tarreaue7dff022015-04-03 01:14:29 +02002850 if (!(s->flags & SF_FINST_MASK))
2851 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002852 return 0;
2853 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002854
Willy Tarreau59234e92008-11-30 23:51:27 +01002855 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002856 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002857 if (!(s->flags & SF_ERR_MASK))
2858 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002859
Willy Tarreaufcffa692010-01-10 14:21:19 +01002860 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002861 goto failed_keep_alive;
2862
Willy Tarreau0f228a02015-05-01 15:37:53 +02002863 if (sess->fe->options & PR_O_IGNORE_PRB)
2864 goto failed_keep_alive;
2865
Willy Tarreau4076a152009-04-02 15:18:36 +02002866 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002867 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002868 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002869 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002870 msg->msg_state = HTTP_MSG_ERROR;
2871 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002872
Willy Tarreau87b09662015-04-03 00:22:06 +02002873 stream_inc_http_err_ctr(s);
2874 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002875 proxy_inc_fe_req_ctr(sess->fe);
2876 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002877 if (sess->listener->counters)
2878 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002879
Willy Tarreaue7dff022015-04-03 01:14:29 +02002880 if (!(s->flags & SF_FINST_MASK))
2881 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002882 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002883 }
2884
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002885 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002886 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002887 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002888#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002889 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2890 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002891 /* We need more data, we have to re-enable quick-ack in case we
2892 * previously disabled it, otherwise we might cause the client
2893 * to delay next data.
2894 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002895 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002896 }
2897#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002898
Willy Tarreaufcffa692010-01-10 14:21:19 +01002899 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2900 /* If the client starts to talk, let's fall back to
2901 * request timeout processing.
2902 */
2903 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002904 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002905 }
2906
Willy Tarreau59234e92008-11-30 23:51:27 +01002907 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002908 if (!tick_isset(req->analyse_exp)) {
2909 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2910 (txn->flags & TX_WAIT_NEXT_RQ) &&
2911 tick_isset(s->be->timeout.httpka))
2912 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2913 else
2914 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2915 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002916
Willy Tarreau59234e92008-11-30 23:51:27 +01002917 /* we're not ready yet */
2918 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002919
2920 failed_keep_alive:
2921 /* Here we process low-level errors for keep-alive requests. In
2922 * short, if the request is not the first one and it experiences
2923 * a timeout, read error or shutdown, we just silently close so
2924 * that the client can try again.
2925 */
2926 txn->status = 0;
2927 msg->msg_state = HTTP_MSG_RQBEFORE;
2928 req->analysers = 0;
2929 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002930 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002931 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau350f4872014-11-28 14:42:25 +01002932 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002933 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002934 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002935
Willy Tarreaud787e662009-07-07 10:14:51 +02002936 /* OK now we have a complete HTTP request with indexed headers. Let's
2937 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002938 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002939 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002940 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002941 * byte after the last LF. msg->sov points to the first byte of data.
2942 * msg->eol cannot be trusted because it may have been left uninitialized
2943 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002944 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002945
Willy Tarreau87b09662015-04-03 00:22:06 +02002946 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002947 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002948
Willy Tarreaub16a5742010-01-10 14:46:16 +01002949 if (txn->flags & TX_WAIT_NEXT_RQ) {
2950 /* kill the pending keep-alive timeout */
2951 txn->flags &= ~TX_WAIT_NEXT_RQ;
2952 req->analyse_exp = TICK_ETERNITY;
2953 }
2954
2955
Willy Tarreaud787e662009-07-07 10:14:51 +02002956 /* Maybe we found in invalid header name while we were configured not
2957 * to block on that, so we have to capture it now.
2958 */
2959 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002960 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002961
Willy Tarreau59234e92008-11-30 23:51:27 +01002962 /*
2963 * 1: identify the method
2964 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002965 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002966
2967 /* we can make use of server redirect on GET and HEAD */
2968 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002969 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002970
Willy Tarreau59234e92008-11-30 23:51:27 +01002971 /*
2972 * 2: check if the URI matches the monitor_uri.
2973 * We have to do this for every request which gets in, because
2974 * the monitor-uri is defined by the frontend.
2975 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002976 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2977 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002978 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002979 sess->fe->monitor_uri,
2980 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002981 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002982 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002983 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002984 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002985
Willy Tarreaue7dff022015-04-03 01:14:29 +02002986 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002987 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002988
Willy Tarreau59234e92008-11-30 23:51:27 +01002989 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002990 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002991 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002992
Willy Tarreau59234e92008-11-30 23:51:27 +01002993 ret = acl_pass(ret);
2994 if (cond->pol == ACL_COND_UNLESS)
2995 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002996
Willy Tarreau59234e92008-11-30 23:51:27 +01002997 if (ret) {
2998 /* we fail this request, let's return 503 service unavail */
2999 txn->status = 503;
Willy Tarreau350f4872014-11-28 14:42:25 +01003000 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02003001 if (!(s->flags & SF_ERR_MASK))
3002 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01003003 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01003004 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003005 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01003006
Willy Tarreau59234e92008-11-30 23:51:27 +01003007 /* nothing to fail, let's reply normaly */
3008 txn->status = 200;
Willy Tarreau350f4872014-11-28 14:42:25 +01003009 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02003010 if (!(s->flags & SF_ERR_MASK))
3011 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01003012 goto return_prx_cond;
3013 }
3014
3015 /*
3016 * 3: Maybe we have to copy the original REQURI for the logs ?
3017 * Note: we cannot log anymore if the request has been
3018 * classified as invalid.
3019 */
3020 if (unlikely(s->logs.logwait & LW_REQ)) {
3021 /* we have a complete HTTP request that we must log */
3022 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
3023 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003024
Willy Tarreau59234e92008-11-30 23:51:27 +01003025 if (urilen >= REQURI_LEN)
3026 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02003027 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01003028 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003029
Willy Tarreaud79a3b22012-12-28 09:40:16 +01003030 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01003031 s->do_log(s);
3032 } else {
3033 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003034 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003035 }
Willy Tarreau06619262006-12-17 08:37:22 +01003036
Willy Tarreau91852eb2015-05-01 13:26:00 +02003037 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
3038 * exactly one digit "." one digit. This check may be disabled using
3039 * option accept-invalid-http-request.
3040 */
3041 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
3042 if (msg->sl.rq.v_l != 8) {
3043 msg->err_pos = msg->sl.rq.v;
3044 goto return_bad_req;
3045 }
3046
3047 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
3048 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
3049 req->buf->p[msg->sl.rq.v + 6] != '.' ||
3050 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
3051 msg->err_pos = msg->sl.rq.v + 4;
3052 goto return_bad_req;
3053 }
3054 }
Willy Tarreau13317662015-05-01 13:47:08 +02003055 else {
3056 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
3057 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
3058 goto return_bad_req;
3059 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02003060
Willy Tarreau5b154472009-12-21 20:11:07 +01003061 /* ... and check if the request is HTTP/1.1 or above */
3062 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003063 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
3064 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
3065 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003066 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01003067
3068 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01003069 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 +01003070
Willy Tarreau88d349d2010-01-25 12:15:43 +01003071 /* if the frontend has "option http-use-proxy-header", we'll check if
3072 * we have what looks like a proxied connection instead of a connection,
3073 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
3074 * Note that this is *not* RFC-compliant, however browsers and proxies
3075 * happen to do that despite being non-standard :-(
3076 * We consider that a request not beginning with either '/' or '*' is
3077 * a proxied connection, which covers both "scheme://location" and
3078 * CONNECT ip:port.
3079 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003080 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003081 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01003082 txn->flags |= TX_USE_PX_CONN;
3083
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003084 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003085 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003086
Willy Tarreau59234e92008-11-30 23:51:27 +01003087 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003088 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003089 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003090 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003091
Willy Tarreau557f1992015-05-01 10:05:17 +02003092 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3093 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003094 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003095 * The length of a message body is determined by one of the following
3096 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003097 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003098 * 1. Any response to a HEAD request and any response with a 1xx
3099 * (Informational), 204 (No Content), or 304 (Not Modified) status
3100 * code is always terminated by the first empty line after the
3101 * header fields, regardless of the header fields present in the
3102 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003103 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003104 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3105 * the connection will become a tunnel immediately after the empty
3106 * line that concludes the header fields. A client MUST ignore any
3107 * Content-Length or Transfer-Encoding header fields received in
3108 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003109 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003110 * 3. If a Transfer-Encoding header field is present and the chunked
3111 * transfer coding (Section 4.1) is the final encoding, the message
3112 * body length is determined by reading and decoding the chunked
3113 * data until the transfer coding indicates the data is complete.
3114 *
3115 * If a Transfer-Encoding header field is present in a response and
3116 * the chunked transfer coding is not the final encoding, the
3117 * message body length is determined by reading the connection until
3118 * it is closed by the server. If a Transfer-Encoding header field
3119 * is present in a request and the chunked transfer coding is not
3120 * the final encoding, the message body length cannot be determined
3121 * reliably; the server MUST respond with the 400 (Bad Request)
3122 * status code and then close the connection.
3123 *
3124 * If a message is received with both a Transfer-Encoding and a
3125 * Content-Length header field, the Transfer-Encoding overrides the
3126 * Content-Length. Such a message might indicate an attempt to
3127 * perform request smuggling (Section 9.5) or response splitting
3128 * (Section 9.4) and ought to be handled as an error. A sender MUST
3129 * remove the received Content-Length field prior to forwarding such
3130 * a message downstream.
3131 *
3132 * 4. If a message is received without Transfer-Encoding and with
3133 * either multiple Content-Length header fields having differing
3134 * field-values or a single Content-Length header field having an
3135 * invalid value, then the message framing is invalid and the
3136 * recipient MUST treat it as an unrecoverable error. If this is a
3137 * request message, the server MUST respond with a 400 (Bad Request)
3138 * status code and then close the connection. If this is a response
3139 * message received by a proxy, the proxy MUST close the connection
3140 * to the server, discard the received response, and send a 502 (Bad
3141 * Gateway) response to the client. If this is a response message
3142 * received by a user agent, the user agent MUST close the
3143 * connection to the server and discard the received response.
3144 *
3145 * 5. If a valid Content-Length header field is present without
3146 * Transfer-Encoding, its decimal value defines the expected message
3147 * body length in octets. If the sender closes the connection or
3148 * the recipient times out before the indicated number of octets are
3149 * received, the recipient MUST consider the message to be
3150 * incomplete and close the connection.
3151 *
3152 * 6. If this is a request message and none of the above are true, then
3153 * the message body length is zero (no message body is present).
3154 *
3155 * 7. Otherwise, this is a response message without a declared message
3156 * body length, so the message body length is determined by the
3157 * number of octets received prior to the server closing the
3158 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003159 */
3160
Willy Tarreau32b47f42009-10-18 20:55:02 +02003161 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003162 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003163 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003164 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003165 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3166 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003167 /* chunked not last, return badreq */
3168 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003169 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003170 }
3171
Willy Tarreau1c913912015-04-30 10:57:51 +02003172 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003173 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003174 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3175 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3176 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3177 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003178 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003179 signed long long cl;
3180
Willy Tarreauad14f752011-09-02 20:33:27 +02003181 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003182 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003183 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003184 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003185
Willy Tarreauad14f752011-09-02 20:33:27 +02003186 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003187 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003188 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003189 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003190
Willy Tarreauad14f752011-09-02 20:33:27 +02003191 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003192 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003193 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003194 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003195
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003196 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003197 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003198 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003199 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003200
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003201 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003202 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003203 }
3204
Willy Tarreau34dfc602015-05-01 10:09:49 +02003205 /* even bodyless requests have a known length */
3206 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003207
Willy Tarreau179085c2014-04-28 16:48:56 +02003208 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3209 * only change if both the request and the config reference something else.
3210 * Option httpclose by itself sets tunnel mode where headers are mangled.
3211 * However, if another mode is set, it will affect it (eg: server-close/
3212 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3213 * if FE and BE have the same settings (common). The method consists in
3214 * checking if options changed between the two calls (implying that either
3215 * one is non-null, or one of them is non-null and we are there for the first
3216 * time.
3217 */
3218 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003219 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003220 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003221
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003222 /* we may have to wait for the request's body */
3223 if ((s->be->options & PR_O_WREQ_BODY) &&
3224 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3225 req->analysers |= AN_REQ_HTTP_BODY;
3226
Willy Tarreaud787e662009-07-07 10:14:51 +02003227 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003228 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003229 req->analyse_exp = TICK_ETERNITY;
3230 return 1;
3231
3232 return_bad_req:
3233 /* We centralize bad requests processing here */
3234 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3235 /* we detected a parsing error. We want to archive this request
3236 * in the dedicated proxy area for later troubleshooting.
3237 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003238 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003239 }
3240
3241 txn->req.msg_state = HTTP_MSG_ERROR;
3242 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01003243 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003244
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003245 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003246 if (sess->listener->counters)
3247 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003248
3249 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003250 if (!(s->flags & SF_ERR_MASK))
3251 s->flags |= SF_ERR_PRXCOND;
3252 if (!(s->flags & SF_FINST_MASK))
3253 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003254
3255 req->analysers = 0;
3256 req->analyse_exp = TICK_ETERNITY;
3257 return 0;
3258}
3259
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003260
Willy Tarreau347a35d2013-11-22 17:51:09 +01003261/* This function prepares an applet to handle the stats. It can deal with the
3262 * "100-continue" expectation, check that admin rules are met for POST requests,
3263 * and program a response message if something was unexpected. It cannot fail
3264 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003265 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003266 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003267 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003268 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003269int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003270{
3271 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003272 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003273 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003274 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003275 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003276 struct uri_auth *uri_auth = s->be->uri_auth;
3277 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003278 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003279
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003280 appctx = si_appctx(si);
3281 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3282 appctx->st1 = appctx->st2 = 0;
3283 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3284 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003285 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003286 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003287
3288 uri = msg->chn->buf->p + msg->sl.rq.u;
3289 lookup = uri + uri_auth->uri_len;
3290
3291 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3292 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003293 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003294 break;
3295 }
3296 }
3297
3298 if (uri_auth->refresh) {
3299 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3300 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003301 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003302 break;
3303 }
3304 }
3305 }
3306
3307 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3308 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003309 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003310 break;
3311 }
3312 }
3313
3314 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3315 if (memcmp(h, ";st=", 4) == 0) {
3316 int i;
3317 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003318 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003319 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3320 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003321 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003322 break;
3323 }
3324 }
3325 break;
3326 }
3327 }
3328
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003329 appctx->ctx.stats.scope_str = 0;
3330 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003331 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3332 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3333 int itx = 0;
3334 const char *h2;
3335 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3336 const char *err;
3337
3338 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3339 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003340 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003341 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3342 itx++;
3343 h++;
3344 }
3345
3346 if (itx > STAT_SCOPE_TXT_MAXLEN)
3347 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003348 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003349
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003350 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003351 memcpy(scope_txt, h2, itx);
3352 scope_txt[itx] = '\0';
3353 err = invalid_char(scope_txt);
3354 if (err) {
3355 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003356 appctx->ctx.stats.scope_str = 0;
3357 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003358 }
3359 break;
3360 }
3361 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003362
3363 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003364 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003365 int ret = 1;
3366
3367 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003368 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 +01003369 ret = acl_pass(ret);
3370 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3371 ret = !ret;
3372 }
3373
3374 if (ret) {
3375 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003376 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003377 break;
3378 }
3379 }
3380
3381 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003382 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003383 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003384 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003385 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3386 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003387 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003388 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003389 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003390 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3391 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003392 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003393 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003394 else {
3395 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003396 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003397 }
3398
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003399 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003400 return 1;
3401}
3402
Lukas Tribus67db8df2013-06-23 17:37:13 +02003403/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3404 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3405 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003406void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003407{
3408#ifdef IP_TOS
3409 if (from.ss_family == AF_INET)
3410 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3411#endif
3412#ifdef IPV6_TCLASS
3413 if (from.ss_family == AF_INET6) {
3414 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3415 /* v4-mapped addresses need IP_TOS */
3416 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3417 else
3418 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3419 }
3420#endif
3421}
3422
Willy Tarreau87b09662015-04-03 00:22:06 +02003423int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003424 const char* name, unsigned int name_len,
3425 const char *str, struct my_regex *re,
3426 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003427{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003428 struct hdr_ctx ctx;
3429 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003430 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003431 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3432 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003433 struct chunk *output = get_trash_chunk();
3434
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003435 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003436
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003437 /* Choose the header browsing function. */
3438 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003439 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003440 http_find_hdr_func = http_find_header2;
3441 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003442 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003443 http_find_hdr_func = http_find_full_header2;
3444 break;
3445 default: /* impossible */
3446 return -1;
3447 }
3448
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003449 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3450 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003451 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003452 char *val = ctx.line + ctx.val;
3453 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003454
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003455 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3456 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003457
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003458 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003459 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003460 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003461
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003462 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003463
3464 hdr->len += delta;
3465 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003466
3467 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003468 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003469 }
3470
3471 return 0;
3472}
3473
Willy Tarreau87b09662015-04-03 00:22:06 +02003474static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003475 const char* name, unsigned int name_len,
3476 struct list *fmt, struct my_regex *re,
3477 int action)
3478{
3479 struct chunk *replace = get_trash_chunk();
3480
3481 replace->len = build_logline(s, replace->str, replace->size, fmt);
3482 if (replace->len >= replace->size - 1)
3483 return -1;
3484
3485 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3486}
3487
Willy Tarreau87b09662015-04-03 00:22:06 +02003488/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003489 * transaction <txn>. Returns the verdict of the first rule that prevents
3490 * further processing of the request (auth, deny, ...), and defaults to
3491 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3492 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3493 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003494 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003495enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003496http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003497{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003498 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003499 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003500 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003501 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003502 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003503 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003504
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003505 /* If "the current_rule_list" match the executed rule list, we are in
3506 * resume condition. If a resume is needed it is always in the action
3507 * and never in the ACL or converters. In this case, we initialise the
3508 * current rule, and go to the action execution point.
3509 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003510 if (s->current_rule) {
3511 rule = s->current_rule;
3512 s->current_rule = NULL;
3513 if (s->current_rule_list == rules)
3514 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003515 }
3516 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003517
Willy Tarreauff011f22011-01-06 17:51:27 +01003518 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003519
Willy Tarreau96257ec2012-12-27 10:46:37 +01003520 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003521 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003522 int ret;
3523
Willy Tarreau192252e2015-04-04 01:47:55 +02003524 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003525 ret = acl_pass(ret);
3526
Willy Tarreauff011f22011-01-06 17:51:27 +01003527 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003528 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003529
3530 if (!ret) /* condition not matched */
3531 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003532 }
3533
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003534resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003535 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003536 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003537 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003538
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003539 case ACT_ACTION_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003540 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003541 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003542
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003543 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003544 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003545 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003546 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003547
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003548 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003549 /* Auth might be performed on regular http-req rules as well as on stats */
3550 auth_realm = rule->arg.auth.realm;
3551 if (!auth_realm) {
3552 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3553 auth_realm = STATS_DEFAULT_REALM;
3554 else
3555 auth_realm = px->id;
3556 }
3557 /* send 401/407 depending on whether we use a proxy or not. We still
3558 * count one error, because normal browsing won't significantly
3559 * increase the counter but brute force attempts will.
3560 */
3561 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3562 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3563 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003564 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003565 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003566
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003567 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003568 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3569 return HTTP_RULE_RES_BADREQ;
3570 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003571
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003572 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003573 s->task->nice = rule->arg.nice;
3574 break;
3575
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003576 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003577 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003578 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003579 break;
3580
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003581 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003582#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003583 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003584 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003585#endif
3586 break;
3587
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003588 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003589 s->logs.level = rule->arg.loglevel;
3590 break;
3591
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003592 case ACT_HTTP_REPLACE_HDR:
3593 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003594 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3595 rule->arg.hdr_add.name_len,
3596 &rule->arg.hdr_add.fmt,
3597 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003598 return HTTP_RULE_RES_BADREQ;
3599 break;
3600
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003601 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003602 ctx.idx = 0;
3603 /* remove all occurrences of the header */
3604 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3605 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3606 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003607 }
Willy Tarreau85603282015-01-21 20:39:27 +01003608 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003609
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003610 case ACT_HTTP_SET_HDR:
3611 case ACT_HTTP_ADD_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003612 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3613 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3614 trash.len = rule->arg.hdr_add.name_len;
3615 trash.str[trash.len++] = ':';
3616 trash.str[trash.len++] = ' ';
3617 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 +01003618
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003619 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003620 /* remove all occurrences of the header */
3621 ctx.idx = 0;
3622 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3623 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3624 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3625 }
3626 }
3627
Willy Tarreau96257ec2012-12-27 10:46:37 +01003628 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3629 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003630
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003631 case ACT_HTTP_DEL_ACL:
3632 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003633 struct pat_ref *ref;
3634 char *key;
3635 int len;
3636
3637 /* collect reference */
3638 ref = pat_ref_lookup(rule->arg.map.ref);
3639 if (!ref)
3640 continue;
3641
3642 /* collect key */
3643 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3644 key = trash.str;
3645 key[len] = '\0';
3646
3647 /* perform update */
3648 /* returned code: 1=ok, 0=ko */
3649 pat_ref_delete(ref, key);
3650
3651 break;
3652 }
3653
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003654 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003655 struct pat_ref *ref;
3656 char *key;
3657 struct chunk *trash_key;
3658 int len;
3659
3660 trash_key = get_trash_chunk();
3661
3662 /* collect reference */
3663 ref = pat_ref_lookup(rule->arg.map.ref);
3664 if (!ref)
3665 continue;
3666
3667 /* collect key */
3668 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3669 key = trash_key->str;
3670 key[len] = '\0';
3671
3672 /* perform update */
3673 /* add entry only if it does not already exist */
3674 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003675 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003676
3677 break;
3678 }
3679
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003680 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003681 struct pat_ref *ref;
3682 char *key, *value;
3683 struct chunk *trash_key, *trash_value;
3684 int len;
3685
3686 trash_key = get_trash_chunk();
3687 trash_value = get_trash_chunk();
3688
3689 /* collect reference */
3690 ref = pat_ref_lookup(rule->arg.map.ref);
3691 if (!ref)
3692 continue;
3693
3694 /* collect key */
3695 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3696 key = trash_key->str;
3697 key[len] = '\0';
3698
3699 /* collect value */
3700 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3701 value = trash_value->str;
3702 value[len] = '\0';
3703
3704 /* perform update */
3705 if (pat_ref_find_elt(ref, key) != NULL)
3706 /* update entry if it exists */
3707 pat_ref_set(ref, key, value, NULL);
3708 else
3709 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003710 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003711
3712 break;
3713 }
William Lallemand73025dd2014-04-24 14:38:37 +02003714
Thierry FOURNIER91f6ba02015-08-06 08:30:11 +02003715 case ACT_ACTION_CONT:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003716 switch (rule->action_ptr(rule, px, s->sess, s)) {
3717 case ACT_RET_ERR:
3718 case ACT_RET_CONT:
3719 break;
3720 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003721 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003722 return HTTP_RULE_RES_YIELD;
3723 }
William Lallemand73025dd2014-04-24 14:38:37 +02003724 break;
3725
Thierry FOURNIER91f6ba02015-08-06 08:30:11 +02003726 case ACT_ACTION_STOP:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003727 switch (rule->action_ptr(rule, px, s->sess, s)) {
3728 case ACT_RET_YIELD:
3729 case ACT_RET_ERR:
3730 case ACT_RET_CONT:
3731 return HTTP_RULE_RES_DONE;
3732 }
3733 break;
Willy Tarreau09448f72014-06-25 18:12:15 +02003734
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003735 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003736 /* Note: only the first valid tracking parameter of each
3737 * applies.
3738 */
3739
3740 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3741 struct stktable *t;
3742 struct stksess *ts;
3743 struct stktable_key *key;
3744 void *ptr;
3745
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003746 t = rule->arg.trk_ctr.table.t;
3747 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 +02003748
3749 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003750 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003751
3752 /* let's count a new HTTP request as it's the first time we do it */
3753 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3754 if (ptr)
3755 stktable_data_cast(ptr, http_req_cnt)++;
3756
3757 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3758 if (ptr)
3759 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3760 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3761
3762 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003763 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003764 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3765 }
3766 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003767 break;
3768
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003769 case ACT_HTTP_REQ_SET_SRC:
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003770 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
3771 struct sample *smp;
3772
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02003773 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 +02003774
3775 if (smp) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003776 if (smp->data.type == SMP_T_IPV4) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003777 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003778 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003779 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003780 } else if (smp->data.type == SMP_T_IPV6) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003781 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003782 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 +02003783 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0;
3784 }
3785 }
3786 }
3787 break;
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003788
3789 /* other flags exists, but normaly, they never be matched. */
3790 default:
3791 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003792 }
3793 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003794
3795 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003796 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003797}
3798
Willy Tarreau71241ab2012-12-27 11:30:54 +01003799
Willy Tarreau51d861a2015-05-22 17:30:48 +02003800/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3801 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3802 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3803 * is returned, the process can continue the evaluation of next rule list. If
3804 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3805 * is returned, it means the operation could not be processed and a server error
3806 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3807 * deny rule. If *YIELD is returned, the caller must call again the function
3808 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003809 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003810static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003811http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003812{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003813 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003814 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003815 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003816 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003817 struct hdr_ctx ctx;
3818
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003819 /* If "the current_rule_list" match the executed rule list, we are in
3820 * resume condition. If a resume is needed it is always in the action
3821 * and never in the ACL or converters. In this case, we initialise the
3822 * current rule, and go to the action execution point.
3823 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003824 if (s->current_rule) {
3825 rule = s->current_rule;
3826 s->current_rule = NULL;
3827 if (s->current_rule_list == rules)
3828 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003829 }
3830 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003831
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003832 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003833
3834 /* check optional condition */
3835 if (rule->cond) {
3836 int ret;
3837
Willy Tarreau192252e2015-04-04 01:47:55 +02003838 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003839 ret = acl_pass(ret);
3840
3841 if (rule->cond->pol == ACL_COND_UNLESS)
3842 ret = !ret;
3843
3844 if (!ret) /* condition not matched */
3845 continue;
3846 }
3847
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003848resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003849 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003850 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003851 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003852
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003853 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003854 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003855 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003856
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003857 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003858 s->task->nice = rule->arg.nice;
3859 break;
3860
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003861 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003862 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003863 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003864 break;
3865
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003866 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003867#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003868 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003869 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003870#endif
3871 break;
3872
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003873 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003874 s->logs.level = rule->arg.loglevel;
3875 break;
3876
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003877 case ACT_HTTP_REPLACE_HDR:
3878 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003879 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3880 rule->arg.hdr_add.name_len,
3881 &rule->arg.hdr_add.fmt,
3882 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003883 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003884 break;
3885
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003886 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003887 ctx.idx = 0;
3888 /* remove all occurrences of the header */
3889 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3890 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3891 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3892 }
Willy Tarreau85603282015-01-21 20:39:27 +01003893 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003894
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003895 case ACT_HTTP_SET_HDR:
3896 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003897 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3898 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3899 trash.len = rule->arg.hdr_add.name_len;
3900 trash.str[trash.len++] = ':';
3901 trash.str[trash.len++] = ' ';
3902 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 +01003903
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003904 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003905 /* remove all occurrences of the header */
3906 ctx.idx = 0;
3907 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3908 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3909 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3910 }
3911 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003912 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3913 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003914
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003915 case ACT_HTTP_DEL_ACL:
3916 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003917 struct pat_ref *ref;
3918 char *key;
3919 int len;
3920
3921 /* collect reference */
3922 ref = pat_ref_lookup(rule->arg.map.ref);
3923 if (!ref)
3924 continue;
3925
3926 /* collect key */
3927 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3928 key = trash.str;
3929 key[len] = '\0';
3930
3931 /* perform update */
3932 /* returned code: 1=ok, 0=ko */
3933 pat_ref_delete(ref, key);
3934
3935 break;
3936 }
3937
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003938 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003939 struct pat_ref *ref;
3940 char *key;
3941 struct chunk *trash_key;
3942 int len;
3943
3944 trash_key = get_trash_chunk();
3945
3946 /* collect reference */
3947 ref = pat_ref_lookup(rule->arg.map.ref);
3948 if (!ref)
3949 continue;
3950
3951 /* collect key */
3952 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3953 key = trash_key->str;
3954 key[len] = '\0';
3955
3956 /* perform update */
3957 /* check if the entry already exists */
3958 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003959 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003960
3961 break;
3962 }
3963
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003964 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003965 struct pat_ref *ref;
3966 char *key, *value;
3967 struct chunk *trash_key, *trash_value;
3968 int len;
3969
3970 trash_key = get_trash_chunk();
3971 trash_value = get_trash_chunk();
3972
3973 /* collect reference */
3974 ref = pat_ref_lookup(rule->arg.map.ref);
3975 if (!ref)
3976 continue;
3977
3978 /* collect key */
3979 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3980 key = trash_key->str;
3981 key[len] = '\0';
3982
3983 /* collect value */
3984 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3985 value = trash_value->str;
3986 value[len] = '\0';
3987
3988 /* perform update */
3989 if (pat_ref_find_elt(ref, key) != NULL)
3990 /* update entry if it exists */
3991 pat_ref_set(ref, key, value, NULL);
3992 else
3993 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003994 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003995
3996 break;
3997 }
William Lallemand73025dd2014-04-24 14:38:37 +02003998
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003999 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02004000 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
4001 return HTTP_RULE_RES_BADREQ;
4002 return HTTP_RULE_RES_DONE;
4003
Thierry FOURNIER91f6ba02015-08-06 08:30:11 +02004004 case ACT_ACTION_CONT:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004005 switch (rule->action_ptr(rule, px, s->sess, s)) {
4006 case ACT_RET_ERR:
4007 case ACT_RET_CONT:
4008 break;
4009 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02004010 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004011 return HTTP_RULE_RES_YIELD;
4012 }
William Lallemand73025dd2014-04-24 14:38:37 +02004013 break;
4014
Thierry FOURNIER91f6ba02015-08-06 08:30:11 +02004015 case ACT_ACTION_STOP:
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02004016 rule->action_ptr(rule, px, s->sess, s);
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004017 return HTTP_RULE_RES_STOP;
Thierry FOURNIER22e49012015-08-05 19:13:48 +02004018
4019 /* other flags exists, but normaly, they never be matched. */
4020 default:
4021 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004022 }
4023 }
4024
4025 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004026 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004027}
4028
4029
Willy Tarreau71241ab2012-12-27 11:30:54 +01004030/* Perform an HTTP redirect based on the information in <rule>. The function
4031 * returns non-zero on success, or zero in case of a, irrecoverable error such
4032 * as too large a request to build a valid response.
4033 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004034static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004035{
Willy Tarreaub329a312015-05-22 16:27:37 +02004036 struct http_msg *req = &txn->req;
4037 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004038 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004039 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004040
4041 /* build redirect message */
4042 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004043 case 308:
4044 msg_fmt = HTTP_308;
4045 break;
4046 case 307:
4047 msg_fmt = HTTP_307;
4048 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004049 case 303:
4050 msg_fmt = HTTP_303;
4051 break;
4052 case 301:
4053 msg_fmt = HTTP_301;
4054 break;
4055 case 302:
4056 default:
4057 msg_fmt = HTTP_302;
4058 break;
4059 }
4060
4061 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
4062 return 0;
4063
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004064 location = trash.str + trash.len;
4065
Willy Tarreau71241ab2012-12-27 11:30:54 +01004066 switch(rule->type) {
4067 case REDIRECT_TYPE_SCHEME: {
4068 const char *path;
4069 const char *host;
4070 struct hdr_ctx ctx;
4071 int pathlen;
4072 int hostlen;
4073
4074 host = "";
4075 hostlen = 0;
4076 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004077 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004078 host = ctx.line + ctx.val;
4079 hostlen = ctx.vlen;
4080 }
4081
4082 path = http_get_path(txn);
4083 /* build message using path */
4084 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004085 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004086 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4087 int qs = 0;
4088 while (qs < pathlen) {
4089 if (path[qs] == '?') {
4090 pathlen = qs;
4091 break;
4092 }
4093 qs++;
4094 }
4095 }
4096 } else {
4097 path = "/";
4098 pathlen = 1;
4099 }
4100
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004101 if (rule->rdr_str) { /* this is an old "redirect" rule */
4102 /* check if we can add scheme + "://" + host + path */
4103 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
4104 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004105
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004106 /* add scheme */
4107 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4108 trash.len += rule->rdr_len;
4109 }
4110 else {
4111 /* add scheme with executing log format */
4112 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004113
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004114 /* check if we can add scheme + "://" + host + path */
4115 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
4116 return 0;
4117 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004118 /* add "://" */
4119 memcpy(trash.str + trash.len, "://", 3);
4120 trash.len += 3;
4121
4122 /* add host */
4123 memcpy(trash.str + trash.len, host, hostlen);
4124 trash.len += hostlen;
4125
4126 /* add path */
4127 memcpy(trash.str + trash.len, path, pathlen);
4128 trash.len += pathlen;
4129
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004130 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004131 if (trash.len && trash.str[trash.len - 1] != '/' &&
4132 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4133 if (trash.len > trash.size - 5)
4134 return 0;
4135 trash.str[trash.len] = '/';
4136 trash.len++;
4137 }
4138
4139 break;
4140 }
4141 case REDIRECT_TYPE_PREFIX: {
4142 const char *path;
4143 int pathlen;
4144
4145 path = http_get_path(txn);
4146 /* build message using path */
4147 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004148 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004149 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4150 int qs = 0;
4151 while (qs < pathlen) {
4152 if (path[qs] == '?') {
4153 pathlen = qs;
4154 break;
4155 }
4156 qs++;
4157 }
4158 }
4159 } else {
4160 path = "/";
4161 pathlen = 1;
4162 }
4163
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004164 if (rule->rdr_str) { /* this is an old "redirect" rule */
4165 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4166 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004167
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004168 /* add prefix. Note that if prefix == "/", we don't want to
4169 * add anything, otherwise it makes it hard for the user to
4170 * configure a self-redirection.
4171 */
4172 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4173 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4174 trash.len += rule->rdr_len;
4175 }
4176 }
4177 else {
4178 /* add prefix with executing log format */
4179 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4180
4181 /* Check length */
4182 if (trash.len + pathlen > trash.size - 4)
4183 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004184 }
4185
4186 /* add path */
4187 memcpy(trash.str + trash.len, path, pathlen);
4188 trash.len += pathlen;
4189
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004190 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004191 if (trash.len && trash.str[trash.len - 1] != '/' &&
4192 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4193 if (trash.len > trash.size - 5)
4194 return 0;
4195 trash.str[trash.len] = '/';
4196 trash.len++;
4197 }
4198
4199 break;
4200 }
4201 case REDIRECT_TYPE_LOCATION:
4202 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004203 if (rule->rdr_str) { /* this is an old "redirect" rule */
4204 if (trash.len + rule->rdr_len > trash.size - 4)
4205 return 0;
4206
4207 /* add location */
4208 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4209 trash.len += rule->rdr_len;
4210 }
4211 else {
4212 /* add location with executing log format */
4213 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004214
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004215 /* Check left length */
4216 if (trash.len > trash.size - 4)
4217 return 0;
4218 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004219 break;
4220 }
4221
4222 if (rule->cookie_len) {
4223 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4224 trash.len += 14;
4225 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4226 trash.len += rule->cookie_len;
4227 memcpy(trash.str + trash.len, "\r\n", 2);
4228 trash.len += 2;
4229 }
4230
4231 /* add end of headers and the keep-alive/close status.
4232 * We may choose to set keep-alive if the Location begins
4233 * with a slash, because the client will come back to the
4234 * same server.
4235 */
4236 txn->status = rule->code;
4237 /* let's log the request time */
4238 s->logs.tv_request = now;
4239
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004240 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004241 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004242 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004243 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4244 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4245 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004246 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004247 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4248 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4249 trash.len += 30;
4250 } else {
4251 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4252 trash.len += 24;
4253 }
4254 }
4255 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4256 trash.len += 4;
Willy Tarreaub329a312015-05-22 16:27:37 +02004257 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004258 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004259 bi_fast_delete(req->chn->buf, req->sov);
4260 req->next -= req->sov;
4261 req->sov = 0;
4262 s->req.analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004263 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreaub329a312015-05-22 16:27:37 +02004264 req->msg_state = HTTP_MSG_CLOSED;
4265 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004266 /* Trim any possible response */
4267 res->chn->buf->i = 0;
4268 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004269 } else {
4270 /* keep-alive not possible */
4271 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4272 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4273 trash.len += 29;
4274 } else {
4275 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4276 trash.len += 23;
4277 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004278 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreaub329a312015-05-22 16:27:37 +02004279 req->chn->analysers = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004280 }
4281
Willy Tarreaue7dff022015-04-03 01:14:29 +02004282 if (!(s->flags & SF_ERR_MASK))
4283 s->flags |= SF_ERR_LOCAL;
4284 if (!(s->flags & SF_FINST_MASK))
4285 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004286
4287 return 1;
4288}
4289
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004290/* This stream analyser runs all HTTP request processing which is common to
4291 * frontends and backends, which means blocking ACLs, filters, connection-close,
4292 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004293 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004294 * either needs more data or wants to immediately abort the request (eg: deny,
4295 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004296 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004297int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004298{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004299 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004300 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004301 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004302 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004303 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004304 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004305
Willy Tarreau655dce92009-11-08 13:10:58 +01004306 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004307 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004308 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004309 }
4310
Willy Tarreau87b09662015-04-03 00:22:06 +02004311 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02004312 now_ms, __FUNCTION__,
4313 s,
4314 req,
4315 req->rex, req->wex,
4316 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004317 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004318 req->analysers);
4319
Willy Tarreau65410832014-04-28 21:25:43 +02004320 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004321 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004322
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004323 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004324 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004325 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004326
Willy Tarreau0b748332014-04-29 00:13:29 +02004327 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004328 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4329 goto return_prx_yield;
4330
Willy Tarreau0b748332014-04-29 00:13:29 +02004331 case HTTP_RULE_RES_CONT:
4332 case HTTP_RULE_RES_STOP: /* nothing to do */
4333 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004334
Willy Tarreau0b748332014-04-29 00:13:29 +02004335 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4336 if (txn->flags & TX_CLTARPIT)
4337 goto tarpit;
4338 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004339
Willy Tarreau0b748332014-04-29 00:13:29 +02004340 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4341 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004342
Willy Tarreau0b748332014-04-29 00:13:29 +02004343 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004344 goto done;
4345
Willy Tarreau0b748332014-04-29 00:13:29 +02004346 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4347 goto return_bad_req;
4348 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004349 }
4350
Willy Tarreau52542592014-04-28 18:33:26 +02004351 /* OK at this stage, we know that the request was accepted according to
4352 * the http-request rules, we can check for the stats. Note that the
4353 * URI is detected *before* the req* rules in order not to be affected
4354 * by a possible reqrep, while they are processed *after* so that a
4355 * reqdeny can still block them. This clearly needs to change in 1.6!
4356 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004357 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004358 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004359 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004360 txn->status = 500;
4361 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004362 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004363
Willy Tarreaue7dff022015-04-03 01:14:29 +02004364 if (!(s->flags & SF_ERR_MASK))
4365 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004366 goto return_prx_cond;
4367 }
4368
4369 /* parse the whole stats request and extract the relevant information */
4370 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004371 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004372 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004373
Willy Tarreau0b748332014-04-29 00:13:29 +02004374 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4375 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004376
Willy Tarreau0b748332014-04-29 00:13:29 +02004377 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4378 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004379 }
4380
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004381 /* evaluate the req* rules except reqadd */
4382 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004383 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004384 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004385
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004386 if (txn->flags & TX_CLDENY)
4387 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004388
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004389 if (txn->flags & TX_CLTARPIT)
4390 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004391 }
Willy Tarreau06619262006-12-17 08:37:22 +01004392
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004393 /* add request headers from the rule sets in the same order */
4394 list_for_each_entry(wl, &px->req_add, list) {
4395 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004396 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004397 ret = acl_pass(ret);
4398 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4399 ret = !ret;
4400 if (!ret)
4401 continue;
4402 }
4403
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004404 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004405 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004406 }
4407
Willy Tarreau52542592014-04-28 18:33:26 +02004408
4409 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004410 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004411 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004412 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4413 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004414
Willy Tarreaue7dff022015-04-03 01:14:29 +02004415 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4416 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4417 if (!(s->flags & SF_FINST_MASK))
4418 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004419
Willy Tarreau70730dd2014-04-24 18:06:27 +02004420 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004421 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004422 select_compression_request_header(s, req->buf);
4423
4424 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004425 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004426 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004427 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004428
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004429 /* check whether we have some ACLs set to redirect this request */
4430 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004431 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004432 int ret;
4433
Willy Tarreau192252e2015-04-04 01:47:55 +02004434 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004435 ret = acl_pass(ret);
4436 if (rule->cond->pol == ACL_COND_UNLESS)
4437 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004438 if (!ret)
4439 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004440 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004441 if (!http_apply_redirect_rule(rule, s, txn))
4442 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004443 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004444 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004445
Willy Tarreau2be39392010-01-03 17:24:51 +01004446 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4447 * If this happens, then the data will not come immediately, so we must
4448 * send all what we have without waiting. Note that due to the small gain
4449 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004450 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004451 * itself once used.
4452 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004453 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004454
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004455 done: /* done with this analyser, continue with next ones that the calling
4456 * points will have set, if any.
4457 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004458 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004459 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4460 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004461 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004462
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004463 tarpit:
4464 /* When a connection is tarpitted, we use the tarpit timeout,
4465 * which may be the same as the connect timeout if unspecified.
4466 * If unset, then set it to zero because we really want it to
4467 * eventually expire. We build the tarpit as an analyser.
4468 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004469 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004470
4471 /* wipe the request out so that we can drop the connection early
4472 * if the client closes first.
4473 */
4474 channel_dont_connect(req);
4475 req->analysers = 0; /* remove switching rules etc... */
4476 req->analysers |= AN_REQ_HTTP_TARPIT;
4477 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4478 if (!req->analyse_exp)
4479 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004480 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004481 sess->fe->fe_counters.denied_req++;
4482 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004483 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004484 if (sess->listener->counters)
4485 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004486 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004487
4488 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004489 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004490 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004491 s->logs.tv_request = now;
CJ Ess108b1dd2015-04-07 12:03:37 -04004492 stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004493 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004494 sess->fe->fe_counters.denied_req++;
4495 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004496 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004497 if (sess->listener->counters)
4498 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004499 goto return_prx_cond;
4500
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004501 return_bad_req:
4502 /* We centralize bad requests processing here */
4503 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4504 /* we detected a parsing error. We want to archive this request
4505 * in the dedicated proxy area for later troubleshooting.
4506 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004507 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004508 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004509
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004510 txn->req.msg_state = HTTP_MSG_ERROR;
4511 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004512 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004513
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004514 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004515 if (sess->listener->counters)
4516 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004517
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004518 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004519 if (!(s->flags & SF_ERR_MASK))
4520 s->flags |= SF_ERR_PRXCOND;
4521 if (!(s->flags & SF_FINST_MASK))
4522 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004523
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004524 req->analysers = 0;
4525 req->analyse_exp = TICK_ETERNITY;
4526 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004527
4528 return_prx_yield:
4529 channel_dont_connect(req);
4530 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004531}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004532
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004533/* This function performs all the processing enabled for the current request.
4534 * It returns 1 if the processing can continue on next analysers, or zero if it
4535 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004536 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004537 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004538int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004539{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004540 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004541 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004542 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004543 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004544
Willy Tarreau655dce92009-11-08 13:10:58 +01004545 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004546 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004547 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004548 return 0;
4549 }
4550
Willy Tarreau87b09662015-04-03 00:22:06 +02004551 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004552 now_ms, __FUNCTION__,
4553 s,
4554 req,
4555 req->rex, req->wex,
4556 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004557 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004558 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004559
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004560 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004561 select_compression_request_header(s, req->buf);
4562
Willy Tarreau59234e92008-11-30 23:51:27 +01004563 /*
4564 * Right now, we know that we have processed the entire headers
4565 * and that unwanted requests have been filtered out. We can do
4566 * whatever we want with the remaining request. Also, now we
4567 * may have separate values for ->fe, ->be.
4568 */
Willy Tarreau06619262006-12-17 08:37:22 +01004569
Willy Tarreau59234e92008-11-30 23:51:27 +01004570 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004571 * If HTTP PROXY is set we simply get remote server address parsing
4572 * incoming request. Note that this requires that a connection is
4573 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004574 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004575 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004576 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004577 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004578
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004579 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004580 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004581 txn->req.msg_state = HTTP_MSG_ERROR;
4582 txn->status = 500;
4583 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004584 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004585
Willy Tarreaue7dff022015-04-03 01:14:29 +02004586 if (!(s->flags & SF_ERR_MASK))
4587 s->flags |= SF_ERR_RESOURCE;
4588 if (!(s->flags & SF_FINST_MASK))
4589 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004590
4591 return 0;
4592 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004593
4594 path = http_get_path(txn);
4595 url2sa(req->buf->p + msg->sl.rq.u,
4596 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004597 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004598 /* if the path was found, we have to remove everything between
4599 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4600 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4601 * u_l characters by a single "/".
4602 */
4603 if (path) {
4604 char *cur_ptr = req->buf->p;
4605 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4606 int delta;
4607
4608 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4609 http_msg_move_end(&txn->req, delta);
4610 cur_end += delta;
4611 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4612 goto return_bad_req;
4613 }
4614 else {
4615 char *cur_ptr = req->buf->p;
4616 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4617 int delta;
4618
4619 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4620 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4621 http_msg_move_end(&txn->req, delta);
4622 cur_end += delta;
4623 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4624 goto return_bad_req;
4625 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004626 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004627
Willy Tarreau59234e92008-11-30 23:51:27 +01004628 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004629 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004630 * Note that doing so might move headers in the request, but
4631 * the fields will stay coherent and the URI will not move.
4632 * This should only be performed in the backend.
4633 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02004634 if ((s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004635 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4636 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004637
William Lallemanda73203e2012-03-12 12:48:57 +01004638 /* add unique-id if "header-unique-id" is specified */
4639
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004640 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004641 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4642 goto return_bad_req;
4643 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004644 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004645 }
William Lallemanda73203e2012-03-12 12:48:57 +01004646
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004647 if (sess->fe->header_unique_id && s->unique_id) {
4648 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004649 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004650 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004651 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004652 goto return_bad_req;
4653 }
4654
Cyril Bontéb21570a2009-11-29 20:04:48 +01004655 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004656 * 9: add X-Forwarded-For if either the frontend or the backend
4657 * asks for it.
4658 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004659 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004660 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004661 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4662 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4663 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004664 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004665 /* The header is set to be added only if none is present
4666 * and we found it, so don't do anything.
4667 */
4668 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004669 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004670 /* Add an X-Forwarded-For header unless the source IP is
4671 * in the 'except' network range.
4672 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004673 if ((!sess->fe->except_mask.s_addr ||
4674 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4675 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004676 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004677 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004678 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004679 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004680 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004681 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004682
4683 /* Note: we rely on the backend to get the header name to be used for
4684 * x-forwarded-for, because the header is really meant for the backends.
4685 * However, if the backend did not specify any option, we have to rely
4686 * on the frontend's header name.
4687 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004688 if (s->be->fwdfor_hdr_len) {
4689 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004690 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004691 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004692 len = sess->fe->fwdfor_hdr_len;
4693 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004694 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004695 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01004696
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004697 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004698 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004699 }
4700 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004701 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004702 /* FIXME: for the sake of completeness, we should also support
4703 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004704 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004705 int len;
4706 char pn[INET6_ADDRSTRLEN];
4707 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004708 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004709 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004710
Willy Tarreau59234e92008-11-30 23:51:27 +01004711 /* Note: we rely on the backend to get the header name to be used for
4712 * x-forwarded-for, because the header is really meant for the backends.
4713 * However, if the backend did not specify any option, we have to rely
4714 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004715 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004716 if (s->be->fwdfor_hdr_len) {
4717 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004718 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004719 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004720 len = sess->fe->fwdfor_hdr_len;
4721 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004722 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004723 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004724
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004725 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004726 goto return_bad_req;
4727 }
4728 }
4729
4730 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004731 * 10: add X-Original-To if either the frontend or the backend
4732 * asks for it.
4733 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004734 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004735
4736 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004737 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004738 /* Add an X-Original-To header unless the destination IP is
4739 * in the 'except' network range.
4740 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004741 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004742
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004743 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004744 ((!sess->fe->except_mask_to.s_addr ||
4745 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4746 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004747 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004748 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02004749 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004750 int len;
4751 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004752 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004753
4754 /* Note: we rely on the backend to get the header name to be used for
4755 * x-original-to, because the header is really meant for the backends.
4756 * However, if the backend did not specify any option, we have to rely
4757 * on the frontend's header name.
4758 */
4759 if (s->be->orgto_hdr_len) {
4760 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004761 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004762 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004763 len = sess->fe->orgto_hdr_len;
4764 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004765 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004766 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004767
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004768 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004769 goto return_bad_req;
4770 }
4771 }
4772 }
4773
Willy Tarreau50fc7772012-11-11 22:19:57 +01004774 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4775 * If an "Upgrade" token is found, the header is left untouched in order not to have
4776 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4777 * "Upgrade" is present in the Connection header.
4778 */
4779 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4780 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004781 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004782 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004783 unsigned int want_flags = 0;
4784
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004785 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004786 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004787 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004788 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004789 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004790 want_flags |= TX_CON_CLO_SET;
4791 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004792 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004793 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004794 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004795 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004796 want_flags |= TX_CON_KAL_SET;
4797 }
4798
4799 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004800 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004801 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004802
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004803
Willy Tarreau522d6c02009-12-06 18:49:18 +01004804 /* If we have no server assigned yet and we're balancing on url_param
4805 * with a POST request, we may be interested in checking the body for
4806 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004807 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004808 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004809 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004810 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004811 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004812 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004813 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004814
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004815 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004816 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004817#ifdef TCP_QUICKACK
4818 /* We expect some data from the client. Unless we know for sure
4819 * we already have a full request, we have to re-enable quick-ack
4820 * in case we previously disabled it, otherwise we might cause
4821 * the client to delay further data.
4822 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004823 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004824 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004825 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004826 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004827 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004828#endif
4829 }
Willy Tarreau03945942009-12-22 16:50:27 +01004830
Willy Tarreau59234e92008-11-30 23:51:27 +01004831 /*************************************************************
4832 * OK, that's finished for the headers. We have done what we *
4833 * could. Let's switch to the DATA state. *
4834 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004835 req->analyse_exp = TICK_ETERNITY;
4836 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004837
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004838 /* if the server closes the connection, we want to immediately react
4839 * and close the socket to save packets and syscalls.
4840 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004841 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004842 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004843
Willy Tarreau59234e92008-11-30 23:51:27 +01004844 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004845 /* OK let's go on with the BODY now */
4846 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004847
Willy Tarreau59234e92008-11-30 23:51:27 +01004848 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004849 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004850 /* we detected a parsing error. We want to archive this request
4851 * in the dedicated proxy area for later troubleshooting.
4852 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004853 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004854 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004855
Willy Tarreau59234e92008-11-30 23:51:27 +01004856 txn->req.msg_state = HTTP_MSG_ERROR;
4857 txn->status = 400;
4858 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004859 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004860
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004861 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004862 if (sess->listener->counters)
4863 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004864
Willy Tarreaue7dff022015-04-03 01:14:29 +02004865 if (!(s->flags & SF_ERR_MASK))
4866 s->flags |= SF_ERR_PRXCOND;
4867 if (!(s->flags & SF_FINST_MASK))
4868 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004869 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004870}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004871
Willy Tarreau60b85b02008-11-30 23:28:40 +01004872/* This function is an analyser which processes the HTTP tarpit. It always
4873 * returns zero, at the beginning because it prevents any other processing
4874 * from occurring, and at the end because it terminates the request.
4875 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004876int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004877{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004878 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004879
4880 /* This connection is being tarpitted. The CLIENT side has
4881 * already set the connect expiration date to the right
4882 * timeout. We just have to check that the client is still
4883 * there and that the timeout has not expired.
4884 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004885 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004886 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004887 !tick_is_expired(req->analyse_exp, now_ms))
4888 return 0;
4889
4890 /* We will set the queue timer to the time spent, just for
4891 * logging purposes. We fake a 500 server error, so that the
4892 * attacker will not suspect his connection has been tarpitted.
4893 * It will not cause trouble to the logs because we can exclude
4894 * the tarpitted connections by filtering on the 'PT' status flags.
4895 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004896 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4897
4898 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004899 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004900 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004901
4902 req->analysers = 0;
4903 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004904
Willy Tarreaue7dff022015-04-03 01:14:29 +02004905 if (!(s->flags & SF_ERR_MASK))
4906 s->flags |= SF_ERR_PRXCOND;
4907 if (!(s->flags & SF_FINST_MASK))
4908 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004909 return 0;
4910}
4911
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004912/* This function is an analyser which waits for the HTTP request body. It waits
4913 * for either the buffer to be full, or the full advertised contents to have
4914 * reached the buffer. It must only be called after the standard HTTP request
4915 * processing has occurred, because it expects the request to be parsed and will
4916 * look for the Expect header. It may send a 100-Continue interim response. It
4917 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4918 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4919 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004920 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004921int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004922{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004923 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004924 struct http_txn *txn = s->txn;
4925 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004926
4927 /* We have to parse the HTTP request body to find any required data.
4928 * "balance url_param check_post" should have been the only way to get
4929 * into this. We were brought here after HTTP header analysis, so all
4930 * related structures are ready.
4931 */
4932
Willy Tarreau890988f2014-04-10 11:59:33 +02004933 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4934 /* This is the first call */
4935 if (msg->msg_state < HTTP_MSG_BODY)
4936 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004937
Willy Tarreau890988f2014-04-10 11:59:33 +02004938 if (msg->msg_state < HTTP_MSG_100_SENT) {
4939 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4940 * send an HTTP/1.1 100 Continue intermediate response.
4941 */
4942 if (msg->flags & HTTP_MSGF_VER_11) {
4943 struct hdr_ctx ctx;
4944 ctx.idx = 0;
4945 /* Expect is allowed in 1.1, look for it */
4946 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4947 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004948 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004949 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004950 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004951 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004952 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004953
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004954 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004955 * req->buf->p still points to the beginning of the message. We
4956 * must save the body in msg->next because it survives buffer
4957 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004958 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004959 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004960
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004961 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004962 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4963 else
4964 msg->msg_state = HTTP_MSG_DATA;
4965 }
4966
Willy Tarreau890988f2014-04-10 11:59:33 +02004967 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4968 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004969 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004970 goto missing_data;
4971
4972 /* OK we have everything we need now */
4973 goto http_end;
4974 }
4975
4976 /* OK here we're parsing a chunked-encoded message */
4977
Willy Tarreau522d6c02009-12-06 18:49:18 +01004978 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004979 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004980 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004981 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004982 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004983 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004984
Willy Tarreau115acb92009-12-26 13:56:06 +01004985 if (!ret)
4986 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004987 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004988 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004989 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004990 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004991 }
4992
Willy Tarreaud98cf932009-12-27 22:54:55 +01004993 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004994 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4995 * for at least a whole chunk or the whole content length bytes after
4996 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004997 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004998 if (msg->msg_state == HTTP_MSG_TRAILERS)
4999 goto http_end;
5000
Willy Tarreaue115b492015-05-01 23:05:14 +02005001 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01005002 goto http_end;
5003
5004 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02005005 /* we get here if we need to wait for more data. If the buffer is full,
5006 * we have the maximum we can expect.
5007 */
5008 if (buffer_full(req->buf, global.tune.maxrewrite))
5009 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01005010
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005011 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01005012 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01005013 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02005014
Willy Tarreaue7dff022015-04-03 01:14:29 +02005015 if (!(s->flags & SF_ERR_MASK))
5016 s->flags |= SF_ERR_CLITO;
5017 if (!(s->flags & SF_FINST_MASK))
5018 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005019 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005020 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005021
5022 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005023 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005024 /* Not enough data. We'll re-use the http-request
5025 * timeout here. Ideally, we should set the timeout
5026 * relative to the accept() date. We just set the
5027 * request timeout once at the beginning of the
5028 * request.
5029 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005030 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005031 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005032 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005033 return 0;
5034 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005035
5036 http_end:
5037 /* The situation will not evolve, so let's give up on the analysis. */
5038 s->logs.tv_request = now; /* update the request timer to reflect full request */
5039 req->analysers &= ~an_bit;
5040 req->analyse_exp = TICK_ETERNITY;
5041 return 1;
5042
5043 return_bad_req: /* let's centralize all bad requests */
5044 txn->req.msg_state = HTTP_MSG_ERROR;
5045 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005046 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005047
Willy Tarreaue7dff022015-04-03 01:14:29 +02005048 if (!(s->flags & SF_ERR_MASK))
5049 s->flags |= SF_ERR_PRXCOND;
5050 if (!(s->flags & SF_FINST_MASK))
5051 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005052
Willy Tarreau522d6c02009-12-06 18:49:18 +01005053 return_err_msg:
5054 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005055 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005056 if (sess->listener->counters)
5057 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005058 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005059}
5060
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005061/* send a server's name with an outgoing request over an established connection.
5062 * Note: this function is designed to be called once the request has been scheduled
5063 * for being forwarded. This is the reason why it rewinds the buffer before
5064 * proceeding.
5065 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005066int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005067
5068 struct hdr_ctx ctx;
5069
Mark Lamourinec2247f02012-01-04 13:02:01 -05005070 char *hdr_name = be->server_id_hdr_name;
5071 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005072 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005073 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005074 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005075
William Lallemandd9e90662012-01-30 17:27:17 +01005076 ctx.idx = 0;
5077
Willy Tarreau211cdec2014-04-17 20:18:08 +02005078 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005079 if (old_o) {
5080 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005081 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005082 txn->req.next += old_o;
5083 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005084 }
5085
Willy Tarreau9b28e032012-10-12 23:49:43 +02005086 old_i = chn->buf->i;
5087 while (http_find_header2(hdr_name, hdr_name_len, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005088 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005089 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005090 }
5091
5092 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005093 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005094 memcpy(hdr_val, hdr_name, hdr_name_len);
5095 hdr_val += hdr_name_len;
5096 *hdr_val++ = ':';
5097 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005098 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5099 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005100
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005101 if (old_o) {
5102 /* If this was a forwarded request, we must readjust the amount of
5103 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005104 * variations. Note that the current state is >= HTTP_MSG_BODY,
5105 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005106 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005107 old_o += chn->buf->i - old_i;
5108 b_adv(chn->buf, old_o);
5109 txn->req.next -= old_o;
5110 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005111 }
5112
Mark Lamourinec2247f02012-01-04 13:02:01 -05005113 return 0;
5114}
5115
Willy Tarreau610ecce2010-01-04 21:15:02 +01005116/* Terminate current transaction and prepare a new one. This is very tricky
5117 * right now but it works.
5118 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005119void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005120{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005121 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005122 struct proxy *fe = strm_fe(s);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005123 struct connection *srv_conn;
5124 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005125 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005126
Willy Tarreau610ecce2010-01-04 21:15:02 +01005127 /* FIXME: We need a more portable way of releasing a backend's and a
5128 * server's connections. We need a safer way to reinitialize buffer
5129 * flags. We also need a more accurate method for computing per-request
5130 * data.
5131 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005132 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005133
Willy Tarreau4213a112013-12-15 10:25:42 +01005134 /* unless we're doing keep-alive, we want to quickly close the connection
5135 * to the server.
5136 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005137 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005138 !si_conn_ready(&s->si[1])) {
5139 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5140 si_shutr(&s->si[1]);
5141 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005142 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005143
Willy Tarreaue7dff022015-04-03 01:14:29 +02005144 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005145 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005146 if (unlikely(s->srv_conn))
5147 sess_change_server(s, NULL);
5148 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005149
5150 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005151 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005152
Willy Tarreaueee5b512015-04-03 23:46:31 +02005153 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005154 int n;
5155
Willy Tarreaueee5b512015-04-03 23:46:31 +02005156 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005157 if (n < 1 || n > 5)
5158 n = 0;
5159
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005160 if (fe->mode == PR_MODE_HTTP) {
5161 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005162 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005163 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005164 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005165 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005166 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005167 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005168 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005169 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005170 s->be->be_counters.p.http.comp_rsp++;
5171 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005172 }
5173
5174 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005175 s->logs.bytes_in -= s->req.buf->i;
5176 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005177
5178 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005179 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005180 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005181 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005182 s->do_log(s);
5183 }
5184
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005185 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005186 stream_stop_content_counters(s);
5187 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005188
Willy Tarreau610ecce2010-01-04 21:15:02 +01005189 s->logs.accept_date = date; /* user-visible date for logging */
5190 s->logs.tv_accept = now; /* corrected date for internal use */
5191 tv_zero(&s->logs.tv_request);
5192 s->logs.t_queue = -1;
5193 s->logs.t_connect = -1;
5194 s->logs.t_data = -1;
5195 s->logs.t_close = 0;
5196 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5197 s->logs.srv_queue_size = 0; /* we will get this number soon */
5198
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005199 s->logs.bytes_in = s->req.total = s->req.buf->i;
5200 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005201
5202 if (s->pend_pos)
5203 pendconn_free(s->pend_pos);
5204
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005205 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005206 if (s->flags & SF_CURR_SESS) {
5207 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005208 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005209 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005210 if (may_dequeue_tasks(objt_server(s->target), s->be))
5211 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005212 }
5213
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005214 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005215
Willy Tarreau4213a112013-12-15 10:25:42 +01005216 /* only release our endpoint if we don't intend to reuse the
5217 * connection.
5218 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005219 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005220 !si_conn_ready(&s->si[1])) {
5221 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005222 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005223 }
5224
Willy Tarreau350f4872014-11-28 14:42:25 +01005225 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5226 s->si[1].err_type = SI_ET_NONE;
5227 s->si[1].conn_retries = 0; /* used for logging too */
5228 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005229 s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_stream */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005230 s->req.flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WAKE_CONNECT|CF_WROTE_DATA);
5231 s->res.flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA);
Willy Tarreaue7dff022015-04-03 01:14:29 +02005232 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5233 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5234 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005235
Willy Tarreaueee5b512015-04-03 23:46:31 +02005236 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005237 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005238 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005239
5240 if (prev_status == 401 || prev_status == 407) {
5241 /* In HTTP keep-alive mode, if we receive a 401, we still have
5242 * a chance of being able to send the visitor again to the same
5243 * server over the same connection. This is required by some
5244 * broken protocols such as NTLM, and anyway whenever there is
5245 * an opportunity for sending the challenge to the proper place,
5246 * it's better to do it (at least it helps with debugging).
5247 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005248 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau387ebf82015-08-04 19:24:13 +02005249 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005250 }
5251
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005252 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005253 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005254
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005255 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005256 s->req.flags |= CF_NEVER_WAIT;
5257 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005258 }
5259
Willy Tarreau610ecce2010-01-04 21:15:02 +01005260 /* if the request buffer is not empty, it means we're
5261 * about to process another request, so send pending
5262 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005263 * Just don't do this if the buffer is close to be full,
5264 * because the request will wait for it to flush a little
5265 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005266 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005267 if (s->req.buf->i) {
5268 if (s->res.buf->o &&
5269 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5270 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5271 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005272 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005273
5274 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005275 channel_auto_read(&s->req);
5276 channel_auto_close(&s->req);
5277 channel_auto_read(&s->res);
5278 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005279
Willy Tarreau27375622013-12-17 00:00:28 +01005280 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005281 if (srv_conn) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005282 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005283 if (!srv)
5284 si_idle_conn(&s->si[1], NULL);
5285 else if ((srv_conn->flags & CO_FL_PRIVATE) ||
5286 ((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR))
5287 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005288 else if (prev_flags & TX_NOT_FIRST)
5289 /* note: we check the request, not the connection, but
5290 * this is valid for strategies SAFE and AGGR, and in
5291 * case of ALWS, we don't care anyway.
5292 */
5293 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005294 else
5295 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005296 }
Willy Tarreau27375622013-12-17 00:00:28 +01005297
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005298 s->req.analysers = strm_li(s)->analysers;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005299 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005300}
5301
5302
5303/* This function updates the request state machine according to the response
5304 * state machine and buffer flags. It returns 1 if it changes anything (flag
5305 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5306 * it is only used to find when a request/response couple is complete. Both
5307 * this function and its equivalent should loop until both return zero. It
5308 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5309 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005310int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005311{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005312 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005313 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005314 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005315 unsigned int old_state = txn->req.msg_state;
5316
Willy Tarreau610ecce2010-01-04 21:15:02 +01005317 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5318 return 0;
5319
5320 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005321 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005322 * We can shut the read side unless we want to abort_on_close,
5323 * or we have a POST request. The issue with POST requests is
5324 * that some browsers still send a CRLF after the request, and
5325 * this CRLF must be read so that it does not remain in the kernel
5326 * buffers, otherwise a close could cause an RST on some systems
5327 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005328 * Note that if we're using keep-alive on the client side, we'd
5329 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005330 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005331 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005332 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005333 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5334 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5335 !(s->be->options & PR_O_ABRT_CLOSE) &&
5336 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005337 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005338
Willy Tarreau40f151a2012-12-20 12:10:09 +01005339 /* if the server closes the connection, we want to immediately react
5340 * and close the socket to save packets and syscalls.
5341 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005342 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005343
Willy Tarreau610ecce2010-01-04 21:15:02 +01005344 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5345 goto wait_other_side;
5346
5347 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5348 /* The server has not finished to respond, so we
5349 * don't want to move in order not to upset it.
5350 */
5351 goto wait_other_side;
5352 }
5353
5354 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5355 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005356 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005357 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005358 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005359 goto wait_other_side;
5360 }
5361
5362 /* When we get here, it means that both the request and the
5363 * response have finished receiving. Depending on the connection
5364 * mode, we'll have to wait for the last bytes to leave in either
5365 * direction, and sometimes for a close to be effective.
5366 */
5367
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005368 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5369 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005370 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5371 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005372 }
5373 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5374 /* Option forceclose is set, or either side wants to close,
5375 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005376 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005377 * once both states are CLOSED.
5378 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005379 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5380 channel_shutr_now(chn);
5381 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005382 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005383 }
5384 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005385 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5386 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005387 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005388 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5389 channel_auto_read(chn);
5390 txn->req.msg_state = HTTP_MSG_TUNNEL;
5391 chn->flags |= CF_NEVER_WAIT;
5392 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005393 }
5394
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005395 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005396 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005397 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005398
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005399 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005400 txn->req.msg_state = HTTP_MSG_CLOSING;
5401 goto http_msg_closing;
5402 }
5403 else {
5404 txn->req.msg_state = HTTP_MSG_CLOSED;
5405 goto http_msg_closed;
5406 }
5407 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005408 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005409 }
5410
5411 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5412 http_msg_closing:
5413 /* nothing else to forward, just waiting for the output buffer
5414 * to be empty and for the shutw_now to take effect.
5415 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005416 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005417 txn->req.msg_state = HTTP_MSG_CLOSED;
5418 goto http_msg_closed;
5419 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005420 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005421 txn->req.msg_state = HTTP_MSG_ERROR;
5422 goto wait_other_side;
5423 }
5424 }
5425
5426 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5427 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005428 /* see above in MSG_DONE why we only do this in these states */
5429 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5430 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5431 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005432 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005433 goto wait_other_side;
5434 }
5435
5436 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005437 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005438}
5439
5440
5441/* This function updates the response state machine according to the request
5442 * state machine and buffer flags. It returns 1 if it changes anything (flag
5443 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5444 * it is only used to find when a request/response couple is complete. Both
5445 * this function and its equivalent should loop until both return zero. It
5446 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5447 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005448int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005449{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005450 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005451 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005452 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005453 unsigned int old_state = txn->rsp.msg_state;
5454
Willy Tarreau610ecce2010-01-04 21:15:02 +01005455 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5456 return 0;
5457
5458 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5459 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005460 * still monitor the server connection for a possible close
5461 * while the request is being uploaded, so we don't disable
5462 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005463 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005464 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005465
5466 if (txn->req.msg_state == HTTP_MSG_ERROR)
5467 goto wait_other_side;
5468
5469 if (txn->req.msg_state < HTTP_MSG_DONE) {
5470 /* The client seems to still be sending data, probably
5471 * because we got an error response during an upload.
5472 * We have the choice of either breaking the connection
5473 * or letting it pass through. Let's do the later.
5474 */
5475 goto wait_other_side;
5476 }
5477
5478 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5479 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005480 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005481 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005482 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005483 goto wait_other_side;
5484 }
5485
5486 /* When we get here, it means that both the request and the
5487 * response have finished receiving. Depending on the connection
5488 * mode, we'll have to wait for the last bytes to leave in either
5489 * direction, and sometimes for a close to be effective.
5490 */
5491
5492 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5493 /* Server-close mode : shut read and wait for the request
5494 * side to close its output buffer. The caller will detect
5495 * when we're in DONE and the other is in CLOSED and will
5496 * catch that for the final cleanup.
5497 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005498 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5499 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005500 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005501 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5502 /* Option forceclose is set, or either side wants to close,
5503 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005504 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005505 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005506 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005507 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5508 channel_shutr_now(chn);
5509 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005510 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005511 }
5512 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005513 /* The last possible modes are keep-alive and tunnel. Tunnel will
5514 * need to forward remaining data. Keep-alive will need to monitor
5515 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005516 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005517 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005518 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005519 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5520 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005521 }
5522
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005523 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005524 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005525 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005526 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5527 goto http_msg_closing;
5528 }
5529 else {
5530 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5531 goto http_msg_closed;
5532 }
5533 }
5534 goto wait_other_side;
5535 }
5536
5537 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5538 http_msg_closing:
5539 /* nothing else to forward, just waiting for the output buffer
5540 * to be empty and for the shutw_now to take effect.
5541 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005542 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005543 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5544 goto http_msg_closed;
5545 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005546 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005547 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005548 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005549 if (objt_server(s->target))
5550 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005551 goto wait_other_side;
5552 }
5553 }
5554
5555 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5556 http_msg_closed:
5557 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005558 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005559 channel_auto_close(chn);
5560 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005561 goto wait_other_side;
5562 }
5563
5564 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005565 /* We force the response to leave immediately if we're waiting for the
5566 * other side, since there is no pending shutdown to push it out.
5567 */
5568 if (!channel_is_empty(chn))
5569 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005570 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005571}
5572
5573
5574/* Resync the request and response state machines. Return 1 if either state
5575 * changes.
5576 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005577int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005578{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005579 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005580 int old_req_state = txn->req.msg_state;
5581 int old_res_state = txn->rsp.msg_state;
5582
Willy Tarreau610ecce2010-01-04 21:15:02 +01005583 http_sync_req_state(s);
5584 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005585 if (!http_sync_res_state(s))
5586 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005587 if (!http_sync_req_state(s))
5588 break;
5589 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005590
Willy Tarreau610ecce2010-01-04 21:15:02 +01005591 /* OK, both state machines agree on a compatible state.
5592 * There are a few cases we're interested in :
5593 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5594 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5595 * directions, so let's simply disable both analysers.
5596 * - HTTP_MSG_CLOSED on the response only means we must abort the
5597 * request.
5598 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5599 * with server-close mode means we've completed one request and we
5600 * must re-initialize the server connection.
5601 */
5602
5603 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5604 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5605 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5606 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005607 s->req.analysers = 0;
5608 channel_auto_close(&s->req);
5609 channel_auto_read(&s->req);
5610 s->res.analysers = 0;
5611 channel_auto_close(&s->res);
5612 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005613 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005614 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005615 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005616 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005617 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005618 s->res.analysers = 0;
5619 channel_auto_close(&s->res);
5620 channel_auto_read(&s->res);
5621 s->req.analysers = 0;
5622 channel_abort(&s->req);
5623 channel_auto_close(&s->req);
5624 channel_auto_read(&s->req);
5625 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005626 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005627 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5628 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005629 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005630 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5631 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5632 /* server-close/keep-alive: terminate this transaction,
5633 * possibly killing the server connection and reinitialize
5634 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005635 */
5636 http_end_txn_clean_session(s);
5637 }
5638
Willy Tarreau610ecce2010-01-04 21:15:02 +01005639 return txn->req.msg_state != old_req_state ||
5640 txn->rsp.msg_state != old_res_state;
5641}
5642
Willy Tarreaud98cf932009-12-27 22:54:55 +01005643/* This function is an analyser which forwards request body (including chunk
5644 * sizes if any). It is called as soon as we must forward, even if we forward
5645 * zero byte. The only situation where it must not be called is when we're in
5646 * tunnel mode and we want to forward till the close. It's used both to forward
5647 * remaining data and to resync after end of body. It expects the msg_state to
5648 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005649 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005650 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005651 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005652 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005653int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005654{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005655 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005656 struct http_txn *txn = s->txn;
5657 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005658
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005659 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5660 return 0;
5661
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005662 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005663 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005664 /* Output closed while we were sending data. We must abort and
5665 * wake the other side up.
5666 */
5667 msg->msg_state = HTTP_MSG_ERROR;
5668 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005669 return 1;
5670 }
5671
Willy Tarreaud98cf932009-12-27 22:54:55 +01005672 /* Note that we don't have to send 100-continue back because we don't
5673 * need the data to complete our job, and it's up to the server to
5674 * decide whether to return 100, 417 or anything else in return of
5675 * an "Expect: 100-continue" header.
5676 */
5677
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005678 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005679 /* we have msg->sov which points to the first byte of message
5680 * body, and req->buf.p still points to the beginning of the
5681 * message. We forward the headers now, as we don't need them
5682 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005683 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005684 b_adv(req->buf, msg->sov);
5685 msg->next -= msg->sov;
5686 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005687
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005688 /* The previous analysers guarantee that the state is somewhere
5689 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5690 * msg->next are always correct.
5691 */
5692 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5693 if (msg->flags & HTTP_MSGF_TE_CHNK)
5694 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5695 else
5696 msg->msg_state = HTTP_MSG_DATA;
5697 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005698 }
5699
Willy Tarreau7ba23542014-04-17 21:50:00 +02005700 /* Some post-connect processing might want us to refrain from starting to
5701 * forward data. Currently, the only reason for this is "balance url_param"
5702 * whichs need to parse/process the request after we've enabled forwarding.
5703 */
5704 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005705 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005706 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005707 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005708 goto missing_data;
5709 }
5710 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5711 }
5712
Willy Tarreau80a92c02014-03-12 10:41:13 +01005713 /* in most states, we should abort in case of early close */
5714 channel_auto_close(req);
5715
Willy Tarreauefdf0942014-04-24 20:08:57 +02005716 if (req->to_forward) {
5717 /* We can't process the buffer's contents yet */
5718 req->flags |= CF_WAKE_WRITE;
5719 goto missing_data;
5720 }
5721
Willy Tarreaud98cf932009-12-27 22:54:55 +01005722 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005723 if (msg->msg_state == HTTP_MSG_DATA) {
5724 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005725 /* we may have some pending data starting at req->buf->p */
5726 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005727 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005728 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005729 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005730 msg->next += msg->chunk_len;
5731 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005732
5733 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005734 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005735 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005736 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005737 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005738 }
5739 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005740 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005741 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005742 * TRAILERS state.
5743 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005744 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005745
Willy Tarreau54d23df2012-10-25 19:04:45 +02005746 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005747 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005748 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005749 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005750 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005751 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005752 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005753 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005754 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005755 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005756 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005757 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005758 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005759
5760 if (ret == 0)
5761 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005762 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005763 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005764 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005765 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005766 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005767 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005768 /* we're in MSG_CHUNK_SIZE now */
5769 }
5770 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005771 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005772
5773 if (ret == 0)
5774 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005775 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005776 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005777 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005778 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005779 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005780 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005781 /* we're in HTTP_MSG_DONE now */
5782 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005783 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005784 int old_state = msg->msg_state;
5785
Willy Tarreau610ecce2010-01-04 21:15:02 +01005786 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005787
5788 /* we may have some pending data starting at req->buf->p
5789 * such as last chunk of data or trailers.
5790 */
5791 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005792 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005793 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005794 msg->next = 0;
5795
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005796 /* we don't want to forward closes on DONE except in
5797 * tunnel mode.
5798 */
5799 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005800 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005801 if (http_resync_states(s)) {
5802 /* some state changes occurred, maybe the analyser
5803 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005804 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005805 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005806 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005807 /* request errors are most likely due to
5808 * the server aborting the transfer.
5809 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005810 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005811 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005812 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005813 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005814 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005815 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005816 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005817 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005818
5819 /* If "option abortonclose" is set on the backend, we
5820 * want to monitor the client's connection and forward
5821 * any shutdown notification to the server, which will
5822 * decide whether to close or to go on processing the
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005823 * request. We only do that in tunnel mode, and not in
5824 * other modes since it can be abused to exhaust source
5825 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005826 */
5827 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005828 channel_auto_read(req);
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005829 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5830 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5831 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005832 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005833 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005834 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005835 /* POST requests may require to read extra CRLF
5836 * sent by broken browsers and which could cause
5837 * an RST to be sent upon close on some systems
5838 * (eg: Linux).
5839 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005840 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005841 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005842
Willy Tarreau610ecce2010-01-04 21:15:02 +01005843 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005844 }
5845 }
5846
Willy Tarreaud98cf932009-12-27 22:54:55 +01005847 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005848 /* we may have some pending data starting at req->buf->p */
5849 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005850 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005851 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5852
Willy Tarreaubed410e2014-04-22 08:19:34 +02005853 msg->next = 0;
5854 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5855
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005856 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005857 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005858 if (!(s->flags & SF_ERR_MASK))
5859 s->flags |= SF_ERR_CLICL;
5860 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005861 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005862 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005863 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005864 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005865 }
5866
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005867 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005868 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005869 if (objt_server(s->target))
5870 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005871
5872 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005873 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005874
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005875 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005876 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005877 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005878
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005879 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005880 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005881 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005882 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005883 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005884
Willy Tarreau5c620922011-05-11 19:56:11 +02005885 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005886 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005887 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005888 * modes are already handled by the stream sock layer. We must not do
5889 * this in content-length mode because it could present the MSG_MORE
5890 * flag with the last block of forwarded data, which would cause an
5891 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005892 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005893 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005894 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005895
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005896 return 0;
5897
Willy Tarreaud98cf932009-12-27 22:54:55 +01005898 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005899 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005900 if (sess->listener->counters)
5901 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005902
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005903 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005904 /* we may have some pending data starting at req->buf->p */
5905 b_adv(req->buf, msg->next);
5906 msg->next = 0;
5907
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005908 txn->req.msg_state = HTTP_MSG_ERROR;
5909 if (txn->status) {
5910 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005911 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005912 } else {
5913 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005914 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005915 }
5916 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005917 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005918
Willy Tarreaue7dff022015-04-03 01:14:29 +02005919 if (!(s->flags & SF_ERR_MASK))
5920 s->flags |= SF_ERR_PRXCOND;
5921 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005922 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005923 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005924 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005925 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005926 }
5927 return 0;
5928
5929 aborted_xfer:
5930 txn->req.msg_state = HTTP_MSG_ERROR;
5931 if (txn->status) {
5932 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005933 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005934 } else {
5935 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005936 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005937 }
5938 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005939 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005940
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005941 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005942 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005943 if (objt_server(s->target))
5944 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005945
Willy Tarreaue7dff022015-04-03 01:14:29 +02005946 if (!(s->flags & SF_ERR_MASK))
5947 s->flags |= SF_ERR_SRVCL;
5948 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005949 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005950 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005951 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005952 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005953 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005954 return 0;
5955}
5956
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005957/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5958 * processing can continue on next analysers, or zero if it either needs more
5959 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005960 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005961 * when it has nothing left to do, and may remove any analyser when it wants to
5962 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005963 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005964int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005965{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005966 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005967 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005968 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005969 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005970 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005971 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005972 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005973
Willy Tarreau87b09662015-04-03 00:22:06 +02005974 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 +02005975 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005976 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005977 rep,
5978 rep->rex, rep->wex,
5979 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005980 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005981 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005982
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005983 /*
5984 * Now parse the partial (or complete) lines.
5985 * We will check the response syntax, and also join multi-line
5986 * headers. An index of all the lines will be elaborated while
5987 * parsing.
5988 *
5989 * For the parsing, we use a 28 states FSM.
5990 *
5991 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005992 * rep->buf->p = beginning of response
5993 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5994 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005995 * msg->eol = end of current header or line (LF or CRLF)
5996 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005997 */
5998
Willy Tarreau628c40c2014-04-24 19:11:26 +02005999 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01006000 /* There's a protected area at the end of the buffer for rewriting
6001 * purposes. We don't want to start to parse the request if the
6002 * protected area is affected, because we may have to move processed
6003 * data later, which is much more complicated.
6004 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006005 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01006006 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02006007 /* some data has still not left the buffer, wake us once that's done */
6008 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
6009 goto abort_response;
6010 channel_dont_close(rep);
6011 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01006012 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02006013 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01006014 }
6015
Willy Tarreau379357a2013-06-08 12:55:46 +02006016 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
6017 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
6018 buffer_slow_realign(rep->buf);
6019
Willy Tarreau9b28e032012-10-12 23:49:43 +02006020 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01006021 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01006022 }
6023
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006024 /* 1: we might have to print this header in debug mode */
6025 if (unlikely((global.mode & MODE_DEBUG) &&
6026 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02006027 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006028 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006029
Willy Tarreau9b28e032012-10-12 23:49:43 +02006030 sol = rep->buf->p;
6031 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006032 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006033
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006034 sol += hdr_idx_first_pos(&txn->hdr_idx);
6035 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006036
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006037 while (cur_idx) {
6038 eol = sol + txn->hdr_idx.v[cur_idx].len;
6039 debug_hdr("srvhdr", s, sol, eol);
6040 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
6041 cur_idx = txn->hdr_idx.v[cur_idx].next;
6042 }
6043 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006044
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006045 /*
6046 * Now we quickly check if we have found a full valid response.
6047 * If not so, we check the FD and buffer states before leaving.
6048 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01006049 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006050 * responses are checked first.
6051 *
6052 * Depending on whether the client is still there or not, we
6053 * may send an error response back or not. Note that normally
6054 * we should only check for HTTP status there, and check I/O
6055 * errors somewhere else.
6056 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006057
Willy Tarreau655dce92009-11-08 13:10:58 +01006058 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006059 /* Invalid response */
6060 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6061 /* we detected a parsing error. We want to archive this response
6062 * in the dedicated proxy area for later troubleshooting.
6063 */
6064 hdr_response_bad:
6065 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006066 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006067
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006068 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006069 if (objt_server(s->target)) {
6070 objt_server(s->target)->counters.failed_resp++;
6071 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006072 }
Willy Tarreau64648412010-03-05 10:41:54 +01006073 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006074 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006075 rep->analysers = 0;
6076 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006077 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006078 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006079 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006080
Willy Tarreaue7dff022015-04-03 01:14:29 +02006081 if (!(s->flags & SF_ERR_MASK))
6082 s->flags |= SF_ERR_PRXCOND;
6083 if (!(s->flags & SF_FINST_MASK))
6084 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006085
6086 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006087 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006088
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006089 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006090 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006091 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006092 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006093 goto hdr_response_bad;
6094 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006095
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006096 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006097 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006098 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006099 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006100 else if (txn->flags & TX_NOT_FIRST)
6101 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006102
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006103 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006104 if (objt_server(s->target)) {
6105 objt_server(s->target)->counters.failed_resp++;
6106 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006107 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006108
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006109 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006110 rep->analysers = 0;
6111 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006112 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006113 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006114 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006115
Willy Tarreaue7dff022015-04-03 01:14:29 +02006116 if (!(s->flags & SF_ERR_MASK))
6117 s->flags |= SF_ERR_SRVCL;
6118 if (!(s->flags & SF_FINST_MASK))
6119 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006120 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006121 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006122
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006123 /* read timeout : return a 504 to the client. */
6124 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006125 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006126 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006127 else if (txn->flags & TX_NOT_FIRST)
6128 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006129
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006130 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006131 if (objt_server(s->target)) {
6132 objt_server(s->target)->counters.failed_resp++;
6133 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006134 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006135
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006136 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006137 rep->analysers = 0;
6138 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006139 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006140 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006141 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006142
Willy Tarreaue7dff022015-04-03 01:14:29 +02006143 if (!(s->flags & SF_ERR_MASK))
6144 s->flags |= SF_ERR_SRVTO;
6145 if (!(s->flags & SF_FINST_MASK))
6146 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006147 return 0;
6148 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006149
Willy Tarreauf003d372012-11-26 13:35:37 +01006150 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006151 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006152 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006153 s->be->be_counters.cli_aborts++;
6154 if (objt_server(s->target))
6155 objt_server(s->target)->counters.cli_aborts++;
6156
6157 rep->analysers = 0;
6158 channel_auto_close(rep);
6159
6160 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006161 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006162 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006163
Willy Tarreaue7dff022015-04-03 01:14:29 +02006164 if (!(s->flags & SF_ERR_MASK))
6165 s->flags |= SF_ERR_CLICL;
6166 if (!(s->flags & SF_FINST_MASK))
6167 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006168
Willy Tarreau87b09662015-04-03 00:22:06 +02006169 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006170 return 0;
6171 }
6172
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006173 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006174 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006175 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006176 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006177 else if (txn->flags & TX_NOT_FIRST)
6178 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006179
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006180 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006181 if (objt_server(s->target)) {
6182 objt_server(s->target)->counters.failed_resp++;
6183 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006184 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006185
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006186 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006187 rep->analysers = 0;
6188 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006189 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006190 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006191 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006192
Willy Tarreaue7dff022015-04-03 01:14:29 +02006193 if (!(s->flags & SF_ERR_MASK))
6194 s->flags |= SF_ERR_SRVCL;
6195 if (!(s->flags & SF_FINST_MASK))
6196 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006197 return 0;
6198 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006199
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006200 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006201 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006202 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006203 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006204 else if (txn->flags & TX_NOT_FIRST)
6205 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006206
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006207 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006208 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006209 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006210
Willy Tarreaue7dff022015-04-03 01:14:29 +02006211 if (!(s->flags & SF_ERR_MASK))
6212 s->flags |= SF_ERR_CLICL;
6213 if (!(s->flags & SF_FINST_MASK))
6214 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006215
Willy Tarreau87b09662015-04-03 00:22:06 +02006216 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006217 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006218 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006219
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006220 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006221 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006222 return 0;
6223 }
6224
6225 /* More interesting part now : we know that we have a complete
6226 * response which at least looks like HTTP. We have an indicator
6227 * of each header's length, so we can parse them quickly.
6228 */
6229
6230 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006231 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006232
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006233 /*
6234 * 1: get the status code
6235 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006236 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006237 if (n < 1 || n > 5)
6238 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006239 /* when the client triggers a 4xx from the server, it's most often due
6240 * to a missing object or permission. These events should be tracked
6241 * because if they happen often, it may indicate a brute force or a
6242 * vulnerability scan.
6243 */
6244 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006245 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006246
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006247 if (objt_server(s->target))
6248 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006249
Willy Tarreau91852eb2015-05-01 13:26:00 +02006250 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6251 * exactly one digit "." one digit. This check may be disabled using
6252 * option accept-invalid-http-response.
6253 */
6254 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6255 if (msg->sl.st.v_l != 8) {
6256 msg->err_pos = 0;
6257 goto hdr_response_bad;
6258 }
6259
6260 if (rep->buf->p[4] != '/' ||
6261 !isdigit((unsigned char)rep->buf->p[5]) ||
6262 rep->buf->p[6] != '.' ||
6263 !isdigit((unsigned char)rep->buf->p[7])) {
6264 msg->err_pos = 4;
6265 goto hdr_response_bad;
6266 }
6267 }
6268
Willy Tarreau5b154472009-12-21 20:11:07 +01006269 /* check if the response is HTTP/1.1 or above */
6270 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006271 ((rep->buf->p[5] > '1') ||
6272 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006273 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006274
6275 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006276 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 +01006277
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006278 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006279 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006280
Willy Tarreau9b28e032012-10-12 23:49:43 +02006281 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006282
Willy Tarreau39650402010-03-15 19:44:39 +01006283 /* Adjust server's health based on status code. Note: status codes 501
6284 * and 505 are triggered on demand by client request, so we must not
6285 * count them as server failures.
6286 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006287 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006288 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006289 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006290 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006291 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006292 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006293
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006294 /*
6295 * 2: check for cacheability.
6296 */
6297
6298 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006299 case 100:
6300 /*
6301 * We may be facing a 100-continue response, in which case this
6302 * is not the right response, and we're waiting for the next one.
6303 * Let's allow this response to go to the client and wait for the
6304 * next one.
6305 */
6306 hdr_idx_init(&txn->hdr_idx);
6307 msg->next -= channel_forward(rep, msg->next);
6308 msg->msg_state = HTTP_MSG_RPBEFORE;
6309 txn->status = 0;
6310 s->logs.t_data = -1; /* was not a response yet */
6311 goto next_one;
6312
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006313 case 200:
6314 case 203:
6315 case 206:
6316 case 300:
6317 case 301:
6318 case 410:
6319 /* RFC2616 @13.4:
6320 * "A response received with a status code of
6321 * 200, 203, 206, 300, 301 or 410 MAY be stored
6322 * by a cache (...) unless a cache-control
6323 * directive prohibits caching."
6324 *
6325 * RFC2616 @9.5: POST method :
6326 * "Responses to this method are not cacheable,
6327 * unless the response includes appropriate
6328 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006329 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006330 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006331 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006332 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6333 break;
6334 default:
6335 break;
6336 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006337
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006338 /*
6339 * 3: we may need to capture headers
6340 */
6341 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006342 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006343 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006344 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006345
Willy Tarreau557f1992015-05-01 10:05:17 +02006346 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6347 * by RFC7230#3.3.3 :
6348 *
6349 * The length of a message body is determined by one of the following
6350 * (in order of precedence):
6351 *
6352 * 1. Any response to a HEAD request and any response with a 1xx
6353 * (Informational), 204 (No Content), or 304 (Not Modified) status
6354 * code is always terminated by the first empty line after the
6355 * header fields, regardless of the header fields present in the
6356 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006357 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006358 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6359 * the connection will become a tunnel immediately after the empty
6360 * line that concludes the header fields. A client MUST ignore any
6361 * Content-Length or Transfer-Encoding header fields received in
6362 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006363 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006364 * 3. If a Transfer-Encoding header field is present and the chunked
6365 * transfer coding (Section 4.1) is the final encoding, the message
6366 * body length is determined by reading and decoding the chunked
6367 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006368 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006369 * If a Transfer-Encoding header field is present in a response and
6370 * the chunked transfer coding is not the final encoding, the
6371 * message body length is determined by reading the connection until
6372 * it is closed by the server. If a Transfer-Encoding header field
6373 * is present in a request and the chunked transfer coding is not
6374 * the final encoding, the message body length cannot be determined
6375 * reliably; the server MUST respond with the 400 (Bad Request)
6376 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006377 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006378 * If a message is received with both a Transfer-Encoding and a
6379 * Content-Length header field, the Transfer-Encoding overrides the
6380 * Content-Length. Such a message might indicate an attempt to
6381 * perform request smuggling (Section 9.5) or response splitting
6382 * (Section 9.4) and ought to be handled as an error. A sender MUST
6383 * remove the received Content-Length field prior to forwarding such
6384 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006385 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006386 * 4. If a message is received without Transfer-Encoding and with
6387 * either multiple Content-Length header fields having differing
6388 * field-values or a single Content-Length header field having an
6389 * invalid value, then the message framing is invalid and the
6390 * recipient MUST treat it as an unrecoverable error. If this is a
6391 * request message, the server MUST respond with a 400 (Bad Request)
6392 * status code and then close the connection. If this is a response
6393 * message received by a proxy, the proxy MUST close the connection
6394 * to the server, discard the received response, and send a 502 (Bad
6395 * Gateway) response to the client. If this is a response message
6396 * received by a user agent, the user agent MUST close the
6397 * connection to the server and discard the received response.
6398 *
6399 * 5. If a valid Content-Length header field is present without
6400 * Transfer-Encoding, its decimal value defines the expected message
6401 * body length in octets. If the sender closes the connection or
6402 * the recipient times out before the indicated number of octets are
6403 * received, the recipient MUST consider the message to be
6404 * incomplete and close the connection.
6405 *
6406 * 6. If this is a request message and none of the above are true, then
6407 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006408 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006409 * 7. Otherwise, this is a response message without a declared message
6410 * body length, so the message body length is determined by the
6411 * number of octets received prior to the server closing the
6412 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006413 */
6414
6415 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006416 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006417 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006418 * FIXME: should we parse anyway and return an error on chunked encoding ?
6419 */
6420 if (txn->meth == HTTP_METH_HEAD ||
6421 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006422 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006423 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006424 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006425 goto skip_content_length;
6426 }
6427
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006428 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006429 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006430 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006431 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006432 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6433 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006434 /* bad transfer-encoding (chunked followed by something else) */
6435 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006436 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006437 break;
6438 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006439 }
6440
Willy Tarreau1c913912015-04-30 10:57:51 +02006441 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006442 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006443 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006444 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6445 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6446 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006447 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006448 signed long long cl;
6449
Willy Tarreauad14f752011-09-02 20:33:27 +02006450 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006451 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006452 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006453 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006454
Willy Tarreauad14f752011-09-02 20:33:27 +02006455 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006456 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006457 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006458 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006459
Willy Tarreauad14f752011-09-02 20:33:27 +02006460 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006461 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006462 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006463 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006464
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006465 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006466 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006467 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006468 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006469
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006470 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006471 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006472 }
6473
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006474 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006475 select_compression_response_header(s, rep->buf);
6476
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006477skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006478 /* Now we have to check if we need to modify the Connection header.
6479 * This is more difficult on the response than it is on the request,
6480 * because we can have two different HTTP versions and we don't know
6481 * how the client will interprete a response. For instance, let's say
6482 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6483 * HTTP/1.1 response without any header. Maybe it will bound itself to
6484 * HTTP/1.0 because it only knows about it, and will consider the lack
6485 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6486 * the lack of header as a keep-alive. Thus we will use two flags
6487 * indicating how a request MAY be understood by the client. In case
6488 * of multiple possibilities, we'll fix the header to be explicit. If
6489 * ambiguous cases such as both close and keepalive are seen, then we
6490 * will fall back to explicit close. Note that we won't take risks with
6491 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006492 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006493 */
6494
Willy Tarreaudc008c52010-02-01 16:20:08 +01006495 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6496 txn->status == 101)) {
6497 /* Either we've established an explicit tunnel, or we're
6498 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006499 * to understand the next protocols. We have to switch to tunnel
6500 * mode, so that we transfer the request and responses then let
6501 * this protocol pass unmodified. When we later implement specific
6502 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006503 * header which contains information about that protocol for
6504 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006505 */
6506 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6507 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006508 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6509 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006510 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006511 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006512 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006513
Willy Tarreau70dffda2014-01-30 03:07:23 +01006514 /* this situation happens when combining pretend-keepalive with httpclose. */
6515 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006516 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006517 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006518 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6519
Willy Tarreau60466522010-01-18 19:08:45 +01006520 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006521 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006522 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6523 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006524
Willy Tarreau60466522010-01-18 19:08:45 +01006525 /* now adjust header transformations depending on current state */
6526 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6527 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6528 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006529 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006530 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006531 }
Willy Tarreau60466522010-01-18 19:08:45 +01006532 else { /* SCL / KAL */
6533 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006534 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006535 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006536 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006537
Willy Tarreau60466522010-01-18 19:08:45 +01006538 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006539 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006540
Willy Tarreau60466522010-01-18 19:08:45 +01006541 /* Some keep-alive responses are converted to Server-close if
6542 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006543 */
Willy Tarreau60466522010-01-18 19:08:45 +01006544 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6545 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006546 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006547 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006548 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006549 }
6550
Willy Tarreau7959a552013-09-23 16:44:27 +02006551 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006552 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006553
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006554 /* end of job, return OK */
6555 rep->analysers &= ~an_bit;
6556 rep->analyse_exp = TICK_ETERNITY;
6557 channel_auto_close(rep);
6558 return 1;
6559
6560 abort_keep_alive:
6561 /* A keep-alive request to the server failed on a network error.
6562 * The client is required to retry. We need to close without returning
6563 * any other information so that the client retries.
6564 */
6565 txn->status = 0;
6566 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006567 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006568 channel_auto_close(rep);
6569 s->logs.logwait = 0;
6570 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006571 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006572 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006573 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006574 return 0;
6575}
6576
6577/* This function performs all the processing enabled for the current response.
6578 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006579 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006580 * other functions. It works like process_request (see indications above).
6581 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006582int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006583{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006584 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006585 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006586 struct http_msg *msg = &txn->rsp;
6587 struct proxy *cur_proxy;
6588 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006589 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006590
Willy Tarreau87b09662015-04-03 00:22:06 +02006591 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 +02006592 now_ms, __FUNCTION__,
6593 s,
6594 rep,
6595 rep->rex, rep->wex,
6596 rep->flags,
6597 rep->buf->i,
6598 rep->analysers);
6599
6600 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6601 return 0;
6602
Willy Tarreau70730dd2014-04-24 18:06:27 +02006603 /* The stats applet needs to adjust the Connection header but we don't
6604 * apply any filter there.
6605 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006606 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6607 rep->analysers &= ~an_bit;
6608 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006609 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006610 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006611
Willy Tarreau58975672014-04-24 21:13:57 +02006612 /*
6613 * We will have to evaluate the filters.
6614 * As opposed to version 1.2, now they will be evaluated in the
6615 * filters order and not in the header order. This means that
6616 * each filter has to be validated among all headers.
6617 *
6618 * Filters are tried with ->be first, then with ->fe if it is
6619 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006620 *
6621 * Maybe we are in resume condiion. In this case I choose the
6622 * "struct proxy" which contains the rule list matching the resume
6623 * pointer. If none of theses "struct proxy" match, I initialise
6624 * the process with the first one.
6625 *
6626 * In fact, I check only correspondance betwwen the current list
6627 * pointer and the ->fe rule list. If it doesn't match, I initialize
6628 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006629 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006630 if (s->current_rule_list == &sess->fe->http_res_rules)
6631 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006632 else
6633 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006634 while (1) {
6635 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006636
Willy Tarreau58975672014-04-24 21:13:57 +02006637 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006638 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006639 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006640
Willy Tarreau51d861a2015-05-22 17:30:48 +02006641 if (ret == HTTP_RULE_RES_BADREQ)
6642 goto return_srv_prx_502;
6643
6644 if (ret == HTTP_RULE_RES_DONE) {
6645 rep->analysers &= ~an_bit;
6646 rep->analyse_exp = TICK_ETERNITY;
6647 return 1;
6648 }
6649 }
6650
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006651 /* we need to be called again. */
6652 if (ret == HTTP_RULE_RES_YIELD) {
6653 channel_dont_close(rep);
6654 return 0;
6655 }
6656
Willy Tarreau58975672014-04-24 21:13:57 +02006657 /* try headers filters */
6658 if (rule_set->rsp_exp != NULL) {
6659 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6660 return_bad_resp:
6661 if (objt_server(s->target)) {
6662 objt_server(s->target)->counters.failed_resp++;
6663 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006664 }
Willy Tarreau58975672014-04-24 21:13:57 +02006665 s->be->be_counters.failed_resp++;
6666 return_srv_prx_502:
6667 rep->analysers = 0;
6668 txn->status = 502;
6669 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006670 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006671 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006672 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006673 if (!(s->flags & SF_ERR_MASK))
6674 s->flags |= SF_ERR_PRXCOND;
6675 if (!(s->flags & SF_FINST_MASK))
6676 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006677 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006678 }
Willy Tarreau58975672014-04-24 21:13:57 +02006679 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006680
Willy Tarreau58975672014-04-24 21:13:57 +02006681 /* has the response been denied ? */
6682 if (txn->flags & TX_SVDENY) {
6683 if (objt_server(s->target))
6684 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006685
Willy Tarreau58975672014-04-24 21:13:57 +02006686 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006687 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006688 if (sess->listener->counters)
6689 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006690
Willy Tarreau58975672014-04-24 21:13:57 +02006691 goto return_srv_prx_502;
6692 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006693
Willy Tarreau58975672014-04-24 21:13:57 +02006694 /* add response headers from the rule sets in the same order */
6695 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006696 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006697 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006698 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006699 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006700 ret = acl_pass(ret);
6701 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6702 ret = !ret;
6703 if (!ret)
6704 continue;
6705 }
6706 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6707 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006708 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006709
Willy Tarreau58975672014-04-24 21:13:57 +02006710 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006711 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006712 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006713 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006714 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006715
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006716 /* After this point, this anayzer can't return yield, so we can
6717 * remove the bit corresponding to this analyzer from the list.
6718 *
6719 * Note that the intermediate returns and goto found previously
6720 * reset the analyzers.
6721 */
6722 rep->analysers &= ~an_bit;
6723 rep->analyse_exp = TICK_ETERNITY;
6724
Willy Tarreau58975672014-04-24 21:13:57 +02006725 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006726 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006727 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006728
Willy Tarreau58975672014-04-24 21:13:57 +02006729 /*
6730 * Now check for a server cookie.
6731 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006732 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006733 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006734
Willy Tarreau58975672014-04-24 21:13:57 +02006735 /*
6736 * Check for cache-control or pragma headers if required.
6737 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006738 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 +02006739 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006740
Willy Tarreau58975672014-04-24 21:13:57 +02006741 /*
6742 * Add server cookie in the response if needed
6743 */
6744 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6745 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006746 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006747 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6748 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6749 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6750 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6751 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006752 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006753 /* the server is known, it's not the one the client requested, or the
6754 * cookie's last seen date needs to be refreshed. We have to
6755 * insert a set-cookie here, except if we want to insert only on POST
6756 * requests and this one isn't. Note that servers which don't have cookies
6757 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006758 */
Willy Tarreau58975672014-04-24 21:13:57 +02006759 if (!objt_server(s->target)->cookie) {
6760 chunk_printf(&trash,
6761 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6762 s->be->cookie_name);
6763 }
6764 else {
6765 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006766
Willy Tarreau58975672014-04-24 21:13:57 +02006767 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6768 /* emit last_date, which is mandatory */
6769 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6770 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6771 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006772
Willy Tarreau58975672014-04-24 21:13:57 +02006773 if (s->be->cookie_maxlife) {
6774 /* emit first_date, which is either the original one or
6775 * the current date.
6776 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006777 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006778 s30tob64(txn->cookie_first_date ?
6779 txn->cookie_first_date >> 2 :
6780 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006781 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006782 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006783 }
Willy Tarreau58975672014-04-24 21:13:57 +02006784 chunk_appendf(&trash, "; path=/");
6785 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006786
Willy Tarreau58975672014-04-24 21:13:57 +02006787 if (s->be->cookie_domain)
6788 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006789
Willy Tarreau58975672014-04-24 21:13:57 +02006790 if (s->be->ck_opts & PR_CK_HTTPONLY)
6791 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006792
Willy Tarreau58975672014-04-24 21:13:57 +02006793 if (s->be->ck_opts & PR_CK_SECURE)
6794 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006795
Willy Tarreau58975672014-04-24 21:13:57 +02006796 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6797 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006798
Willy Tarreau58975672014-04-24 21:13:57 +02006799 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006800 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006801 /* the server did not change, only the date was updated */
6802 txn->flags |= TX_SCK_UPDATED;
6803 else
6804 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006805
Willy Tarreau58975672014-04-24 21:13:57 +02006806 /* Here, we will tell an eventual cache on the client side that we don't
6807 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6808 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6809 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006810 */
Willy Tarreau58975672014-04-24 21:13:57 +02006811 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006812
Willy Tarreau58975672014-04-24 21:13:57 +02006813 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006814
Willy Tarreau58975672014-04-24 21:13:57 +02006815 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6816 "Cache-control: private", 22) < 0))
6817 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006818 }
Willy Tarreau58975672014-04-24 21:13:57 +02006819 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006820
Willy Tarreau58975672014-04-24 21:13:57 +02006821 /*
6822 * Check if result will be cacheable with a cookie.
6823 * We'll block the response if security checks have caught
6824 * nasty things such as a cacheable cookie.
6825 */
6826 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6827 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6828 (s->be->options & PR_O_CHK_CACHE)) {
6829 /* we're in presence of a cacheable response containing
6830 * a set-cookie header. We'll block it as requested by
6831 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006832 */
Willy Tarreau58975672014-04-24 21:13:57 +02006833 if (objt_server(s->target))
6834 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006835
Willy Tarreau58975672014-04-24 21:13:57 +02006836 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006837 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006838 if (sess->listener->counters)
6839 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006840
Willy Tarreau58975672014-04-24 21:13:57 +02006841 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6842 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6843 send_log(s->be, LOG_ALERT,
6844 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6845 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6846 goto return_srv_prx_502;
6847 }
Willy Tarreau03945942009-12-22 16:50:27 +01006848
Willy Tarreau70730dd2014-04-24 18:06:27 +02006849 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006850 /*
6851 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6852 * If an "Upgrade" token is found, the header is left untouched in order
6853 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006854 * if anything but "Upgrade" is present in the Connection header. We don't
6855 * want to touch any 101 response either since it's switching to another
6856 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006857 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006858 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006859 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006860 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006861 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6862 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006863
Willy Tarreau58975672014-04-24 21:13:57 +02006864 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6865 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6866 /* we want a keep-alive response here. Keep-alive header
6867 * required if either side is not 1.1.
6868 */
6869 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6870 want_flags |= TX_CON_KAL_SET;
6871 }
6872 else {
6873 /* we want a close response here. Close header required if
6874 * the server is 1.1, regardless of the client.
6875 */
6876 if (msg->flags & HTTP_MSGF_VER_11)
6877 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006878 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006879
Willy Tarreau58975672014-04-24 21:13:57 +02006880 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6881 http_change_connection_header(txn, msg, want_flags);
6882 }
6883
6884 skip_header_mangling:
6885 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6886 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6887 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006888
Willy Tarreau58975672014-04-24 21:13:57 +02006889 /* if the user wants to log as soon as possible, without counting
6890 * bytes from the server, then this is the right moment. We have
6891 * to temporarily assign bytes_out to log what we currently have.
6892 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006893 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006894 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6895 s->logs.bytes_out = txn->rsp.eoh;
6896 s->do_log(s);
6897 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006898 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006899 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006900}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006901
Willy Tarreaud98cf932009-12-27 22:54:55 +01006902/* This function is an analyser which forwards response body (including chunk
6903 * sizes if any). It is called as soon as we must forward, even if we forward
6904 * zero byte. The only situation where it must not be called is when we're in
6905 * tunnel mode and we want to forward till the close. It's used both to forward
6906 * remaining data and to resync after end of body. It expects the msg_state to
6907 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006908 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006909 *
6910 * It is capable of compressing response data both in content-length mode and
6911 * in chunked mode. The state machines follows different flows depending on
6912 * whether content-length and chunked modes are used, since there are no
6913 * trailers in content-length :
6914 *
6915 * chk-mode cl-mode
6916 * ,----- BODY -----.
6917 * / \
6918 * V size > 0 V chk-mode
6919 * .--> SIZE -------------> DATA -------------> CRLF
6920 * | | size == 0 | last byte |
6921 * | v final crlf v inspected |
6922 * | TRAILERS -----------> DONE |
6923 * | |
6924 * `----------------------------------------------'
6925 *
6926 * Compression only happens in the DATA state, and must be flushed in final
6927 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6928 * is performed at once on final states for all bytes parsed, or when leaving
6929 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006930 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006931int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006932{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006933 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006934 struct http_txn *txn = s->txn;
6935 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006936 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006937 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006938 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006939
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006940 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6941 return 0;
6942
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006943 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006944 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006945 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006946 /* Output closed while we were sending data. We must abort and
6947 * wake the other side up.
6948 */
6949 msg->msg_state = HTTP_MSG_ERROR;
6950 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006951 return 1;
6952 }
6953
Willy Tarreau4fe41902010-06-07 22:27:41 +02006954 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006955 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006956
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006957 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006958 /* we have msg->sov which points to the first byte of message
6959 * body, and res->buf.p still points to the beginning of the
6960 * message. We forward the headers now, as we don't need them
6961 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006962 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006963 b_adv(res->buf, msg->sov);
6964 msg->next -= msg->sov;
6965 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006966
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006967 /* The previous analysers guarantee that the state is somewhere
6968 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6969 * msg->next are always correct.
6970 */
6971 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6972 if (msg->flags & HTTP_MSGF_TE_CHNK)
6973 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6974 else
6975 msg->msg_state = HTTP_MSG_DATA;
6976 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006977 }
6978
Willy Tarreauefdf0942014-04-24 20:08:57 +02006979 if (res->to_forward) {
6980 /* We can't process the buffer's contents yet */
6981 res->flags |= CF_WAKE_WRITE;
6982 goto missing_data;
6983 }
6984
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006985 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6986 /* We need a compression buffer in the DATA state to put the
6987 * output of compressed data, and in CRLF state to let the
6988 * TRAILERS state finish the job of removing the trailing CRLF.
6989 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006990 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006991 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006992 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006993 goto aborted_xfer; /* no memory */
6994 }
6995
6996 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006997 if (ret < 0) {
6998 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006999 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01007000 }
William Lallemand82fe75c2012-10-23 10:25:10 +02007001 compressing = 1;
7002 }
7003
Willy Tarreaud98cf932009-12-27 22:54:55 +01007004 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007005 switch (msg->msg_state - HTTP_MSG_DATA) {
7006 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02007007 /* we may have some pending data starting at res->buf->p */
7008 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02007009 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01007010 if (ret < 0)
7011 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02007012
Willy Tarreaud5a67832014-04-21 10:54:27 +02007013 if (msg->chunk_len) {
7014 /* input empty or output full */
7015 if (res->buf->i > msg->next)
7016 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02007017 goto missing_data;
7018 }
William Lallemandbf3ae612012-11-19 12:35:37 +01007019 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007020 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02007021 if (msg->chunk_len > res->buf->i - msg->next) {
7022 /* output full */
7023 res->flags |= CF_WAKE_WRITE;
7024 goto missing_data;
7025 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007026 msg->next += msg->chunk_len;
7027 msg->chunk_len = 0;
7028 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01007029
7030 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01007031 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02007032 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01007033 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01007034 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007035 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01007036 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007037 /* fall through for HTTP_MSG_CHUNK_CRLF */
7038
7039 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
7040 /* we want the CRLF after the data */
7041
7042 ret = http_skip_chunk_crlf(msg);
7043 if (ret == 0)
7044 goto missing_data;
7045 else if (ret < 0) {
7046 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007047 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007048 goto return_bad_res;
7049 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007050 /* we're in MSG_CHUNK_SIZE now, fall through */
7051
7052 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01007053 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02007054 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01007055 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01007056 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007057
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007058 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02007059 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01007060 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007061 else if (ret < 0) {
7062 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007063 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007064 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007065 }
Willy Tarreau0161d622013-04-02 01:26:55 +02007066 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007067 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01007068
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007069 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02007070 if (unlikely(compressing)) {
7071 /* we need to flush output contents before syncing FSMs */
7072 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7073 compressing = 0;
7074 }
7075
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007076 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007077 if (ret == 0)
7078 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007079 else if (ret < 0) {
7080 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007081 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007082 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007083 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02007084 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007085
7086 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01007087 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007088 if (unlikely(compressing)) {
7089 /* we need to flush output contents before syncing FSMs */
7090 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7091 compressing = 0;
7092 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007093
Willy Tarreauc623c172014-04-18 09:53:50 +02007094 /* we may have some pending data starting at res->buf->p
7095 * such as a last chunk of data or trailers.
7096 */
7097 b_adv(res->buf, msg->next);
7098 msg->next = 0;
7099
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007100 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02007101 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007102 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7103 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007104 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02007105
Willy Tarreau610ecce2010-01-04 21:15:02 +01007106 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01007107 /* some state changes occurred, maybe the analyser
7108 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01007109 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007110 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007111 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007112 /* response errors are most likely due to
7113 * the client aborting the transfer.
7114 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007115 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007116 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007117 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007118 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01007119 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007120 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007121 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01007122 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007123 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007124 }
7125 }
7126
Willy Tarreaud98cf932009-12-27 22:54:55 +01007127 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02007128 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007129 if (unlikely(compressing)) {
7130 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02007131 compressing = 0;
7132 }
Willy Tarreauf003d372012-11-26 13:35:37 +01007133
Willy Tarreauc623c172014-04-18 09:53:50 +02007134 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
7135 b_adv(res->buf, msg->next);
7136 msg->next = 0;
7137 msg->chunk_len -= channel_forward(res, msg->chunk_len);
7138 }
7139
Willy Tarreauf003d372012-11-26 13:35:37 +01007140 if (res->flags & CF_SHUTW)
7141 goto aborted_xfer;
7142
7143 /* stop waiting for data if the input is closed before the end. If the
7144 * client side was already closed, it means that the client has aborted,
7145 * so we don't want to count this as a server abort. Otherwise it's a
7146 * server abort.
7147 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007148 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007149 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01007150 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007151 if (!(s->flags & SF_ERR_MASK))
7152 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007153 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007154 if (objt_server(s->target))
7155 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007156 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01007157 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01007158
Willy Tarreau40dba092010-03-04 18:14:51 +01007159 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007160 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01007161 goto return_bad_res;
7162
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007163 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007164 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007165 * Similarly, with keep-alive on the client side, we don't want to forward a
7166 * close.
7167 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007168 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007169 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7170 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007171 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007172
Willy Tarreau5c620922011-05-11 19:56:11 +02007173 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007174 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007175 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007176 * modes are already handled by the stream sock layer. We must not do
7177 * this in content-length mode because it could present the MSG_MORE
7178 * flag with the last block of forwarded data, which would cause an
7179 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007180 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007181 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007182 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007183
Willy Tarreau87b09662015-04-03 00:22:06 +02007184 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007185 return 0;
7186
Willy Tarreau40dba092010-03-04 18:14:51 +01007187 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007188 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007189 if (objt_server(s->target))
7190 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007191
7192 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02007193 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02007194 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02007195 compressing = 0;
7196 }
7197
Willy Tarreauc623c172014-04-18 09:53:50 +02007198 /* we may have some pending data starting at res->buf->p */
7199 if (s->comp_algo == NULL) {
7200 b_adv(res->buf, msg->next);
7201 msg->next = 0;
7202 }
7203
Willy Tarreaud98cf932009-12-27 22:54:55 +01007204 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007205 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007206 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007207 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007208 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007209 if (objt_server(s->target))
7210 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007211
Willy Tarreaue7dff022015-04-03 01:14:29 +02007212 if (!(s->flags & SF_ERR_MASK))
7213 s->flags |= SF_ERR_PRXCOND;
7214 if (!(s->flags & SF_FINST_MASK))
7215 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007216 return 0;
7217
7218 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02007219 if (unlikely(compressing)) {
7220 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
7221 compressing = 0;
7222 }
7223
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007224 txn->rsp.msg_state = HTTP_MSG_ERROR;
7225 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007226 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007227 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007228 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007229
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007230 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007231 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007232 if (objt_server(s->target))
7233 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007234
Willy Tarreaue7dff022015-04-03 01:14:29 +02007235 if (!(s->flags & SF_ERR_MASK))
7236 s->flags |= SF_ERR_CLICL;
7237 if (!(s->flags & SF_FINST_MASK))
7238 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007239 return 0;
7240}
7241
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007242/* Iterate the same filter through all request headers.
7243 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007244 * Since it can manage the switch to another backend, it updates the per-proxy
7245 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007246 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007247int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007248{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007249 char *cur_ptr, *cur_end, *cur_next;
7250 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007251 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007252 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007253 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007254
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007255 last_hdr = 0;
7256
Willy Tarreau9b28e032012-10-12 23:49:43 +02007257 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007258 old_idx = 0;
7259
7260 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007261 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007262 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007263 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007264 (exp->action == ACT_ALLOW ||
7265 exp->action == ACT_DENY ||
7266 exp->action == ACT_TARPIT))
7267 return 0;
7268
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007269 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007270 if (!cur_idx)
7271 break;
7272
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007273 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007274 cur_ptr = cur_next;
7275 cur_end = cur_ptr + cur_hdr->len;
7276 cur_next = cur_end + cur_hdr->cr + 1;
7277
7278 /* Now we have one header between cur_ptr and cur_end,
7279 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007280 */
7281
Willy Tarreau15a53a42015-01-21 13:39:42 +01007282 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007283 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007284 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007285 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007286 last_hdr = 1;
7287 break;
7288
7289 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007290 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007291 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007292 break;
7293
7294 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007295 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007296 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007297 break;
7298
7299 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007300 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7301 if (trash.len < 0)
7302 return -1;
7303
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007304 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007305 /* FIXME: if the user adds a newline in the replacement, the
7306 * index will not be recalculated for now, and the new line
7307 * will not be counted as a new header.
7308 */
7309
7310 cur_end += delta;
7311 cur_next += delta;
7312 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007313 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007314 break;
7315
7316 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007317 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007318 cur_next += delta;
7319
Willy Tarreaufa355d42009-11-29 18:12:29 +01007320 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007321 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7322 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007323 cur_hdr->len = 0;
7324 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007325 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007326 break;
7327
7328 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007329 }
7330
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007331 /* keep the link from this header to next one in case of later
7332 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007333 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007334 old_idx = cur_idx;
7335 }
7336 return 0;
7337}
7338
7339
7340/* Apply the filter to the request line.
7341 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7342 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007343 * Since it can manage the switch to another backend, it updates the per-proxy
7344 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007345 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007346int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007347{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007348 char *cur_ptr, *cur_end;
7349 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007350 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007351 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007352
Willy Tarreau3d300592007-03-18 18:34:41 +01007353 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007354 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007355 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007356 (exp->action == ACT_ALLOW ||
7357 exp->action == ACT_DENY ||
7358 exp->action == ACT_TARPIT))
7359 return 0;
7360 else if (exp->action == ACT_REMOVE)
7361 return 0;
7362
7363 done = 0;
7364
Willy Tarreau9b28e032012-10-12 23:49:43 +02007365 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007366 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007367
7368 /* Now we have the request line between cur_ptr and cur_end */
7369
Willy Tarreau15a53a42015-01-21 13:39:42 +01007370 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007371 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007372 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007373 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007374 done = 1;
7375 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007376
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007377 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007378 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007379 done = 1;
7380 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007381
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007382 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007383 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007384 done = 1;
7385 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007387 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007388 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7389 if (trash.len < 0)
7390 return -1;
7391
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007392 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007393 /* FIXME: if the user adds a newline in the replacement, the
7394 * index will not be recalculated for now, and the new line
7395 * will not be counted as a new header.
7396 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007397
Willy Tarreaufa355d42009-11-29 18:12:29 +01007398 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007399 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007400 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007401 HTTP_MSG_RQMETH,
7402 cur_ptr, cur_end + 1,
7403 NULL, NULL);
7404 if (unlikely(!cur_end))
7405 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007406
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007407 /* we have a full request and we know that we have either a CR
7408 * or an LF at <ptr>.
7409 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007410 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7411 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007412 /* there is no point trying this regex on headers */
7413 return 1;
7414 }
7415 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007416 return done;
7417}
Willy Tarreau97de6242006-12-27 17:18:38 +01007418
Willy Tarreau58f10d72006-12-04 02:26:12 +01007419
Willy Tarreau58f10d72006-12-04 02:26:12 +01007420
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007421/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007422 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007423 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007424 * unparsable request. Since it can manage the switch to another backend, it
7425 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007426 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007427int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007428{
Willy Tarreau192252e2015-04-04 01:47:55 +02007429 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007430 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007431 struct hdr_exp *exp;
7432
7433 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007434 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007435
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007436 /*
7437 * The interleaving of transformations and verdicts
7438 * makes it difficult to decide to continue or stop
7439 * the evaluation.
7440 */
7441
Willy Tarreau6c123b12010-01-28 20:22:06 +01007442 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7443 break;
7444
Willy Tarreau3d300592007-03-18 18:34:41 +01007445 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007446 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007447 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007448 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007449
7450 /* if this filter had a condition, evaluate it now and skip to
7451 * next filter if the condition does not match.
7452 */
7453 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007454 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007455 ret = acl_pass(ret);
7456 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7457 ret = !ret;
7458
7459 if (!ret)
7460 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007461 }
7462
7463 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007464 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007465 if (unlikely(ret < 0))
7466 return -1;
7467
7468 if (likely(ret == 0)) {
7469 /* The filter did not match the request, it can be
7470 * iterated through all headers.
7471 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007472 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7473 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007474 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007475 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007476 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007477}
7478
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007479
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007480/* Find the end of a cookie value contained between <s> and <e>. It works the
7481 * same way as with headers above except that the semi-colon also ends a token.
7482 * See RFC2965 for more information. Note that it requires a valid header to
7483 * return a valid result.
7484 */
7485char *find_cookie_value_end(char *s, const char *e)
7486{
7487 int quoted, qdpair;
7488
7489 quoted = qdpair = 0;
7490 for (; s < e; s++) {
7491 if (qdpair) qdpair = 0;
7492 else if (quoted) {
7493 if (*s == '\\') qdpair = 1;
7494 else if (*s == '"') quoted = 0;
7495 }
7496 else if (*s == '"') quoted = 1;
7497 else if (*s == ',' || *s == ';') return s;
7498 }
7499 return s;
7500}
7501
7502/* Delete a value in a header between delimiters <from> and <next> in buffer
7503 * <buf>. The number of characters displaced is returned, and the pointer to
7504 * the first delimiter is updated if required. The function tries as much as
7505 * possible to respect the following principles :
7506 * - replace <from> delimiter by the <next> one unless <from> points to a
7507 * colon, in which case <next> is simply removed
7508 * - set exactly one space character after the new first delimiter, unless
7509 * there are not enough characters in the block being moved to do so.
7510 * - remove unneeded spaces before the previous delimiter and after the new
7511 * one.
7512 *
7513 * It is the caller's responsibility to ensure that :
7514 * - <from> points to a valid delimiter or the colon ;
7515 * - <next> points to a valid delimiter or the final CR/LF ;
7516 * - there are non-space chars before <from> ;
7517 * - there is a CR/LF at or after <next>.
7518 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007519int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007520{
7521 char *prev = *from;
7522
7523 if (*prev == ':') {
7524 /* We're removing the first value, preserve the colon and add a
7525 * space if possible.
7526 */
7527 if (!http_is_crlf[(unsigned char)*next])
7528 next++;
7529 prev++;
7530 if (prev < next)
7531 *prev++ = ' ';
7532
7533 while (http_is_spht[(unsigned char)*next])
7534 next++;
7535 } else {
7536 /* Remove useless spaces before the old delimiter. */
7537 while (http_is_spht[(unsigned char)*(prev-1)])
7538 prev--;
7539 *from = prev;
7540
7541 /* copy the delimiter and if possible a space if we're
7542 * not at the end of the line.
7543 */
7544 if (!http_is_crlf[(unsigned char)*next]) {
7545 *prev++ = *next++;
7546 if (prev + 1 < next)
7547 *prev++ = ' ';
7548 while (http_is_spht[(unsigned char)*next])
7549 next++;
7550 }
7551 }
7552 return buffer_replace2(buf, prev, next, NULL, 0);
7553}
7554
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007555/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007556 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007557 * desirable to call it only when needed. This code is quite complex because
7558 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7559 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007560 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007561void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007562{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007563 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007564 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007565 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007566 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007567 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7568 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007569
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007570 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007571 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007572 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007573
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007574 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007575 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007576 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007577
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007578 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007579 hdr_beg = hdr_next;
7580 hdr_end = hdr_beg + cur_hdr->len;
7581 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007582
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007583 /* We have one full header between hdr_beg and hdr_end, and the
7584 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007585 * "Cookie:" headers.
7586 */
7587
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007588 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007589 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007590 old_idx = cur_idx;
7591 continue;
7592 }
7593
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007594 del_from = NULL; /* nothing to be deleted */
7595 preserve_hdr = 0; /* assume we may kill the whole header */
7596
Willy Tarreau58f10d72006-12-04 02:26:12 +01007597 /* Now look for cookies. Conforming to RFC2109, we have to support
7598 * attributes whose name begin with a '$', and associate them with
7599 * the right cookie, if we want to delete this cookie.
7600 * So there are 3 cases for each cookie read :
7601 * 1) it's a special attribute, beginning with a '$' : ignore it.
7602 * 2) it's a server id cookie that we *MAY* want to delete : save
7603 * some pointers on it (last semi-colon, beginning of cookie...)
7604 * 3) it's an application cookie : we *MAY* have to delete a previous
7605 * "special" cookie.
7606 * At the end of loop, if a "special" cookie remains, we may have to
7607 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007608 * *MUST* delete it.
7609 *
7610 * Note: RFC2965 is unclear about the processing of spaces around
7611 * the equal sign in the ATTR=VALUE form. A careful inspection of
7612 * the RFC explicitly allows spaces before it, and not within the
7613 * tokens (attrs or values). An inspection of RFC2109 allows that
7614 * too but section 10.1.3 lets one think that spaces may be allowed
7615 * after the equal sign too, resulting in some (rare) buggy
7616 * implementations trying to do that. So let's do what servers do.
7617 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7618 * allowed quoted strings in values, with any possible character
7619 * after a backslash, including control chars and delimitors, which
7620 * causes parsing to become ambiguous. Browsers also allow spaces
7621 * within values even without quotes.
7622 *
7623 * We have to keep multiple pointers in order to support cookie
7624 * removal at the beginning, middle or end of header without
7625 * corrupting the header. All of these headers are valid :
7626 *
7627 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7628 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7629 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7630 * | | | | | | | | |
7631 * | | | | | | | | hdr_end <--+
7632 * | | | | | | | +--> next
7633 * | | | | | | +----> val_end
7634 * | | | | | +-----------> val_beg
7635 * | | | | +--------------> equal
7636 * | | | +----------------> att_end
7637 * | | +---------------------> att_beg
7638 * | +--------------------------> prev
7639 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007640 */
7641
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007642 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7643 /* Iterate through all cookies on this line */
7644
7645 /* find att_beg */
7646 att_beg = prev + 1;
7647 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7648 att_beg++;
7649
7650 /* find att_end : this is the first character after the last non
7651 * space before the equal. It may be equal to hdr_end.
7652 */
7653 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007654
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007655 while (equal < hdr_end) {
7656 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007657 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007658 if (http_is_spht[(unsigned char)*equal++])
7659 continue;
7660 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007661 }
7662
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007663 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7664 * is between <att_beg> and <equal>, both may be identical.
7665 */
7666
7667 /* look for end of cookie if there is an equal sign */
7668 if (equal < hdr_end && *equal == '=') {
7669 /* look for the beginning of the value */
7670 val_beg = equal + 1;
7671 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7672 val_beg++;
7673
7674 /* find the end of the value, respecting quotes */
7675 next = find_cookie_value_end(val_beg, hdr_end);
7676
7677 /* make val_end point to the first white space or delimitor after the value */
7678 val_end = next;
7679 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7680 val_end--;
7681 } else {
7682 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007683 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007684
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007685 /* We have nothing to do with attributes beginning with '$'. However,
7686 * they will automatically be removed if a header before them is removed,
7687 * since they're supposed to be linked together.
7688 */
7689 if (*att_beg == '$')
7690 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007691
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007692 /* Ignore cookies with no equal sign */
7693 if (equal == next) {
7694 /* This is not our cookie, so we must preserve it. But if we already
7695 * scheduled another cookie for removal, we cannot remove the
7696 * complete header, but we can remove the previous block itself.
7697 */
7698 preserve_hdr = 1;
7699 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007700 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007701 val_end += delta;
7702 next += delta;
7703 hdr_end += delta;
7704 hdr_next += delta;
7705 cur_hdr->len += delta;
7706 http_msg_move_end(&txn->req, delta);
7707 prev = del_from;
7708 del_from = NULL;
7709 }
7710 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007711 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007712
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007713 /* if there are spaces around the equal sign, we need to
7714 * strip them otherwise we'll get trouble for cookie captures,
7715 * or even for rewrites. Since this happens extremely rarely,
7716 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007717 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007718 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7719 int stripped_before = 0;
7720 int stripped_after = 0;
7721
7722 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007723 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007724 equal += stripped_before;
7725 val_beg += stripped_before;
7726 }
7727
7728 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007729 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007730 val_beg += stripped_after;
7731 stripped_before += stripped_after;
7732 }
7733
7734 val_end += stripped_before;
7735 next += stripped_before;
7736 hdr_end += stripped_before;
7737 hdr_next += stripped_before;
7738 cur_hdr->len += stripped_before;
7739 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007740 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007741 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007742
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007743 /* First, let's see if we want to capture this cookie. We check
7744 * that we don't already have a client side cookie, because we
7745 * can only capture one. Also as an optimisation, we ignore
7746 * cookies shorter than the declared name.
7747 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007748 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7749 (val_end - att_beg >= sess->fe->capture_namelen) &&
7750 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007751 int log_len = val_end - att_beg;
7752
7753 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7754 Alert("HTTP logging : out of memory.\n");
7755 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007756 if (log_len > sess->fe->capture_len)
7757 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007758 memcpy(txn->cli_cookie, att_beg, log_len);
7759 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007760 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007761 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007762
Willy Tarreaubca99692010-10-06 19:25:55 +02007763 /* Persistence cookies in passive, rewrite or insert mode have the
7764 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007765 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007766 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007767 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007768 * For cookies in prefix mode, the form is :
7769 *
7770 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007771 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007772 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7773 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7774 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007775 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007776
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007777 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7778 * have the server ID between val_beg and delim, and the original cookie between
7779 * delim+1 and val_end. Otherwise, delim==val_end :
7780 *
7781 * Cookie: NAME=SRV; # in all but prefix modes
7782 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7783 * | || || | |+-> next
7784 * | || || | +--> val_end
7785 * | || || +---------> delim
7786 * | || |+------------> val_beg
7787 * | || +-------------> att_end = equal
7788 * | |+-----------------> att_beg
7789 * | +------------------> prev
7790 * +-------------------------> hdr_beg
7791 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007792
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007793 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007794 for (delim = val_beg; delim < val_end; delim++)
7795 if (*delim == COOKIE_DELIM)
7796 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007797 } else {
7798 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007799 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007800 /* Now check if the cookie contains a date field, which would
7801 * appear after a vertical bar ('|') just after the server name
7802 * and before the delimiter.
7803 */
7804 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7805 if (vbar1) {
7806 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007807 * right is the last seen date. It is a base64 encoded
7808 * 30-bit value representing the UNIX date since the
7809 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007810 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007811 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007812 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007813 if (val_end - vbar1 >= 5) {
7814 val = b64tos30(vbar1);
7815 if (val > 0)
7816 txn->cookie_last_date = val << 2;
7817 }
7818 /* look for a second vertical bar */
7819 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7820 if (vbar1 && (val_end - vbar1 > 5)) {
7821 val = b64tos30(vbar1 + 1);
7822 if (val > 0)
7823 txn->cookie_first_date = val << 2;
7824 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007825 }
7826 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007827
Willy Tarreauf64d1412010-10-07 20:06:11 +02007828 /* if the cookie has an expiration date and the proxy wants to check
7829 * it, then we do that now. We first check if the cookie is too old,
7830 * then only if it has expired. We detect strict overflow because the
7831 * time resolution here is not great (4 seconds). Cookies with dates
7832 * in the future are ignored if their offset is beyond one day. This
7833 * allows an admin to fix timezone issues without expiring everyone
7834 * and at the same time avoids keeping unwanted side effects for too
7835 * long.
7836 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007837 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7838 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007839 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007840 txn->flags &= ~TX_CK_MASK;
7841 txn->flags |= TX_CK_OLD;
7842 delim = val_beg; // let's pretend we have not found the cookie
7843 txn->cookie_first_date = 0;
7844 txn->cookie_last_date = 0;
7845 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007846 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7847 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007848 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007849 txn->flags &= ~TX_CK_MASK;
7850 txn->flags |= TX_CK_EXPIRED;
7851 delim = val_beg; // let's pretend we have not found the cookie
7852 txn->cookie_first_date = 0;
7853 txn->cookie_last_date = 0;
7854 }
7855
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007856 /* Here, we'll look for the first running server which supports the cookie.
7857 * This allows to share a same cookie between several servers, for example
7858 * to dedicate backup servers to specific servers only.
7859 * However, to prevent clients from sticking to cookie-less backup server
7860 * when they have incidentely learned an empty cookie, we simply ignore
7861 * empty cookies and mark them as invalid.
7862 * The same behaviour is applied when persistence must be ignored.
7863 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007864 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007865 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007866
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007867 while (srv) {
7868 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7869 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007870 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007871 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007872 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007873 /* we found the server and we can use it */
7874 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007875 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007876 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007877 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007878 break;
7879 } else {
7880 /* we found a server, but it's down,
7881 * mark it as such and go on in case
7882 * another one is available.
7883 */
7884 txn->flags &= ~TX_CK_MASK;
7885 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007886 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007887 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007888 srv = srv->next;
7889 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007890
Willy Tarreauf64d1412010-10-07 20:06:11 +02007891 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007892 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007893 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007894 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007895 txn->flags |= TX_CK_UNUSED;
7896 else
7897 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007898 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007899
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007900 /* depending on the cookie mode, we may have to either :
7901 * - delete the complete cookie if we're in insert+indirect mode, so that
7902 * the server never sees it ;
7903 * - remove the server id from the cookie value, and tag the cookie as an
7904 * application cookie so that it does not get accidentely removed later,
7905 * if we're in cookie prefix mode
7906 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007907 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007908 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007909
Willy Tarreau9b28e032012-10-12 23:49:43 +02007910 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007911 val_end += delta;
7912 next += delta;
7913 hdr_end += delta;
7914 hdr_next += delta;
7915 cur_hdr->len += delta;
7916 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007917
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007918 del_from = NULL;
7919 preserve_hdr = 1; /* we want to keep this cookie */
7920 }
7921 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007922 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007923 del_from = prev;
7924 }
7925 } else {
7926 /* This is not our cookie, so we must preserve it. But if we already
7927 * scheduled another cookie for removal, we cannot remove the
7928 * complete header, but we can remove the previous block itself.
7929 */
7930 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007931
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007932 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007933 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007934 if (att_beg >= del_from)
7935 att_beg += delta;
7936 if (att_end >= del_from)
7937 att_end += delta;
7938 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007939 val_end += delta;
7940 next += delta;
7941 hdr_end += delta;
7942 hdr_next += delta;
7943 cur_hdr->len += delta;
7944 http_msg_move_end(&txn->req, delta);
7945 prev = del_from;
7946 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007947 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007948 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007949
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007950 /* continue with next cookie on this header line */
7951 att_beg = next;
7952 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007953
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007954 /* There are no more cookies on this line.
7955 * We may still have one (or several) marked for deletion at the
7956 * end of the line. We must do this now in two ways :
7957 * - if some cookies must be preserved, we only delete from the
7958 * mark to the end of line ;
7959 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007960 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007961 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007962 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007963 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007964 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007965 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007966 cur_hdr->len += delta;
7967 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007968 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007969
7970 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007971 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7972 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007973 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007974 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007975 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007976 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007977 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007978 }
7979
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007980 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007981 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007982 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007983}
7984
7985
Willy Tarreaua15645d2007-03-18 16:22:39 +01007986/* Iterate the same filter through all response headers contained in <rtr>.
7987 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7988 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007989int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007990{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007991 char *cur_ptr, *cur_end, *cur_next;
7992 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007993 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007994 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007995 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007996
7997 last_hdr = 0;
7998
Willy Tarreau9b28e032012-10-12 23:49:43 +02007999 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008000 old_idx = 0;
8001
8002 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008003 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008004 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008005 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008006 (exp->action == ACT_ALLOW ||
8007 exp->action == ACT_DENY))
8008 return 0;
8009
8010 cur_idx = txn->hdr_idx.v[old_idx].next;
8011 if (!cur_idx)
8012 break;
8013
8014 cur_hdr = &txn->hdr_idx.v[cur_idx];
8015 cur_ptr = cur_next;
8016 cur_end = cur_ptr + cur_hdr->len;
8017 cur_next = cur_end + cur_hdr->cr + 1;
8018
8019 /* Now we have one header between cur_ptr and cur_end,
8020 * and the next header starts at cur_next.
8021 */
8022
Willy Tarreau15a53a42015-01-21 13:39:42 +01008023 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008024 switch (exp->action) {
8025 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008026 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008027 last_hdr = 1;
8028 break;
8029
8030 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008031 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008032 last_hdr = 1;
8033 break;
8034
8035 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008036 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8037 if (trash.len < 0)
8038 return -1;
8039
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008040 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008041 /* FIXME: if the user adds a newline in the replacement, the
8042 * index will not be recalculated for now, and the new line
8043 * will not be counted as a new header.
8044 */
8045
8046 cur_end += delta;
8047 cur_next += delta;
8048 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008049 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008050 break;
8051
8052 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008053 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008054 cur_next += delta;
8055
Willy Tarreaufa355d42009-11-29 18:12:29 +01008056 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008057 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8058 txn->hdr_idx.used--;
8059 cur_hdr->len = 0;
8060 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008061 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008062 break;
8063
8064 }
8065 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008066
8067 /* keep the link from this header to next one in case of later
8068 * removal of next header.
8069 */
8070 old_idx = cur_idx;
8071 }
8072 return 0;
8073}
8074
8075
8076/* Apply the filter to the status line in the response buffer <rtr>.
8077 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8078 * or -1 if a replacement resulted in an invalid status line.
8079 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008080int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008081{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008082 char *cur_ptr, *cur_end;
8083 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008084 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008085 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008086
8087
Willy Tarreau3d300592007-03-18 18:34:41 +01008088 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008089 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008090 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008091 (exp->action == ACT_ALLOW ||
8092 exp->action == ACT_DENY))
8093 return 0;
8094 else if (exp->action == ACT_REMOVE)
8095 return 0;
8096
8097 done = 0;
8098
Willy Tarreau9b28e032012-10-12 23:49:43 +02008099 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008100 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008101
8102 /* Now we have the status line between cur_ptr and cur_end */
8103
Willy Tarreau15a53a42015-01-21 13:39:42 +01008104 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008105 switch (exp->action) {
8106 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008107 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008108 done = 1;
8109 break;
8110
8111 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008112 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008113 done = 1;
8114 break;
8115
8116 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008117 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8118 if (trash.len < 0)
8119 return -1;
8120
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008121 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008122 /* FIXME: if the user adds a newline in the replacement, the
8123 * index will not be recalculated for now, and the new line
8124 * will not be counted as a new header.
8125 */
8126
Willy Tarreaufa355d42009-11-29 18:12:29 +01008127 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008128 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008129 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008130 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008131 cur_ptr, cur_end + 1,
8132 NULL, NULL);
8133 if (unlikely(!cur_end))
8134 return -1;
8135
8136 /* we have a full respnse and we know that we have either a CR
8137 * or an LF at <ptr>.
8138 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008139 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008140 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008141 /* there is no point trying this regex on headers */
8142 return 1;
8143 }
8144 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008145 return done;
8146}
8147
8148
8149
8150/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008151 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008152 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8153 * unparsable response.
8154 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008155int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008156{
Willy Tarreau192252e2015-04-04 01:47:55 +02008157 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008158 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008159 struct hdr_exp *exp;
8160
8161 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008162 int ret;
8163
8164 /*
8165 * The interleaving of transformations and verdicts
8166 * makes it difficult to decide to continue or stop
8167 * the evaluation.
8168 */
8169
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008170 if (txn->flags & TX_SVDENY)
8171 break;
8172
Willy Tarreau3d300592007-03-18 18:34:41 +01008173 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008174 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8175 exp->action == ACT_PASS)) {
8176 exp = exp->next;
8177 continue;
8178 }
8179
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008180 /* if this filter had a condition, evaluate it now and skip to
8181 * next filter if the condition does not match.
8182 */
8183 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008184 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008185 ret = acl_pass(ret);
8186 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8187 ret = !ret;
8188 if (!ret)
8189 continue;
8190 }
8191
Willy Tarreaua15645d2007-03-18 16:22:39 +01008192 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008193 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008194 if (unlikely(ret < 0))
8195 return -1;
8196
8197 if (likely(ret == 0)) {
8198 /* The filter did not match the response, it can be
8199 * iterated through all headers.
8200 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008201 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8202 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008203 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008204 }
8205 return 0;
8206}
8207
8208
Willy Tarreaua15645d2007-03-18 16:22:39 +01008209/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008210 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008211 * desirable to call it only when needed. This function is also used when we
8212 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008213 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008214void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008215{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008216 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008217 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008218 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008219 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008220 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008221 char *hdr_beg, *hdr_end, *hdr_next;
8222 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008223
Willy Tarreaua15645d2007-03-18 16:22:39 +01008224 /* Iterate through the headers.
8225 * we start with the start line.
8226 */
8227 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008228 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008229
8230 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8231 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008232 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008233
8234 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008235 hdr_beg = hdr_next;
8236 hdr_end = hdr_beg + cur_hdr->len;
8237 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008238
Willy Tarreau24581ba2010-08-31 22:39:35 +02008239 /* We have one full header between hdr_beg and hdr_end, and the
8240 * next header starts at hdr_next. We're only interested in
8241 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008242 */
8243
Willy Tarreau24581ba2010-08-31 22:39:35 +02008244 is_cookie2 = 0;
8245 prev = hdr_beg + 10;
8246 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008247 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008248 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8249 if (!val) {
8250 old_idx = cur_idx;
8251 continue;
8252 }
8253 is_cookie2 = 1;
8254 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008255 }
8256
Willy Tarreau24581ba2010-08-31 22:39:35 +02008257 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8258 * <prev> points to the colon.
8259 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008260 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008261
Willy Tarreau24581ba2010-08-31 22:39:35 +02008262 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8263 * check-cache is enabled) and we are not interested in checking
8264 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008265 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008266 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008267 return;
8268
Willy Tarreau24581ba2010-08-31 22:39:35 +02008269 /* OK so now we know we have to process this response cookie.
8270 * The format of the Set-Cookie header is slightly different
8271 * from the format of the Cookie header in that it does not
8272 * support the comma as a cookie delimiter (thus the header
8273 * cannot be folded) because the Expires attribute described in
8274 * the original Netscape's spec may contain an unquoted date
8275 * with a comma inside. We have to live with this because
8276 * many browsers don't support Max-Age and some browsers don't
8277 * support quoted strings. However the Set-Cookie2 header is
8278 * clean.
8279 *
8280 * We have to keep multiple pointers in order to support cookie
8281 * removal at the beginning, middle or end of header without
8282 * corrupting the header (in case of set-cookie2). A special
8283 * pointer, <scav> points to the beginning of the set-cookie-av
8284 * fields after the first semi-colon. The <next> pointer points
8285 * either to the end of line (set-cookie) or next unquoted comma
8286 * (set-cookie2). All of these headers are valid :
8287 *
8288 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8289 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8290 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8291 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8292 * | | | | | | | | | |
8293 * | | | | | | | | +-> next hdr_end <--+
8294 * | | | | | | | +------------> scav
8295 * | | | | | | +--------------> val_end
8296 * | | | | | +--------------------> val_beg
8297 * | | | | +----------------------> equal
8298 * | | | +------------------------> att_end
8299 * | | +----------------------------> att_beg
8300 * | +------------------------------> prev
8301 * +-----------------------------------------> hdr_beg
8302 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008303
Willy Tarreau24581ba2010-08-31 22:39:35 +02008304 for (; prev < hdr_end; prev = next) {
8305 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008306
Willy Tarreau24581ba2010-08-31 22:39:35 +02008307 /* find att_beg */
8308 att_beg = prev + 1;
8309 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8310 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008311
Willy Tarreau24581ba2010-08-31 22:39:35 +02008312 /* find att_end : this is the first character after the last non
8313 * space before the equal. It may be equal to hdr_end.
8314 */
8315 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008316
Willy Tarreau24581ba2010-08-31 22:39:35 +02008317 while (equal < hdr_end) {
8318 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8319 break;
8320 if (http_is_spht[(unsigned char)*equal++])
8321 continue;
8322 att_end = equal;
8323 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008324
Willy Tarreau24581ba2010-08-31 22:39:35 +02008325 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8326 * is between <att_beg> and <equal>, both may be identical.
8327 */
8328
8329 /* look for end of cookie if there is an equal sign */
8330 if (equal < hdr_end && *equal == '=') {
8331 /* look for the beginning of the value */
8332 val_beg = equal + 1;
8333 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8334 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008335
Willy Tarreau24581ba2010-08-31 22:39:35 +02008336 /* find the end of the value, respecting quotes */
8337 next = find_cookie_value_end(val_beg, hdr_end);
8338
8339 /* make val_end point to the first white space or delimitor after the value */
8340 val_end = next;
8341 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8342 val_end--;
8343 } else {
8344 /* <equal> points to next comma, semi-colon or EOL */
8345 val_beg = val_end = next = equal;
8346 }
8347
8348 if (next < hdr_end) {
8349 /* Set-Cookie2 supports multiple cookies, and <next> points to
8350 * a colon or semi-colon before the end. So skip all attr-value
8351 * pairs and look for the next comma. For Set-Cookie, since
8352 * commas are permitted in values, skip to the end.
8353 */
8354 if (is_cookie2)
8355 next = find_hdr_value_end(next, hdr_end);
8356 else
8357 next = hdr_end;
8358 }
8359
8360 /* Now everything is as on the diagram above */
8361
8362 /* Ignore cookies with no equal sign */
8363 if (equal == val_end)
8364 continue;
8365
8366 /* If there are spaces around the equal sign, we need to
8367 * strip them otherwise we'll get trouble for cookie captures,
8368 * or even for rewrites. Since this happens extremely rarely,
8369 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008370 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008371 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8372 int stripped_before = 0;
8373 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008374
Willy Tarreau24581ba2010-08-31 22:39:35 +02008375 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008376 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008377 equal += stripped_before;
8378 val_beg += stripped_before;
8379 }
8380
8381 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008382 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008383 val_beg += stripped_after;
8384 stripped_before += stripped_after;
8385 }
8386
8387 val_end += stripped_before;
8388 next += stripped_before;
8389 hdr_end += stripped_before;
8390 hdr_next += stripped_before;
8391 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008392 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008393 }
8394
8395 /* First, let's see if we want to capture this cookie. We check
8396 * that we don't already have a server side cookie, because we
8397 * can only capture one. Also as an optimisation, we ignore
8398 * cookies shorter than the declared name.
8399 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008400 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008401 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008402 (val_end - att_beg >= sess->fe->capture_namelen) &&
8403 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008404 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008405 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008406 Alert("HTTP logging : out of memory.\n");
8407 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008408 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008409 if (log_len > sess->fe->capture_len)
8410 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008411 memcpy(txn->srv_cookie, att_beg, log_len);
8412 txn->srv_cookie[log_len] = 0;
8413 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008414 }
8415
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008416 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008417 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008418 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008419 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8420 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008421 /* assume passive cookie by default */
8422 txn->flags &= ~TX_SCK_MASK;
8423 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008424
8425 /* If the cookie is in insert mode on a known server, we'll delete
8426 * this occurrence because we'll insert another one later.
8427 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008428 * a direct access.
8429 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008430 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008431 /* The "preserve" flag was set, we don't want to touch the
8432 * server's cookie.
8433 */
8434 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008435 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008436 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008437 /* this cookie must be deleted */
8438 if (*prev == ':' && next == hdr_end) {
8439 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008440 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008441 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8442 txn->hdr_idx.used--;
8443 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008444 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008445 hdr_next += delta;
8446 http_msg_move_end(&txn->rsp, delta);
8447 /* note: while both invalid now, <next> and <hdr_end>
8448 * are still equal, so the for() will stop as expected.
8449 */
8450 } else {
8451 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008452 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008453 next = prev;
8454 hdr_end += delta;
8455 hdr_next += delta;
8456 cur_hdr->len += delta;
8457 http_msg_move_end(&txn->rsp, delta);
8458 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008459 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008460 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008461 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008462 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008463 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008464 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008465 * with this server since we know it.
8466 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008467 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008468 next += delta;
8469 hdr_end += delta;
8470 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008471 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008472 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008473
Willy Tarreauf1348312010-10-07 15:54:11 +02008474 txn->flags &= ~TX_SCK_MASK;
8475 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008476 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008477 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008478 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008479 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008480 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008481 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008482 next += delta;
8483 hdr_end += delta;
8484 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008485 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008486 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008487
Willy Tarreau827aee92011-03-10 16:55:02 +01008488 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008489 txn->flags &= ~TX_SCK_MASK;
8490 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008491 }
8492 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008493 /* that's done for this cookie, check the next one on the same
8494 * line when next != hdr_end (only if is_cookie2).
8495 */
8496 }
8497 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008498 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008499 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008500}
8501
8502
Willy Tarreaua15645d2007-03-18 16:22:39 +01008503/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008504 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008505 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008506void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008507{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008508 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008509 char *p1, *p2;
8510
8511 char *cur_ptr, *cur_end, *cur_next;
8512 int cur_idx;
8513
Willy Tarreau5df51872007-11-25 16:20:08 +01008514 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008515 return;
8516
8517 /* Iterate through the headers.
8518 * we start with the start line.
8519 */
8520 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008521 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008522
8523 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8524 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008525 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008526
8527 cur_hdr = &txn->hdr_idx.v[cur_idx];
8528 cur_ptr = cur_next;
8529 cur_end = cur_ptr + cur_hdr->len;
8530 cur_next = cur_end + cur_hdr->cr + 1;
8531
8532 /* We have one full header between cur_ptr and cur_end, and the
8533 * next header starts at cur_next. We're only interested in
8534 * "Cookie:" headers.
8535 */
8536
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008537 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8538 if (val) {
8539 if ((cur_end - (cur_ptr + val) >= 8) &&
8540 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8541 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8542 return;
8543 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008544 }
8545
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008546 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8547 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008548 continue;
8549
8550 /* OK, right now we know we have a cache-control header at cur_ptr */
8551
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008552 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008553
8554 if (p1 >= cur_end) /* no more info */
8555 continue;
8556
8557 /* p1 is at the beginning of the value */
8558 p2 = p1;
8559
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008560 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008561 p2++;
8562
8563 /* we have a complete value between p1 and p2 */
8564 if (p2 < cur_end && *p2 == '=') {
8565 /* we have something of the form no-cache="set-cookie" */
8566 if ((cur_end - p1 >= 21) &&
8567 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8568 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008569 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008570 continue;
8571 }
8572
8573 /* OK, so we know that either p2 points to the end of string or to a comma */
8574 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008575 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008576 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8577 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8578 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008579 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008580 return;
8581 }
8582
8583 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008584 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008585 continue;
8586 }
8587 }
8588}
8589
Willy Tarreau58f10d72006-12-04 02:26:12 +01008590
Willy Tarreaub2513902006-12-17 14:52:38 +01008591/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008592 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008593 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008594 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008595 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008596 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008597 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008598 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008599 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008600int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008601{
8602 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008603 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008604 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008605
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008606 if (!uri_auth)
8607 return 0;
8608
Cyril Bonté70be45d2010-10-12 00:14:35 +02008609 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008610 return 0;
8611
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008612 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008613 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008614 return 0;
8615
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008616 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008617 return 0;
8618
Willy Tarreaub2513902006-12-17 14:52:38 +01008619 return 1;
8620}
8621
Willy Tarreau4076a152009-04-02 15:18:36 +02008622/*
8623 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008624 * By default it tries to report the error position as msg->err_pos. However if
8625 * this one is not set, it will then report msg->next, which is the last known
8626 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008627 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008628 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008629void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008630 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008631 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008632{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008633 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008634 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008635 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008636
Willy Tarreau9b28e032012-10-12 23:49:43 +02008637 es->len = MIN(chn->buf->i, sizeof(es->buf));
8638 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008639 len1 = MIN(len1, es->len);
8640 len2 = es->len - len1; /* remaining data if buffer wraps */
8641
Willy Tarreau9b28e032012-10-12 23:49:43 +02008642 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008643 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008644 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008645
Willy Tarreau4076a152009-04-02 15:18:36 +02008646 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008647 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008648 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008649 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008650
Willy Tarreau4076a152009-04-02 15:18:36 +02008651 es->when = date; // user-visible date
8652 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008653 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008654 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008655 if (objt_conn(sess->origin))
8656 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008657 else
8658 memset(&es->src, 0, sizeof(es->src));
8659
Willy Tarreau078272e2010-12-12 12:46:33 +01008660 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008661 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008662 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008663 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008664 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008665 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008666 es->b_out = chn->buf->o;
8667 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008668 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008669 es->m_clen = msg->chunk_len;
8670 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008671}
Willy Tarreaub2513902006-12-17 14:52:38 +01008672
Willy Tarreau294c4732011-12-16 21:35:50 +01008673/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8674 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8675 * performed over the whole headers. Otherwise it must contain a valid header
8676 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8677 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8678 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8679 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008680 * -1. The value fetch stops at commas, so this function is suited for use with
8681 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008682 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008683 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008684unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008685 struct hdr_idx *idx, int occ,
8686 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008687{
Willy Tarreau294c4732011-12-16 21:35:50 +01008688 struct hdr_ctx local_ctx;
8689 char *ptr_hist[MAX_HDR_HISTORY];
8690 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008691 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008692 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008693
Willy Tarreau294c4732011-12-16 21:35:50 +01008694 if (!ctx) {
8695 local_ctx.idx = 0;
8696 ctx = &local_ctx;
8697 }
8698
Willy Tarreaubce70882009-09-07 11:51:47 +02008699 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008700 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008701 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008702 occ--;
8703 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008704 *vptr = ctx->line + ctx->val;
8705 *vlen = ctx->vlen;
8706 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008707 }
8708 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008709 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008710 }
8711
8712 /* negative occurrence, we scan all the list then walk back */
8713 if (-occ > MAX_HDR_HISTORY)
8714 return 0;
8715
Willy Tarreau294c4732011-12-16 21:35:50 +01008716 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008717 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008718 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8719 len_hist[hist_ptr] = ctx->vlen;
8720 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008721 hist_ptr = 0;
8722 found++;
8723 }
8724 if (-occ > found)
8725 return 0;
8726 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008727 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8728 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8729 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008730 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008731 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008732 if (hist_ptr >= MAX_HDR_HISTORY)
8733 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008734 *vptr = ptr_hist[hist_ptr];
8735 *vlen = len_hist[hist_ptr];
8736 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008737}
8738
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008739/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8740 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8741 * performed over the whole headers. Otherwise it must contain a valid header
8742 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8743 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8744 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8745 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8746 * -1. This function differs from http_get_hdr() in that it only returns full
8747 * line header values and does not stop at commas.
8748 * The return value is 0 if nothing was found, or non-zero otherwise.
8749 */
8750unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8751 struct hdr_idx *idx, int occ,
8752 struct hdr_ctx *ctx, char **vptr, int *vlen)
8753{
8754 struct hdr_ctx local_ctx;
8755 char *ptr_hist[MAX_HDR_HISTORY];
8756 int len_hist[MAX_HDR_HISTORY];
8757 unsigned int hist_ptr;
8758 int found;
8759
8760 if (!ctx) {
8761 local_ctx.idx = 0;
8762 ctx = &local_ctx;
8763 }
8764
8765 if (occ >= 0) {
8766 /* search from the beginning */
8767 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8768 occ--;
8769 if (occ <= 0) {
8770 *vptr = ctx->line + ctx->val;
8771 *vlen = ctx->vlen;
8772 return 1;
8773 }
8774 }
8775 return 0;
8776 }
8777
8778 /* negative occurrence, we scan all the list then walk back */
8779 if (-occ > MAX_HDR_HISTORY)
8780 return 0;
8781
8782 found = hist_ptr = 0;
8783 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8784 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8785 len_hist[hist_ptr] = ctx->vlen;
8786 if (++hist_ptr >= MAX_HDR_HISTORY)
8787 hist_ptr = 0;
8788 found++;
8789 }
8790 if (-occ > found)
8791 return 0;
8792 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8793 * find occurrence -occ, so we have to check [hist_ptr+occ].
8794 */
8795 hist_ptr += occ;
8796 if (hist_ptr >= MAX_HDR_HISTORY)
8797 hist_ptr -= MAX_HDR_HISTORY;
8798 *vptr = ptr_hist[hist_ptr];
8799 *vlen = len_hist[hist_ptr];
8800 return 1;
8801}
8802
Willy Tarreaubaaee002006-06-26 02:48:02 +02008803/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008804 * Print a debug line with a header. Always stop at the first CR or LF char,
8805 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8806 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008807 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008808void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008809{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008810 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008811 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008812
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008813 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008814 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008815 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008816 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 +02008817
8818 for (max = 0; start + max < end; max++)
8819 if (start[max] == '\r' || start[max] == '\n')
8820 break;
8821
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008822 UBOUND(max, trash.size - trash.len - 3);
8823 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8824 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008825 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008826}
8827
Willy Tarreaueee5b512015-04-03 23:46:31 +02008828
8829/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8830 * The hdr_idx is allocated as well. In case of allocation failure, everything
8831 * allocated is freed and NULL is returned. Otherwise the new transaction is
8832 * assigned to the stream and returned.
8833 */
8834struct http_txn *http_alloc_txn(struct stream *s)
8835{
8836 struct http_txn *txn = s->txn;
8837
8838 if (txn)
8839 return txn;
8840
8841 txn = pool_alloc2(pool2_http_txn);
8842 if (!txn)
8843 return txn;
8844
8845 txn->hdr_idx.size = global.tune.max_http_hdr;
8846 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8847 if (!txn->hdr_idx.v) {
8848 pool_free2(pool2_http_txn, txn);
8849 return NULL;
8850 }
8851
8852 s->txn = txn;
8853 return txn;
8854}
8855
Willy Tarreau0937bc42009-12-22 15:03:09 +01008856/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008857 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008858 * the required fields are properly allocated and that we only need to (re)init
8859 * them. This should be used before processing any new request.
8860 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008861void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008862{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008863 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008864 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008865
8866 txn->flags = 0;
8867 txn->status = -1;
8868
Willy Tarreauf64d1412010-10-07 20:06:11 +02008869 txn->cookie_first_date = 0;
8870 txn->cookie_last_date = 0;
8871
Willy Tarreaueee5b512015-04-03 23:46:31 +02008872 txn->srv_cookie = NULL;
8873 txn->cli_cookie = NULL;
8874 txn->uri = NULL;
8875
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008876 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008877 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008878 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008879 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008880 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008881 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008882 txn->req.chunk_len = 0LL;
8883 txn->req.body_len = 0LL;
8884 txn->rsp.chunk_len = 0LL;
8885 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008886 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8887 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008888 txn->req.chn = &s->req;
8889 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008890
8891 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008892
8893 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8894 if (fe->options2 & PR_O2_REQBUG_OK)
8895 txn->req.err_pos = -1; /* let buggy requests pass */
8896
Willy Tarreau0937bc42009-12-22 15:03:09 +01008897 if (txn->hdr_idx.v)
8898 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008899
8900 vars_init(&s->vars_txn, SCOPE_TXN);
8901 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008902}
8903
8904/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008905void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008906{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008907 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008908 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008909
Willy Tarreau75195602014-03-11 15:48:55 +01008910 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008911 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008912 s->comp_algo->end(&s->comp_ctx);
8913 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008914 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008915
Willy Tarreau0937bc42009-12-22 15:03:09 +01008916 /* these ones will have been dynamically allocated */
8917 pool_free2(pool2_requri, txn->uri);
8918 pool_free2(pool2_capture, txn->cli_cookie);
8919 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008920 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008921
William Lallemanda73203e2012-03-12 12:48:57 +01008922 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008923 txn->uri = NULL;
8924 txn->srv_cookie = NULL;
8925 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008926
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008927 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008928 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008929 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008930 pool_free2(h->pool, s->req_cap[h->index]);
8931 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008932 }
8933
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008934 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008935 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008936 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008937 pool_free2(h->pool, s->res_cap[h->index]);
8938 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008939 }
8940
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008941 vars_prune(&s->vars_txn, s);
8942 vars_prune(&s->vars_reqres, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008943}
8944
8945/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008946void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008947{
8948 http_end_txn(s);
8949 http_init_txn(s);
8950
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008951 /* reinitialise the current rule list pointer to NULL. We are sure that
8952 * any rulelist match the NULL pointer.
8953 */
8954 s->current_rule_list = NULL;
8955
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008956 s->be = strm_fe(s);
8957 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008958 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008959 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008960 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008961 /* re-init store persistence */
8962 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008963 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008964
Willy Tarreau0937bc42009-12-22 15:03:09 +01008965 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008966
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008967 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008968
Willy Tarreau739cfba2010-01-25 23:11:14 +01008969 /* We must trim any excess data from the response buffer, because we
8970 * may have blocked an invalid response from a server that we don't
8971 * want to accidentely forward once we disable the analysers, nor do
8972 * we want those data to come along with next response. A typical
8973 * example of such data would be from a buggy server responding to
8974 * a HEAD with some data, or sending more than the advertised
8975 * content-length.
8976 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008977 if (unlikely(s->res.buf->i))
8978 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008979
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008980 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008981 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008982
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008983 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008984 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008985
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008986 s->req.rex = TICK_ETERNITY;
8987 s->req.wex = TICK_ETERNITY;
8988 s->req.analyse_exp = TICK_ETERNITY;
8989 s->res.rex = TICK_ETERNITY;
8990 s->res.wex = TICK_ETERNITY;
8991 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008992}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008993
Sasha Pachev218f0642014-06-16 12:05:59 -06008994void free_http_res_rules(struct list *r)
8995{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008996 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008997
8998 list_for_each_entry_safe(pr, tr, r, list) {
8999 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009000 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009001 free(pr);
9002 }
9003}
9004
9005void free_http_req_rules(struct list *r)
9006{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009007 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009008
9009 list_for_each_entry_safe(pr, tr, r, list) {
9010 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009011 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009012 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009013
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009014 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009015 free(pr);
9016 }
9017}
9018
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009019/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009020struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009021{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009022 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009023 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009024 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009025 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009026
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009027 rule = (struct act_rule*)calloc(1, sizeof(struct act_rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009028 if (!rule) {
9029 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009030 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009031 }
9032
CJ Ess108b1dd2015-04-07 12:03:37 -04009033 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009034 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009035 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009036 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009037 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009038 int code;
9039 int hc;
9040
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009041 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01009042 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009043 if (strcmp(args[cur_arg], "deny_status") == 0) {
9044 cur_arg++;
9045 if (!args[cur_arg]) {
9046 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9047 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9048 goto out_err;
9049 }
9050
9051 code = atol(args[cur_arg]);
9052 cur_arg++;
9053 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9054 if (http_err_codes[hc] == code) {
9055 rule->deny_status = hc;
9056 break;
9057 }
9058 }
9059
9060 if (hc >= HTTP_ERR_SIZE) {
9061 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9062 file, linenum, code);
9063 }
9064 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009065 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009066 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009067 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009068 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009069 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009070 cur_arg = 1;
9071
9072 while(*args[cur_arg]) {
9073 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009074 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009075 cur_arg+=2;
9076 continue;
9077 } else
9078 break;
9079 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009080 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009081 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009082 cur_arg = 1;
9083
9084 if (!*args[cur_arg] ||
9085 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9086 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9087 file, linenum, args[0]);
9088 goto out_err;
9089 }
9090 rule->arg.nice = atoi(args[cur_arg]);
9091 if (rule->arg.nice < -1024)
9092 rule->arg.nice = -1024;
9093 else if (rule->arg.nice > 1024)
9094 rule->arg.nice = 1024;
9095 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009096 } else if (!strcmp(args[0], "set-tos")) {
9097#ifdef IP_TOS
9098 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009099 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009100 cur_arg = 1;
9101
9102 if (!*args[cur_arg] ||
9103 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9104 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9105 file, linenum, args[0]);
9106 goto out_err;
9107 }
9108
9109 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9110 if (err && *err != '\0') {
9111 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9112 file, linenum, err, args[0]);
9113 goto out_err;
9114 }
9115 cur_arg++;
9116#else
9117 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9118 goto out_err;
9119#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009120 } else if (!strcmp(args[0], "set-mark")) {
9121#ifdef SO_MARK
9122 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009123 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009124 cur_arg = 1;
9125
9126 if (!*args[cur_arg] ||
9127 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9128 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9129 file, linenum, args[0]);
9130 goto out_err;
9131 }
9132
9133 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9134 if (err && *err != '\0') {
9135 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9136 file, linenum, err, args[0]);
9137 goto out_err;
9138 }
9139 cur_arg++;
9140 global.last_checks |= LSTCHK_NETADM;
9141#else
9142 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9143 goto out_err;
9144#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009145 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009146 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009147 cur_arg = 1;
9148
9149 if (!*args[cur_arg] ||
9150 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9151 bad_log_level:
9152 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9153 file, linenum, args[0]);
9154 goto out_err;
9155 }
9156 if (strcmp(args[cur_arg], "silent") == 0)
9157 rule->arg.loglevel = -1;
9158 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9159 goto bad_log_level;
9160 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009161 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009162 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009163 cur_arg = 1;
9164
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009165 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9166 (*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 +01009167 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9168 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009169 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009170 }
9171
9172 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9173 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9174 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009175
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009176 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009177 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009178 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9179 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009180 free(proxy->conf.lfs_file);
9181 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9182 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009183 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009184 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009185 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009186 cur_arg = 1;
9187
9188 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009189 (*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 -06009190 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9191 file, linenum, args[0]);
9192 goto out_err;
9193 }
9194
9195 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9196 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9197 LIST_INIT(&rule->arg.hdr_add.fmt);
9198
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009199 error = NULL;
9200 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9201 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9202 args[cur_arg + 1], error);
9203 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009204 goto out_err;
9205 }
9206
9207 proxy->conf.args.ctx = ARGC_HRQ;
9208 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9209 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9210 file, linenum);
9211
9212 free(proxy->conf.lfs_file);
9213 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9214 proxy->conf.lfs_line = proxy->conf.args.line;
9215 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009216 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009217 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009218 cur_arg = 1;
9219
9220 if (!*args[cur_arg] ||
9221 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9222 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9223 file, linenum, args[0]);
9224 goto out_err;
9225 }
9226
9227 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9228 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9229
9230 proxy->conf.args.ctx = ARGC_HRQ;
9231 free(proxy->conf.lfs_file);
9232 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9233 proxy->conf.lfs_line = proxy->conf.args.line;
9234 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009235 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9236 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009237 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009238 struct sample_expr *expr;
9239 unsigned int where;
9240 char *err = NULL;
9241
9242 cur_arg = 1;
9243 proxy->conf.args.ctx = ARGC_TRK;
9244
9245 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9246 if (!expr) {
9247 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9248 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9249 free(err);
9250 goto out_err;
9251 }
9252
9253 where = 0;
9254 if (proxy->cap & PR_CAP_FE)
9255 where |= SMP_VAL_FE_HRQ_HDR;
9256 if (proxy->cap & PR_CAP_BE)
9257 where |= SMP_VAL_BE_HRQ_HDR;
9258
9259 if (!(expr->fetch->val & where)) {
9260 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9261 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9262 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9263 args[cur_arg-1], sample_src_names(expr->fetch->use));
9264 free(expr);
9265 goto out_err;
9266 }
9267
9268 if (strcmp(args[cur_arg], "table") == 0) {
9269 cur_arg++;
9270 if (!args[cur_arg]) {
9271 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9272 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9273 free(expr);
9274 goto out_err;
9275 }
9276 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009277 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009278 cur_arg++;
9279 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009280 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009281 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009282 } else if (strcmp(args[0], "redirect") == 0) {
9283 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009284 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009285
Willy Tarreaube4653b2015-05-28 15:26:58 +02009286 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009287 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9288 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9289 goto out_err;
9290 }
9291
9292 /* this redirect rule might already contain a parsed condition which
9293 * we'll pass to the http-request rule.
9294 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009295 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009296 rule->arg.redir = redir;
9297 rule->cond = redir->cond;
9298 redir->cond = NULL;
9299 cur_arg = 2;
9300 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009301 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9302 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009303 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009304 /*
9305 * '+ 8' for 'add-acl('
9306 * '- 9' for 'add-acl(' + trailing ')'
9307 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009308 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009309
9310 cur_arg = 1;
9311
9312 if (!*args[cur_arg] ||
9313 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9314 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9315 file, linenum, args[0]);
9316 goto out_err;
9317 }
9318
9319 LIST_INIT(&rule->arg.map.key);
9320 proxy->conf.args.ctx = ARGC_HRQ;
9321 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9322 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9323 file, linenum);
9324 free(proxy->conf.lfs_file);
9325 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9326 proxy->conf.lfs_line = proxy->conf.args.line;
9327 cur_arg += 1;
9328 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9329 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009330 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009331 /*
9332 * '+ 8' for 'del-acl('
9333 * '- 9' for 'del-acl(' + trailing ')'
9334 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009335 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009336
9337 cur_arg = 1;
9338
9339 if (!*args[cur_arg] ||
9340 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9341 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9342 file, linenum, args[0]);
9343 goto out_err;
9344 }
9345
9346 LIST_INIT(&rule->arg.map.key);
9347 proxy->conf.args.ctx = ARGC_HRQ;
9348 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9349 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9350 file, linenum);
9351 free(proxy->conf.lfs_file);
9352 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9353 proxy->conf.lfs_line = proxy->conf.args.line;
9354 cur_arg += 1;
9355 } else if (strncmp(args[0], "del-map", 7) == 0) {
9356 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009357 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009358 /*
9359 * '+ 8' for 'del-map('
9360 * '- 9' for 'del-map(' + trailing ')'
9361 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009362 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009363
9364 cur_arg = 1;
9365
9366 if (!*args[cur_arg] ||
9367 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9368 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9369 file, linenum, args[0]);
9370 goto out_err;
9371 }
9372
9373 LIST_INIT(&rule->arg.map.key);
9374 proxy->conf.args.ctx = ARGC_HRQ;
9375 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9376 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9377 file, linenum);
9378 free(proxy->conf.lfs_file);
9379 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9380 proxy->conf.lfs_line = proxy->conf.args.line;
9381 cur_arg += 1;
9382 } else if (strncmp(args[0], "set-map", 7) == 0) {
9383 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009384 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009385 /*
9386 * '+ 8' for 'set-map('
9387 * '- 9' for 'set-map(' + trailing ')'
9388 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009389 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009390
9391 cur_arg = 1;
9392
9393 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9394 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9395 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9396 file, linenum, args[0]);
9397 goto out_err;
9398 }
9399
9400 LIST_INIT(&rule->arg.map.key);
9401 LIST_INIT(&rule->arg.map.value);
9402 proxy->conf.args.ctx = ARGC_HRQ;
9403
9404 /* key pattern */
9405 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9406 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9407 file, linenum);
9408
9409 /* value pattern */
9410 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9411 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9412 file, linenum);
9413 free(proxy->conf.lfs_file);
9414 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9415 proxy->conf.lfs_line = proxy->conf.args.line;
9416
9417 cur_arg += 2;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009418 } else if (strncmp(args[0], "set-src", 7) == 0) {
9419 struct sample_expr *expr;
9420 unsigned int where;
9421 char *err = NULL;
9422
9423 cur_arg = 1;
9424 proxy->conf.args.ctx = ARGC_HRQ;
9425
9426 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9427 if (!expr) {
9428 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9429 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9430 free(err);
9431 goto out_err;
9432 }
9433
9434 where = 0;
9435 if (proxy->cap & PR_CAP_FE)
9436 where |= SMP_VAL_FE_HRQ_HDR;
9437 if (proxy->cap & PR_CAP_BE)
9438 where |= SMP_VAL_BE_HRQ_HDR;
9439
9440 if (!(expr->fetch->val & where)) {
9441 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9442 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9443 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9444 args[cur_arg-1], sample_src_names(expr->fetch->use));
9445 free(expr);
9446 goto out_err;
9447 }
9448
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02009449 rule->arg.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009450 rule->action = ACT_HTTP_REQ_SET_SRC;
William Lallemand73025dd2014-04-24 14:38:37 +02009451 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9452 char *errmsg = NULL;
9453 cur_arg = 1;
9454 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009455 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009456 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009457 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9458 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9459 free(errmsg);
9460 goto out_err;
9461 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009462 } else {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009463 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 +01009464 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009465 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009466 }
9467
9468 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9469 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009470 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009471
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009472 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9473 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9474 file, linenum, args[0], errmsg);
9475 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009476 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009477 }
9478 rule->cond = cond;
9479 }
9480 else if (*args[cur_arg]) {
9481 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9482 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9483 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009484 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009485 }
9486
9487 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009488 out_err:
9489 free(rule);
9490 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009491}
9492
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009493/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009494struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009495{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009496 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009497 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009498 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009499 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009500
9501 rule = calloc(1, sizeof(*rule));
9502 if (!rule) {
9503 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9504 goto out_err;
9505 }
9506
9507 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009508 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009509 cur_arg = 1;
9510 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009511 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009512 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009513 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009514 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009515 cur_arg = 1;
9516
9517 if (!*args[cur_arg] ||
9518 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9519 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9520 file, linenum, args[0]);
9521 goto out_err;
9522 }
9523 rule->arg.nice = atoi(args[cur_arg]);
9524 if (rule->arg.nice < -1024)
9525 rule->arg.nice = -1024;
9526 else if (rule->arg.nice > 1024)
9527 rule->arg.nice = 1024;
9528 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009529 } else if (!strcmp(args[0], "set-tos")) {
9530#ifdef IP_TOS
9531 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009532 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009533 cur_arg = 1;
9534
9535 if (!*args[cur_arg] ||
9536 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9537 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9538 file, linenum, args[0]);
9539 goto out_err;
9540 }
9541
9542 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9543 if (err && *err != '\0') {
9544 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9545 file, linenum, err, args[0]);
9546 goto out_err;
9547 }
9548 cur_arg++;
9549#else
9550 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9551 goto out_err;
9552#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009553 } else if (!strcmp(args[0], "set-mark")) {
9554#ifdef SO_MARK
9555 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009556 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009557 cur_arg = 1;
9558
9559 if (!*args[cur_arg] ||
9560 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9561 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9562 file, linenum, args[0]);
9563 goto out_err;
9564 }
9565
9566 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9567 if (err && *err != '\0') {
9568 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9569 file, linenum, err, args[0]);
9570 goto out_err;
9571 }
9572 cur_arg++;
9573 global.last_checks |= LSTCHK_NETADM;
9574#else
9575 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9576 goto out_err;
9577#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009578 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009579 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009580 cur_arg = 1;
9581
9582 if (!*args[cur_arg] ||
9583 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9584 bad_log_level:
9585 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9586 file, linenum, args[0]);
9587 goto out_err;
9588 }
9589 if (strcmp(args[cur_arg], "silent") == 0)
9590 rule->arg.loglevel = -1;
9591 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9592 goto bad_log_level;
9593 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009594 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009595 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009596 cur_arg = 1;
9597
9598 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9599 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9600 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9601 file, linenum, args[0]);
9602 goto out_err;
9603 }
9604
9605 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9606 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9607 LIST_INIT(&rule->arg.hdr_add.fmt);
9608
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009609 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009610 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009611 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9612 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009613 free(proxy->conf.lfs_file);
9614 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9615 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009616 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009617 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009618 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009619 cur_arg = 1;
9620
9621 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009622 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9623 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009624 file, linenum, args[0]);
9625 goto out_err;
9626 }
9627
9628 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9629 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9630 LIST_INIT(&rule->arg.hdr_add.fmt);
9631
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009632 error = NULL;
9633 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9634 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9635 args[cur_arg + 1], error);
9636 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009637 goto out_err;
9638 }
9639
9640 proxy->conf.args.ctx = ARGC_HRQ;
9641 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9642 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9643 file, linenum);
9644
9645 free(proxy->conf.lfs_file);
9646 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9647 proxy->conf.lfs_line = proxy->conf.args.line;
9648 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009649 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009650 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009651 cur_arg = 1;
9652
9653 if (!*args[cur_arg] ||
9654 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9655 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9656 file, linenum, args[0]);
9657 goto out_err;
9658 }
9659
9660 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9661 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9662
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009663 proxy->conf.args.ctx = ARGC_HRS;
9664 free(proxy->conf.lfs_file);
9665 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9666 proxy->conf.lfs_line = proxy->conf.args.line;
9667 cur_arg += 1;
9668 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9669 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009670 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009671 /*
9672 * '+ 8' for 'add-acl('
9673 * '- 9' for 'add-acl(' + trailing ')'
9674 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009675 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009676
9677 cur_arg = 1;
9678
9679 if (!*args[cur_arg] ||
9680 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9681 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9682 file, linenum, args[0]);
9683 goto out_err;
9684 }
9685
9686 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009687 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009688 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9689 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9690 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009691 free(proxy->conf.lfs_file);
9692 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9693 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009694
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009695 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009696 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9697 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009698 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009699 /*
9700 * '+ 8' for 'del-acl('
9701 * '- 9' for 'del-acl(' + trailing ')'
9702 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009703 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009704
9705 cur_arg = 1;
9706
9707 if (!*args[cur_arg] ||
9708 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9709 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9710 file, linenum, args[0]);
9711 goto out_err;
9712 }
9713
9714 LIST_INIT(&rule->arg.map.key);
9715 proxy->conf.args.ctx = ARGC_HRS;
9716 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9717 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9718 file, linenum);
9719 free(proxy->conf.lfs_file);
9720 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9721 proxy->conf.lfs_line = proxy->conf.args.line;
9722 cur_arg += 1;
9723 } else if (strncmp(args[0], "del-map", 7) == 0) {
9724 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009725 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009726 /*
9727 * '+ 8' for 'del-map('
9728 * '- 9' for 'del-map(' + trailing ')'
9729 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009730 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009731
9732 cur_arg = 1;
9733
9734 if (!*args[cur_arg] ||
9735 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9736 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9737 file, linenum, args[0]);
9738 goto out_err;
9739 }
9740
9741 LIST_INIT(&rule->arg.map.key);
9742 proxy->conf.args.ctx = ARGC_HRS;
9743 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9744 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9745 file, linenum);
9746 free(proxy->conf.lfs_file);
9747 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9748 proxy->conf.lfs_line = proxy->conf.args.line;
9749 cur_arg += 1;
9750 } else if (strncmp(args[0], "set-map", 7) == 0) {
9751 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009752 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009753 /*
9754 * '+ 8' for 'set-map('
9755 * '- 9' for 'set-map(' + trailing ')'
9756 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009757 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009758
9759 cur_arg = 1;
9760
9761 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9762 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9763 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9764 file, linenum, args[0]);
9765 goto out_err;
9766 }
9767
9768 LIST_INIT(&rule->arg.map.key);
9769 LIST_INIT(&rule->arg.map.value);
9770
9771 proxy->conf.args.ctx = ARGC_HRS;
9772
9773 /* key pattern */
9774 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9775 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9776 file, linenum);
9777
9778 /* value pattern */
9779 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9780 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9781 file, linenum);
9782
9783 free(proxy->conf.lfs_file);
9784 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9785 proxy->conf.lfs_line = proxy->conf.args.line;
9786
9787 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009788 } else if (strcmp(args[0], "redirect") == 0) {
9789 struct redirect_rule *redir;
9790 char *errmsg = NULL;
9791
9792 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9793 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9794 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9795 goto out_err;
9796 }
9797
9798 /* this redirect rule might already contain a parsed condition which
9799 * we'll pass to the http-request rule.
9800 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009801 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009802 rule->arg.redir = redir;
9803 rule->cond = redir->cond;
9804 redir->cond = NULL;
9805 cur_arg = 2;
9806 return rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009807 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9808 char *errmsg = NULL;
9809 cur_arg = 1;
9810 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009811 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009812 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009813 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9814 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9815 free(errmsg);
9816 goto out_err;
9817 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009818 } else {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02009819 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 +02009820 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9821 goto out_err;
9822 }
9823
9824 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9825 struct acl_cond *cond;
9826 char *errmsg = NULL;
9827
9828 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9829 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9830 file, linenum, args[0], errmsg);
9831 free(errmsg);
9832 goto out_err;
9833 }
9834 rule->cond = cond;
9835 }
9836 else if (*args[cur_arg]) {
9837 Alert("parsing [%s:%d]: 'http-response %s' expects"
9838 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9839 file, linenum, args[0], args[cur_arg]);
9840 goto out_err;
9841 }
9842
9843 return rule;
9844 out_err:
9845 free(rule);
9846 return NULL;
9847}
9848
Willy Tarreau4baae242012-12-27 12:00:31 +01009849/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009850 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009851 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9852 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009853 */
9854struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009855 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009856{
9857 struct redirect_rule *rule;
9858 int cur_arg;
9859 int type = REDIRECT_TYPE_NONE;
9860 int code = 302;
9861 const char *destination = NULL;
9862 const char *cookie = NULL;
9863 int cookie_set = 0;
9864 unsigned int flags = REDIRECT_FLAG_NONE;
9865 struct acl_cond *cond = NULL;
9866
9867 cur_arg = 0;
9868 while (*(args[cur_arg])) {
9869 if (strcmp(args[cur_arg], "location") == 0) {
9870 if (!*args[cur_arg + 1])
9871 goto missing_arg;
9872
9873 type = REDIRECT_TYPE_LOCATION;
9874 cur_arg++;
9875 destination = args[cur_arg];
9876 }
9877 else if (strcmp(args[cur_arg], "prefix") == 0) {
9878 if (!*args[cur_arg + 1])
9879 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009880 type = REDIRECT_TYPE_PREFIX;
9881 cur_arg++;
9882 destination = args[cur_arg];
9883 }
9884 else if (strcmp(args[cur_arg], "scheme") == 0) {
9885 if (!*args[cur_arg + 1])
9886 goto missing_arg;
9887
9888 type = REDIRECT_TYPE_SCHEME;
9889 cur_arg++;
9890 destination = args[cur_arg];
9891 }
9892 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9893 if (!*args[cur_arg + 1])
9894 goto missing_arg;
9895
9896 cur_arg++;
9897 cookie = args[cur_arg];
9898 cookie_set = 1;
9899 }
9900 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9901 if (!*args[cur_arg + 1])
9902 goto missing_arg;
9903
9904 cur_arg++;
9905 cookie = args[cur_arg];
9906 cookie_set = 0;
9907 }
9908 else if (strcmp(args[cur_arg], "code") == 0) {
9909 if (!*args[cur_arg + 1])
9910 goto missing_arg;
9911
9912 cur_arg++;
9913 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009914 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009915 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009916 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009917 args[cur_arg - 1], args[cur_arg]);
9918 return NULL;
9919 }
9920 }
9921 else if (!strcmp(args[cur_arg],"drop-query")) {
9922 flags |= REDIRECT_FLAG_DROP_QS;
9923 }
9924 else if (!strcmp(args[cur_arg],"append-slash")) {
9925 flags |= REDIRECT_FLAG_APPEND_SLASH;
9926 }
9927 else if (strcmp(args[cur_arg], "if") == 0 ||
9928 strcmp(args[cur_arg], "unless") == 0) {
9929 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9930 if (!cond) {
9931 memprintf(errmsg, "error in condition: %s", *errmsg);
9932 return NULL;
9933 }
9934 break;
9935 }
9936 else {
9937 memprintf(errmsg,
9938 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9939 args[cur_arg]);
9940 return NULL;
9941 }
9942 cur_arg++;
9943 }
9944
9945 if (type == REDIRECT_TYPE_NONE) {
9946 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9947 return NULL;
9948 }
9949
Willy Tarreaube4653b2015-05-28 15:26:58 +02009950 if (dir && type != REDIRECT_TYPE_LOCATION) {
9951 memprintf(errmsg, "response only supports redirect type 'location'");
9952 return NULL;
9953 }
9954
Willy Tarreau4baae242012-12-27 12:00:31 +01009955 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9956 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009957 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009958
9959 if (!use_fmt) {
9960 /* old-style static redirect rule */
9961 rule->rdr_str = strdup(destination);
9962 rule->rdr_len = strlen(destination);
9963 }
9964 else {
9965 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009966
9967 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9968 * if prefix == "/", we don't want to add anything, otherwise it
9969 * makes it hard for the user to configure a self-redirection.
9970 */
Godbachd9722032014-12-18 15:44:58 +08009971 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009972 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009973 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009974 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
9975 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009976 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009977 free(curproxy->conf.lfs_file);
9978 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9979 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009980 }
9981 }
9982
Willy Tarreau4baae242012-12-27 12:00:31 +01009983 if (cookie) {
9984 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9985 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9986 */
9987 rule->cookie_len = strlen(cookie);
9988 if (cookie_set) {
9989 rule->cookie_str = malloc(rule->cookie_len + 10);
9990 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9991 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9992 rule->cookie_len += 9;
9993 } else {
9994 rule->cookie_str = malloc(rule->cookie_len + 21);
9995 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9996 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9997 rule->cookie_len += 20;
9998 }
9999 }
10000 rule->type = type;
10001 rule->code = code;
10002 rule->flags = flags;
10003 LIST_INIT(&rule->list);
10004 return rule;
10005
10006 missing_arg:
10007 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10008 return NULL;
10009}
10010
Willy Tarreau8797c062007-05-07 00:55:35 +020010011/************************************************************************/
10012/* The code below is dedicated to ACL parsing and matching */
10013/************************************************************************/
10014
10015
Willy Tarreau14174bc2012-04-16 14:34:04 +020010016/* This function ensures that the prerequisites for an L7 fetch are ready,
10017 * which means that a request or response is ready. If some data is missing,
10018 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010019 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10020 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010021 *
10022 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010023 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10024 * decide whether or not an HTTP message is present ;
10025 * 0 if the requested data cannot be fetched or if it is certain that
10026 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010027 * 1 if an HTTP message is ready
10028 */
10029static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010030smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010031 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010032{
Willy Tarreau192252e2015-04-04 01:47:55 +020010033 struct http_txn *txn;
10034 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010035
Willy Tarreaueee5b512015-04-03 23:46:31 +020010036 /* Note: this function may only be used from places where
10037 * http_init_txn() has already been done, and implies that <s>,
10038 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
10039 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010040 */
10041
Willy Tarreau192252e2015-04-04 01:47:55 +020010042 if (!s)
10043 return 0;
10044 txn = s->txn;
10045
10046 if (!txn)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010047 return 0;
Willy Tarreau192252e2015-04-04 01:47:55 +020010048 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010049
10050 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010051 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010052
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010053 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010054 /* If the buffer does not leave enough free space at the end,
10055 * we must first realign it.
10056 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010057 if (s->req.buf->p > s->req.buf->data &&
10058 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10059 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010060
Willy Tarreau14174bc2012-04-16 14:34:04 +020010061 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010062 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010063 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010064
10065 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010066 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010067 http_msg_analyzer(msg, &txn->hdr_idx);
10068
10069 /* Still no valid request ? */
10070 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010071 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010072 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010073 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010074 }
10075 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010076 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010077 return 0;
10078 }
10079
10080 /* OK we just got a valid HTTP request. We have some minor
10081 * preparation to perform so that further checks can rely
10082 * on HTTP tests.
10083 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010084
10085 /* If the request was parsed but was too large, we must absolutely
10086 * return an error so that it is not processed. At the moment this
10087 * cannot happen, but if the parsers are to change in the future,
10088 * we want this check to be maintained.
10089 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010090 if (unlikely(s->req.buf->i + s->req.buf->p >
10091 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010092 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010093 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010094 return 1;
10095 }
10096
Willy Tarreau9b28e032012-10-12 23:49:43 +020010097 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010098 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010099 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010100
Willy Tarreau506d0502013-07-06 13:29:24 +020010101 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10102 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010103 }
10104
Willy Tarreau506d0502013-07-06 13:29:24 +020010105 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010106 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010107 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010108
10109 /* otherwise everything's ready for the request */
10110 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010111 else {
10112 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010113 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10114 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010115 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010116 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010117 }
10118
10119 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010120 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010121 return 1;
10122}
Willy Tarreau8797c062007-05-07 00:55:35 +020010123
Willy Tarreau6c616e02014-06-25 16:56:41 +020010124/* Note: these functinos *do* modify the sample. Even in case of success, at
10125 * least the type and uint value are modified.
10126 */
Willy Tarreauc0239e02012-04-16 14:42:55 +020010127#define CHECK_HTTP_MESSAGE_FIRST() \
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010128 do { int r = smp_prefetch_http(smp->px, smp->strm, smp->opt, args, smp, 1); if (r <= 0) return r; } while (0)
Willy Tarreauc0239e02012-04-16 14:42:55 +020010129
Willy Tarreau24e32d82012-04-23 23:55:44 +020010130#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010131 do { int r = smp_prefetch_http(smp->px, smp->strm, smp->opt, args, smp, 0); if (r <= 0) return r; } while (0)
Willy Tarreau24e32d82012-04-23 23:55:44 +020010132
Willy Tarreau8797c062007-05-07 00:55:35 +020010133
10134/* 1. Check on METHOD
10135 * We use the pre-parsed method if it is known, and store its number as an
10136 * integer. If it is unknown, we use the pointer and the length.
10137 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010138static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010139{
10140 int len, meth;
10141
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010142 len = strlen(text);
10143 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010144
10145 pattern->val.i = meth;
10146 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010147 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010148 pattern->len = len;
10149 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010150 else {
10151 pattern->ptr.str = NULL;
10152 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010153 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010154 return 1;
10155}
10156
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010157/* This function fetches the method of current HTTP request and stores
10158 * it in the global pattern struct as a chunk. There are two possibilities :
10159 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10160 * in <len> and <ptr> is NULL ;
10161 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10162 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010163 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010164 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010165static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010166smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010167{
10168 int meth;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010169 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010170
Willy Tarreau24e32d82012-04-23 23:55:44 +020010171 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010172
Willy Tarreau8797c062007-05-07 00:55:35 +020010173 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010174 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010175 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010176 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010177 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10178 /* ensure the indexes are not affected */
10179 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010180 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010181 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10182 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010183 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010184 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010185 return 1;
10186}
10187
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010188/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010189static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010190{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010191 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010192 struct pattern_list *lst;
10193 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010194
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010195 list_for_each_entry(lst, &expr->patterns, list) {
10196 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010197
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010198 /* well-known method */
10199 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010200 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010201 return pattern;
10202 else
10203 continue;
10204 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010205
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010206 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010207 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010208 continue;
10209
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010210 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010211 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10212 (!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 +010010213 return pattern;
10214 }
10215 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010216}
10217
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010218static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010219smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010220{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010221 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010222 char *ptr;
10223 int len;
10224
Willy Tarreauc0239e02012-04-16 14:42:55 +020010225 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010226
Willy Tarreau8797c062007-05-07 00:55:35 +020010227 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010228 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010229
10230 while ((len-- > 0) && (*ptr++ != '/'));
10231 if (len <= 0)
10232 return 0;
10233
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010234 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010235 smp->data.u.str.str = ptr;
10236 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010237
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010238 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010239 return 1;
10240}
10241
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010242static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010243smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010244{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010245 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010246 char *ptr;
10247 int len;
10248
Willy Tarreauc0239e02012-04-16 14:42:55 +020010249 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010250
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010251 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010252 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10253 return 0;
10254
Willy Tarreau8797c062007-05-07 00:55:35 +020010255 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010256 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010257
10258 while ((len-- > 0) && (*ptr++ != '/'));
10259 if (len <= 0)
10260 return 0;
10261
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010262 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010263 smp->data.u.str.str = ptr;
10264 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010265
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010266 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010267 return 1;
10268}
10269
10270/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010271static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010272smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010273{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010274 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010275 char *ptr;
10276 int len;
10277
Willy Tarreauc0239e02012-04-16 14:42:55 +020010278 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010279
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010280 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010281 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10282 return 0;
10283
Willy Tarreau8797c062007-05-07 00:55:35 +020010284 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010285 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010286
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010287 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010288 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010289 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010290 return 1;
10291}
10292
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010293/* returns the longest available part of the body. This requires that the body
10294 * has been waited for using http-buffer-request.
10295 */
10296static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010297smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010298{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010299 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010300 struct http_msg *msg;
10301 unsigned long len;
10302 unsigned long block1;
10303 char *body;
10304 struct chunk *temp;
10305
10306 CHECK_HTTP_MESSAGE_FIRST();
10307
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010308 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010309 msg = &txn->req;
10310 else
10311 msg = &txn->rsp;
10312
10313 len = http_body_bytes(msg);
10314 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10315
10316 block1 = len;
10317 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10318 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10319
10320 if (block1 == len) {
10321 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010322 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010323 smp->data.u.str.str = body;
10324 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010325 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10326 }
10327 else {
10328 /* buffer is wrapped, we need to defragment it */
10329 temp = get_trash_chunk();
10330 memcpy(temp->str, body, block1);
10331 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010332 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010333 smp->data.u.str.str = temp->str;
10334 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010335 smp->flags = SMP_F_VOL_TEST;
10336 }
10337 return 1;
10338}
10339
10340
10341/* returns the available length of the body. This requires that the body
10342 * has been waited for using http-buffer-request.
10343 */
10344static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010345smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010346{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010347 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010348 struct http_msg *msg;
10349
10350 CHECK_HTTP_MESSAGE_FIRST();
10351
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010352 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010353 msg = &txn->req;
10354 else
10355 msg = &txn->rsp;
10356
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010357 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010358 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010359
10360 smp->flags = SMP_F_VOL_TEST;
10361 return 1;
10362}
10363
10364
10365/* returns the advertised length of the body, or the advertised size of the
10366 * chunks available in the buffer. This requires that the body has been waited
10367 * for using http-buffer-request.
10368 */
10369static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010370smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010371{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010372 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010373 struct http_msg *msg;
10374
10375 CHECK_HTTP_MESSAGE_FIRST();
10376
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010377 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010378 msg = &txn->req;
10379 else
10380 msg = &txn->rsp;
10381
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010382 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010383 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010384
10385 smp->flags = SMP_F_VOL_TEST;
10386 return 1;
10387}
10388
10389
Willy Tarreau8797c062007-05-07 00:55:35 +020010390/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010391static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010392smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010393{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010394 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010395
Willy Tarreauc0239e02012-04-16 14:42:55 +020010396 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010397
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010398 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010399 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010400 smp->data.u.str.len = txn->req.sl.rq.u_l;
10401 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010402 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010403 return 1;
10404}
10405
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010406static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010407smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010408{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010409 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010410 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010411
Willy Tarreauc0239e02012-04-16 14:42:55 +020010412 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010413
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010414 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010415 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010416 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010417 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010418
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010419 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010420 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010421 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010422 return 1;
10423}
10424
10425static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010426smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010427{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010428 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010429 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010430
Willy Tarreauc0239e02012-04-16 14:42:55 +020010431 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010432
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010433 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010434 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 +020010435 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10436 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010437
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010438 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010439 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010440 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010441 return 1;
10442}
10443
Willy Tarreau185b5c42012-04-26 15:11:51 +020010444/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10445 * Accepts an optional argument of type string containing the header field name,
10446 * and an optional argument of type signed or unsigned integer to request an
10447 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010448 * headers are considered from the first one. It does not stop on commas and
10449 * returns full lines instead (useful for User-Agent or Date for example).
10450 */
10451static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010452smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010453{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010454 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010455 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010456 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010457 int occ = 0;
10458 const char *name_str = NULL;
10459 int name_len = 0;
10460
10461 if (!ctx) {
10462 /* first call */
10463 ctx = &static_hdr_ctx;
10464 ctx->idx = 0;
10465 smp->ctx.a[0] = ctx;
10466 }
10467
10468 if (args) {
10469 if (args[0].type != ARGT_STR)
10470 return 0;
10471 name_str = args[0].data.str.str;
10472 name_len = args[0].data.str.len;
10473
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010474 if (args[1].type == ARGT_SINT)
10475 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010476 }
10477
10478 CHECK_HTTP_MESSAGE_FIRST();
10479
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010480 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010481 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 +020010482
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010483 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10484 /* search for header from the beginning */
10485 ctx->idx = 0;
10486
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010487 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010488 /* no explicit occurrence and single fetch => last header by default */
10489 occ = -1;
10490
10491 if (!occ)
10492 /* prepare to report multiple occurrences for ACL fetches */
10493 smp->flags |= SMP_F_NOT_LAST;
10494
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010495 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010496 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010497 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 +020010498 return 1;
10499
10500 smp->flags &= ~SMP_F_NOT_LAST;
10501 return 0;
10502}
10503
10504/* 6. Check on HTTP header count. The number of occurrences is returned.
10505 * Accepts exactly 1 argument of type string. It does not stop on commas and
10506 * returns full lines instead (useful for User-Agent or Date for example).
10507 */
10508static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010509smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010510{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010511 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010512 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010513 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010514 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010515 const char *name = NULL;
10516 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010517
Willy Tarreau601a4d12015-04-01 19:16:09 +020010518 if (args && args->type == ARGT_STR) {
10519 name = args->data.str.str;
10520 len = args->data.str.len;
10521 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010522
10523 CHECK_HTTP_MESSAGE_FIRST();
10524
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010525 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010526 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 +020010527
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010528 ctx.idx = 0;
10529 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010530 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010531 cnt++;
10532
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010533 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010534 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010535 smp->flags = SMP_F_VOL_HDR;
10536 return 1;
10537}
10538
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010539static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010540smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010541{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010542 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010543 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010544 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010545 struct chunk *temp;
10546 char del = ',';
10547
10548 if (args && args->type == ARGT_STR)
10549 del = *args[0].data.str.str;
10550
10551 CHECK_HTTP_MESSAGE_FIRST();
10552
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010553 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010554 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 +020010555
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010556 temp = get_trash_chunk();
10557
10558 ctx.idx = 0;
10559 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10560 if (temp->len)
10561 temp->str[temp->len++] = del;
10562 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10563 temp->len += ctx.del;
10564 }
10565
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010566 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010567 smp->data.u.str.str = temp->str;
10568 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010569 smp->flags = SMP_F_VOL_HDR;
10570 return 1;
10571}
10572
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010573/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10574 * Accepts an optional argument of type string containing the header field name,
10575 * and an optional argument of type signed or unsigned integer to request an
10576 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010577 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010578 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010579static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010580smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010581{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010582 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010583 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010584 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010585 int occ = 0;
10586 const char *name_str = NULL;
10587 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010588
Willy Tarreaua890d072013-04-02 12:01:06 +020010589 if (!ctx) {
10590 /* first call */
10591 ctx = &static_hdr_ctx;
10592 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010593 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010594 }
10595
Willy Tarreau185b5c42012-04-26 15:11:51 +020010596 if (args) {
10597 if (args[0].type != ARGT_STR)
10598 return 0;
10599 name_str = args[0].data.str.str;
10600 name_len = args[0].data.str.len;
10601
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010602 if (args[1].type == ARGT_SINT)
10603 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010604 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010605
Willy Tarreaue333ec92012-04-16 16:26:40 +020010606 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010607
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010608 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010609 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 +020010610
Willy Tarreau185b5c42012-04-26 15:11:51 +020010611 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010612 /* search for header from the beginning */
10613 ctx->idx = 0;
10614
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010615 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010616 /* no explicit occurrence and single fetch => last header by default */
10617 occ = -1;
10618
10619 if (!occ)
10620 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010621 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010622
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010623 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010624 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010625 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 +020010626 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010627
Willy Tarreau37406352012-04-23 16:16:37 +020010628 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010629 return 0;
10630}
10631
Willy Tarreauc11416f2007-06-17 16:58:38 +020010632/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010633 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010634 */
10635static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010636smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010637{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010638 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010639 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010640 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010641 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010642 const char *name = NULL;
10643 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010644
Willy Tarreau601a4d12015-04-01 19:16:09 +020010645 if (args && args->type == ARGT_STR) {
10646 name = args->data.str.str;
10647 len = args->data.str.len;
10648 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010649
Willy Tarreaue333ec92012-04-16 16:26:40 +020010650 CHECK_HTTP_MESSAGE_FIRST();
10651
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010652 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010653 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 +020010654
Willy Tarreau33a7e692007-06-10 19:45:56 +020010655 ctx.idx = 0;
10656 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010657 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010658 cnt++;
10659
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010660 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010661 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010662 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010663 return 1;
10664}
10665
Willy Tarreau185b5c42012-04-26 15:11:51 +020010666/* Fetch an HTTP header's integer value. The integer value is returned. It
10667 * takes a mandatory argument of type string and an optional one of type int
10668 * to designate a specific occurrence. It returns an unsigned integer, which
10669 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010670 */
10671static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010672smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010673{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010674 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010675
Willy Tarreauf853c462012-04-23 18:53:56 +020010676 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010677 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010678 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010679 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010680
Willy Tarreaud53e2422012-04-16 17:21:11 +020010681 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010682}
10683
Cyril Bonté69fa9922012-10-25 00:01:06 +020010684/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10685 * and an optional one of type int to designate a specific occurrence.
10686 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010687 */
10688static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010689smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010690{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010691 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010692
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010693 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010694 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010695 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010696 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010697 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010698 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010699 if (smp->data.u.str.len < temp->size - 1) {
10700 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10701 temp->str[smp->data.u.str.len] = '\0';
10702 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010703 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010704 break;
10705 }
10706 }
10707 }
10708
Willy Tarreaud53e2422012-04-16 17:21:11 +020010709 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010710 if (!(smp->flags & SMP_F_NOT_LAST))
10711 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010712 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010713 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010714}
10715
Willy Tarreau737b0c12007-06-10 21:28:46 +020010716/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10717 * the first '/' after the possible hostname, and ends before the possible '?'.
10718 */
10719static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010720smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010721{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010722 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010723 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010724
Willy Tarreauc0239e02012-04-16 14:42:55 +020010725 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010726
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010727 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010728 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010729 ptr = http_get_path(txn);
10730 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010731 return 0;
10732
10733 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010734 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010735 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010736
10737 while (ptr < end && *ptr != '?')
10738 ptr++;
10739
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010740 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010741 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010742 return 1;
10743}
10744
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010745/* This produces a concatenation of the first occurrence of the Host header
10746 * followed by the path component if it begins with a slash ('/'). This means
10747 * that '*' will not be added, resulting in exactly the first Host entry.
10748 * If no Host header is found, then the path is returned as-is. The returned
10749 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010750 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010751 */
10752static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010753smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010754{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010755 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010756 char *ptr, *end, *beg;
10757 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010758 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010759
10760 CHECK_HTTP_MESSAGE_FIRST();
10761
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010762 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010763 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010764 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010765 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010766
10767 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010768 temp = get_trash_chunk();
10769 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010770 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010771 smp->data.u.str.str = temp->str;
10772 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010773
10774 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010775 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010776 beg = http_get_path(txn);
10777 if (!beg)
10778 beg = end;
10779
10780 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10781
10782 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010783 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10784 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010785 }
10786
10787 smp->flags = SMP_F_VOL_1ST;
10788 return 1;
10789}
10790
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010791/* This produces a 32-bit hash of the concatenation of the first occurrence of
10792 * the Host header followed by the path component if it begins with a slash ('/').
10793 * This means that '*' will not be added, resulting in exactly the first Host
10794 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010795 * is hashed using the path hash followed by a full avalanche hash and provides a
10796 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010797 * high-traffic sites without having to store whole paths.
10798 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010799int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010800smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010801{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010802 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010803 struct hdr_ctx ctx;
10804 unsigned int hash = 0;
10805 char *ptr, *beg, *end;
10806 int len;
10807
10808 CHECK_HTTP_MESSAGE_FIRST();
10809
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010810 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010811 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010812 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010813 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10814 ptr = ctx.line + ctx.val;
10815 len = ctx.vlen;
10816 while (len--)
10817 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10818 }
10819
10820 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010821 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010822 beg = http_get_path(txn);
10823 if (!beg)
10824 beg = end;
10825
10826 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10827
10828 if (beg < ptr && *beg == '/') {
10829 while (beg < ptr)
10830 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10831 }
10832 hash = full_hash(hash);
10833
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010834 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010835 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010836 smp->flags = SMP_F_VOL_1ST;
10837 return 1;
10838}
10839
Willy Tarreau4a550602012-12-09 14:53:32 +010010840/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010841 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10842 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10843 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010844 * that in environments where IPv6 is insignificant, truncating the output to
10845 * 8 bytes would still work.
10846 */
10847static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010848smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010849{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010850 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010851 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010852
10853 if (!cli_conn)
10854 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010855
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010856 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010857 return 0;
10858
Willy Tarreau47ca5452012-12-23 20:22:19 +010010859 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010860 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010861 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010862
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010863 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010864 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010865 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010866 temp->len += 4;
10867 break;
10868 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010869 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010870 temp->len += 16;
10871 break;
10872 default:
10873 return 0;
10874 }
10875
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010876 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010877 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010878 return 1;
10879}
10880
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010881/* Extracts the query string, which comes after the question mark '?'. If no
10882 * question mark is found, nothing is returned. Otherwise it returns a sample
10883 * of type string carrying the whole query string.
10884 */
10885static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010886smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010887{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010888 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010889 char *ptr, *end;
10890
10891 CHECK_HTTP_MESSAGE_FIRST();
10892
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010893 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010894 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10895 end = ptr + txn->req.sl.rq.u_l;
10896
10897 /* look up the '?' */
10898 do {
10899 if (ptr == end)
10900 return 0;
10901 } while (*ptr++ != '?');
10902
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010903 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010904 smp->data.u.str.str = ptr;
10905 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010906 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10907 return 1;
10908}
10909
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010910static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010911smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010912{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010913 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10914 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10915 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010916
Willy Tarreau24e32d82012-04-23 23:55:44 +020010917 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010918
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010919 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010920 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010921 return 1;
10922}
10923
Willy Tarreau7f18e522010-10-22 20:04:13 +020010924/* return a valid test if the current request is the first one on the connection */
10925static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010926smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010927{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010928 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010929 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010930 return 1;
10931}
10932
Willy Tarreau34db1082012-04-19 17:16:54 +020010933/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010934static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010935smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010936{
10937
Willy Tarreau24e32d82012-04-23 23:55:44 +020010938 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010939 return 0;
10940
Willy Tarreauc0239e02012-04-16 14:42:55 +020010941 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010942
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010943 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010944 return 0;
10945
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010946 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010947 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010948 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010949 return 1;
10950}
Willy Tarreau8797c062007-05-07 00:55:35 +020010951
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010952/* Accepts exactly 1 argument of type userlist */
10953static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010954smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010955{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010956 if (!args || args->type != ARGT_USR)
10957 return 0;
10958
10959 CHECK_HTTP_MESSAGE_FIRST();
10960
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010961 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010962 return 0;
10963
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010964 /* if the user does not belong to the userlist or has a wrong password,
10965 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010966 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010967 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010968 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10969 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010970 return 0;
10971
10972 /* pat_match_auth() will need the user list */
10973 smp->ctx.a[0] = args->data.usr;
10974
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010975 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010976 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010977 smp->data.u.str.str = smp->strm->txn->auth.user;
10978 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010979
10980 return 1;
10981}
10982
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010983/* Try to find the next occurrence of a cookie name in a cookie header value.
10984 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10985 * the cookie value is returned into *value and *value_l, and the function
10986 * returns a pointer to the next pointer to search from if the value was found.
10987 * Otherwise if the cookie was not found, NULL is returned and neither value
10988 * nor value_l are touched. The input <hdr> string should first point to the
10989 * header's value, and the <hdr_end> pointer must point to the first character
10990 * not part of the value. <list> must be non-zero if value may represent a list
10991 * of values (cookie headers). This makes it faster to abort parsing when no
10992 * list is expected.
10993 */
10994static char *
10995extract_cookie_value(char *hdr, const char *hdr_end,
10996 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010997 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010998{
10999 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11000 char *next;
11001
11002 /* we search at least a cookie name followed by an equal, and more
11003 * generally something like this :
11004 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11005 */
11006 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11007 /* Iterate through all cookies on this line */
11008
11009 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
11010 att_beg++;
11011
11012 /* find att_end : this is the first character after the last non
11013 * space before the equal. It may be equal to hdr_end.
11014 */
11015 equal = att_end = att_beg;
11016
11017 while (equal < hdr_end) {
11018 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11019 break;
11020 if (http_is_spht[(unsigned char)*equal++])
11021 continue;
11022 att_end = equal;
11023 }
11024
11025 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11026 * is between <att_beg> and <equal>, both may be identical.
11027 */
11028
11029 /* look for end of cookie if there is an equal sign */
11030 if (equal < hdr_end && *equal == '=') {
11031 /* look for the beginning of the value */
11032 val_beg = equal + 1;
11033 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
11034 val_beg++;
11035
11036 /* find the end of the value, respecting quotes */
11037 next = find_cookie_value_end(val_beg, hdr_end);
11038
11039 /* make val_end point to the first white space or delimitor after the value */
11040 val_end = next;
11041 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
11042 val_end--;
11043 } else {
11044 val_beg = val_end = next = equal;
11045 }
11046
11047 /* We have nothing to do with attributes beginning with '$'. However,
11048 * they will automatically be removed if a header before them is removed,
11049 * since they're supposed to be linked together.
11050 */
11051 if (*att_beg == '$')
11052 continue;
11053
11054 /* Ignore cookies with no equal sign */
11055 if (equal == next)
11056 continue;
11057
11058 /* Now we have the cookie name between att_beg and att_end, and
11059 * its value between val_beg and val_end.
11060 */
11061
11062 if (att_end - att_beg == cookie_name_l &&
11063 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11064 /* let's return this value and indicate where to go on from */
11065 *value = val_beg;
11066 *value_l = val_end - val_beg;
11067 return next + 1;
11068 }
11069
11070 /* Set-Cookie headers only have the name in the first attr=value part */
11071 if (!list)
11072 break;
11073 }
11074
11075 return NULL;
11076}
11077
William Lallemanda43ba4e2014-01-28 18:14:25 +010011078/* Fetch a captured HTTP request header. The index is the position of
11079 * the "capture" option in the configuration file
11080 */
11081static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011082smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011083{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011084 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011085 int idx;
11086
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011087 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011088 return 0;
11089
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011090 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011091
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011092 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 +010011093 return 0;
11094
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011095 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011096 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011097 smp->data.u.str.str = smp->strm->req_cap[idx];
11098 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011099
11100 return 1;
11101}
11102
11103/* Fetch a captured HTTP response header. The index is the position of
11104 * the "capture" option in the configuration file
11105 */
11106static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011107smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011108{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011109 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011110 int idx;
11111
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011112 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011113 return 0;
11114
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011115 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011116
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011117 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 +010011118 return 0;
11119
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011120 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011121 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011122 smp->data.u.str.str = smp->strm->res_cap[idx];
11123 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011124
11125 return 1;
11126}
11127
William Lallemand65ad6e12014-01-31 15:08:02 +010011128/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11129static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011130smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011131{
11132 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011133 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011134 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011135
Willy Tarreau15e91e12015-04-04 00:52:09 +020011136 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011137 return 0;
11138
William Lallemand96a77852014-02-05 00:30:02 +010011139 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011140
William Lallemand96a77852014-02-05 00:30:02 +010011141 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11142 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011143
William Lallemand96a77852014-02-05 00:30:02 +010011144 temp = get_trash_chunk();
11145 temp->str = txn->uri;
11146 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011147 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011148 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011149 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011150
11151 return 1;
11152
11153}
11154
11155/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11156static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011157smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011158{
11159 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011160 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011161 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011162
Willy Tarreau15e91e12015-04-04 00:52:09 +020011163 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011164 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011165
William Lallemand65ad6e12014-01-31 15:08:02 +010011166 ptr = txn->uri;
11167
11168 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11169 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011170
William Lallemand65ad6e12014-01-31 15:08:02 +010011171 if (!*ptr)
11172 return 0;
11173
11174 ptr++; /* skip the space */
11175
11176 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011177 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011178 if (!ptr)
11179 return 0;
11180 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11181 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011182
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011183 smp->data.u.str = *temp;
11184 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011185 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011186 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011187
11188 return 1;
11189}
11190
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011191/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11192 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11193 */
11194static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011195smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011196{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011197 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011198
Willy Tarreau15e91e12015-04-04 00:52:09 +020011199 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011200 return 0;
11201
11202 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011203 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011204 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011205 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011206
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011207 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011208 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011209 smp->flags = SMP_F_CONST;
11210 return 1;
11211
11212}
11213
11214/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11215 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11216 */
11217static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011218smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011219{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011220 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011221
Willy Tarreau15e91e12015-04-04 00:52:09 +020011222 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011223 return 0;
11224
11225 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011226 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011227 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011228 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011229
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011230 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011231 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011232 smp->flags = SMP_F_CONST;
11233 return 1;
11234
11235}
11236
William Lallemand65ad6e12014-01-31 15:08:02 +010011237
Willy Tarreaue333ec92012-04-16 16:26:40 +020011238/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011239 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011240 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011241 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011242 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011243 * Accepts exactly 1 argument of type string. If the input options indicate
11244 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011245 * The returned sample is of type CSTR. Can be used to parse cookies in other
11246 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011247 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011248int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011249{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011250 struct http_txn *txn;
11251 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011252 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011253 const struct http_msg *msg;
11254 const char *hdr_name;
11255 int hdr_name_len;
11256 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011257 int occ = 0;
11258 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011259
Willy Tarreau24e32d82012-04-23 23:55:44 +020011260 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011261 return 0;
11262
Willy Tarreaua890d072013-04-02 12:01:06 +020011263 if (!ctx) {
11264 /* first call */
11265 ctx = &static_hdr_ctx;
11266 ctx->idx = 0;
11267 smp->ctx.a[2] = ctx;
11268 }
11269
Willy Tarreaue333ec92012-04-16 16:26:40 +020011270 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011271
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011272 txn = smp->strm->txn;
11273 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011274
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011275 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011276 msg = &txn->req;
11277 hdr_name = "Cookie";
11278 hdr_name_len = 6;
11279 } else {
11280 msg = &txn->rsp;
11281 hdr_name = "Set-Cookie";
11282 hdr_name_len = 10;
11283 }
11284
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011285 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011286 /* no explicit occurrence and single fetch => last cookie by default */
11287 occ = -1;
11288
11289 /* OK so basically here, either we want only one value and it's the
11290 * last one, or we want to iterate over all of them and we fetch the
11291 * next one.
11292 */
11293
Willy Tarreau9b28e032012-10-12 23:49:43 +020011294 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011295 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011296 /* search for the header from the beginning, we must first initialize
11297 * the search parameters.
11298 */
Willy Tarreau37406352012-04-23 16:16:37 +020011299 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011300 ctx->idx = 0;
11301 }
11302
Willy Tarreau28376d62012-04-26 21:26:10 +020011303 smp->flags |= SMP_F_VOL_HDR;
11304
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011305 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011306 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11307 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011308 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11309 goto out;
11310
Willy Tarreau24e32d82012-04-23 23:55:44 +020011311 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011312 continue;
11313
Willy Tarreau37406352012-04-23 16:16:37 +020011314 smp->ctx.a[0] = ctx->line + ctx->val;
11315 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011316 }
11317
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011318 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011319 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011320 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011321 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011322 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011323 &smp->data.u.str.str,
11324 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011325 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011326 found = 1;
11327 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011328 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011329 smp->flags |= SMP_F_NOT_LAST;
11330 return 1;
11331 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011332 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011333 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011334 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011335 /* all cookie headers and values were scanned. If we're looking for the
11336 * last occurrence, we may return it now.
11337 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011338 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011339 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011340 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011341}
11342
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011343/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011344 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011345 * multiple cookies may be parsed on the same line. The returned sample is of
11346 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011347 */
11348static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011349smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011350{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011351 struct http_txn *txn;
11352 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011353 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011354 const struct http_msg *msg;
11355 const char *hdr_name;
11356 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011357 int cnt;
11358 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011359 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011360
Willy Tarreau24e32d82012-04-23 23:55:44 +020011361 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011362 return 0;
11363
Willy Tarreaue333ec92012-04-16 16:26:40 +020011364 CHECK_HTTP_MESSAGE_FIRST();
11365
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011366 txn = smp->strm->txn;
11367 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011368
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011369 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011370 msg = &txn->req;
11371 hdr_name = "Cookie";
11372 hdr_name_len = 6;
11373 } else {
11374 msg = &txn->rsp;
11375 hdr_name = "Set-Cookie";
11376 hdr_name_len = 10;
11377 }
11378
Willy Tarreau9b28e032012-10-12 23:49:43 +020011379 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011380 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011381 ctx.idx = 0;
11382 cnt = 0;
11383
11384 while (1) {
11385 /* Note: val_beg == NULL every time we need to fetch a new header */
11386 if (!val_beg) {
11387 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11388 break;
11389
Willy Tarreau24e32d82012-04-23 23:55:44 +020011390 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011391 continue;
11392
11393 val_beg = ctx.line + ctx.val;
11394 val_end = val_beg + ctx.vlen;
11395 }
11396
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011397 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011398 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011399 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011400 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011401 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011402 &smp->data.u.str.str,
11403 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011404 cnt++;
11405 }
11406 }
11407
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011408 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011409 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011410 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011411 return 1;
11412}
11413
Willy Tarreau51539362012-05-08 12:46:28 +020011414/* Fetch an cookie's integer value. The integer value is returned. It
11415 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11416 */
11417static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011418smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011419{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011420 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011421
11422 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011423 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011424 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011425 }
11426
11427 return ret;
11428}
11429
Willy Tarreau8797c062007-05-07 00:55:35 +020011430/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011431/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011432/************************************************************************/
11433
David Cournapeau16023ee2010-12-23 20:55:41 +090011434/*
11435 * Given a path string and its length, find the position of beginning of the
11436 * query string. Returns NULL if no query string is found in the path.
11437 *
11438 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11439 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011440 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011441 */
bedis4c75cca2012-10-05 08:38:24 +020011442static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011443{
11444 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011445
bedis4c75cca2012-10-05 08:38:24 +020011446 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011447 return p ? p + 1 : NULL;
11448}
11449
bedis4c75cca2012-10-05 08:38:24 +020011450static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011451{
bedis4c75cca2012-10-05 08:38:24 +020011452 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011453}
11454
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011455/* after increasing a pointer value, it can exceed the first buffer
11456 * size. This function transform the value of <ptr> according with
11457 * the expected position. <chunks> is an array of the one or two
11458 * avalaible chunks. The first value is the start of the first chunk,
11459 * the second value if the end+1 of the first chunks. The third value
11460 * is NULL or the start of the second chunk and the fourth value is
11461 * the end+1 of the second chunk. The function returns 1 if does a
11462 * wrap, else returns 0.
11463 */
11464static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11465{
11466 if (*ptr < chunks[1])
11467 return 0;
11468 if (!chunks[2])
11469 return 0;
11470 *ptr = chunks[2] + ( *ptr - chunks[1] );
11471 return 1;
11472}
11473
David Cournapeau16023ee2010-12-23 20:55:41 +090011474/*
11475 * Given a url parameter, find the starting position of the first occurence,
11476 * or NULL if the parameter is not found.
11477 *
11478 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11479 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011480 *
11481 * Warning:this function returns a pointer that can be point to the first chunk
11482 * or the second chunk. The caller must be check the position before using the
11483 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011484 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011485static const char *
11486find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011487 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011488 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011489{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011490 const char *pos, *last, *equal;
11491 const char **bufs = chunks;
11492 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011493
David Cournapeau16023ee2010-12-23 20:55:41 +090011494
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011495 pos = bufs[0];
11496 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011497 while (pos <= last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011498 /* Check the equal. */
11499 equal = pos + url_param_name_l;
11500 if (fix_pointer_if_wrap(chunks, &equal)) {
11501 if (equal >= chunks[3])
11502 return NULL;
11503 } else {
11504 if (equal >= chunks[1])
11505 return NULL;
11506 }
11507 if (*equal == '=') {
11508 if (pos + url_param_name_l > last) {
11509 /* process wrap case, we detect a wrap. In this case, the
11510 * comparison is performed in two parts.
11511 */
11512
11513 /* This is the end, we dont have any other chunk. */
11514 if (bufs != chunks || !bufs[2])
11515 return NULL;
11516
11517 /* Compute the length of each part of the comparison. */
11518 l1 = last - pos;
11519 l2 = url_param_name_l - l1;
11520
11521 /* The second buffer is too short to contain the compared string. */
11522 if (bufs[2] + l2 > bufs[3])
11523 return NULL;
11524
11525 if (memcmp(pos, url_param_name, l1) == 0 &&
11526 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11527 return pos;
11528
11529 /* Perform wrapping and jump the string who fail the comparison. */
11530 bufs += 2;
11531 pos = bufs[0] + l2;
11532 last = bufs[1];
11533
11534 } else {
11535 /* process a simple comparison. */
11536 if (memcmp(pos, url_param_name, url_param_name_l) == 0) {
11537 return pos; }
11538 pos += url_param_name_l + 1;
11539 if (fix_pointer_if_wrap(chunks, &pos))
11540 last = bufs[2];
11541 }
11542 }
11543
11544 while (1) {
11545 /* Look for the next delimiter. */
11546 while (pos <= last && !is_param_delimiter(*pos, delim))
11547 pos++;
11548 if (pos < last)
11549 break;
11550 /* process buffer wrapping. */
11551 if (bufs != chunks || !bufs[2])
11552 return NULL;
11553 bufs += 2;
11554 pos = bufs[0];
11555 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011556 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011557 pos++;
11558 }
11559 return NULL;
11560}
11561
11562/*
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011563 * Given a url parameter name and a query string, returns its value and size
11564 * into *value and *value_l respectively, and returns non-zero. An empty
11565 * url_param_name matches the first available parameter. If the parameter is
11566 * not found, zero is returned and value/value_l are not touched.
David Cournapeau16023ee2010-12-23 20:55:41 +090011567 */
11568static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011569find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011570 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011571 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011572{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011573 const char *arg_start, *qs_end;
11574 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011575
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011576 arg_start = chunks[0];
11577 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011578 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011579 /* Looks for an argument name. */
11580 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011581 url_param_name, url_param_name_l,
11582 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011583 /* Check for wrapping. */
11584 if (arg_start > qs_end)
11585 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011586 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011587 if (!arg_start)
11588 return 0;
11589
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011590 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011591 while (1) {
11592 /* looks for the first argument. */
11593 value_start = memchr(arg_start, '=', qs_end - arg_start);
11594 if (!value_start) {
11595
11596 /* Check for wrapping. */
11597 if (arg_start >= chunks[0] &&
11598 arg_start <= chunks[1] &&
11599 chunks[2]) {
11600 arg_start = chunks[2];
11601 qs_end = chunks[3];
11602 continue;
11603 }
11604 return 0;
11605 }
11606 break;
11607 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011608 value_start++;
11609 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011610 else {
11611 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011612 value_start = arg_start + url_param_name_l + 1;
11613
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011614 /* Check for pointer wrapping. */
11615 if (fix_pointer_if_wrap(chunks, &value_start)) {
11616 /* Update the end pointer. */
11617 qs_end = chunks[3];
11618
11619 /* Check for overflow. */
11620 if (value_start > qs_end)
11621 return 0;
11622 }
11623 }
11624
David Cournapeau16023ee2010-12-23 20:55:41 +090011625 value_end = value_start;
11626
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011627 while (1) {
11628 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11629 value_end++;
11630 if (value_end < qs_end)
11631 break;
11632 /* process buffer wrapping. */
11633 if (value_end >= chunks[0] &&
11634 value_end <= chunks[1] &&
11635 chunks[2]) {
11636 value_end = chunks[2];
11637 qs_end = chunks[3];
11638 continue;
11639 }
11640 break;
11641 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011642
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011643 *vstart = value_start;
11644 *vend = value_end;
Willy Tarreau00134332011-01-04 14:57:34 +010011645 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011646}
11647
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011648/* This scans a URL-encoded query string. It takes an optionally wrapping
11649 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11650 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11651 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011652 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011653static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011654smp_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 +090011655{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011656 const char *vstart, *vend;
11657 struct chunk *temp;
11658 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011659
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011660 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011661 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011662 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011663 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011664 return 0;
11665
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011666 /* Create sample. If the value is contiguous, return the pointer as CONST,
11667 * if the value is wrapped, copy-it in a buffer.
11668 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011669 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011670 if (chunks[2] &&
11671 vstart >= chunks[0] && vstart <= chunks[1] &&
11672 vend >= chunks[2] && vend <= chunks[3]) {
11673 /* Wrapped case. */
11674 temp = get_trash_chunk();
11675 memcpy(temp->str, vstart, chunks[1] - vstart);
11676 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011677 smp->data.u.str.str = temp->str;
11678 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011679 } else {
11680 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011681 smp->data.u.str.str = (char *)vstart;
11682 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011683 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11684 }
11685
11686 /* Update context, check wrapping. */
11687 chunks[0] = vend;
11688 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11689 chunks[1] = chunks[3];
11690 chunks[2] = NULL;
11691 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011692
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011693 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011694 smp->flags |= SMP_F_NOT_LAST;
11695
David Cournapeau16023ee2010-12-23 20:55:41 +090011696 return 1;
11697}
11698
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011699/* This function iterates over each parameter of the query string. It uses
11700 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011701 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11702 * An optional parameter name is passed in args[0], otherwise any parameter is
11703 * considered. It supports an optional delimiter argument for the beginning of
11704 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011705 */
11706static int
11707smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11708{
11709 struct http_msg *msg;
11710 char delim = '?';
11711 const char *name;
11712 int name_len;
11713
Dragan Dosen26f77e52015-05-25 10:02:11 +020011714 if (!args ||
11715 (args[0].type && args[0].type != ARGT_STR) ||
11716 (args[1].type && args[1].type != ARGT_STR))
11717 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011718
Dragan Dosen26f77e52015-05-25 10:02:11 +020011719 name = "";
11720 name_len = 0;
11721 if (args->type == ARGT_STR) {
11722 name = args->data.str.str;
11723 name_len = args->data.str.len;
11724 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011725
Dragan Dosen26f77e52015-05-25 10:02:11 +020011726 if (args[1].type)
11727 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011728
Dragan Dosen26f77e52015-05-25 10:02:11 +020011729 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011730 CHECK_HTTP_MESSAGE_FIRST();
11731
11732 msg = &smp->strm->txn->req;
11733
11734 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11735 msg->sl.rq.u_l, delim);
11736 if (!smp->ctx.a[0])
11737 return 0;
11738
11739 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011740
11741 /* Assume that the context is filled with NULL pointer
11742 * before the first call.
11743 * smp->ctx.a[2] = NULL;
11744 * smp->ctx.a[3] = NULL;
11745 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011746 }
11747
11748 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11749}
11750
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011751/* This function iterates over each parameter of the body. This requires
11752 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011753 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11754 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11755 * optional second part if the body wraps at the end of the buffer. An optional
11756 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011757 */
11758static int
11759smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11760{
11761 struct http_txn *txn = smp->strm->txn;
11762 struct http_msg *msg;
11763 unsigned long len;
11764 unsigned long block1;
11765 char *body;
11766 const char *name;
11767 int name_len;
11768
11769 if (!args || (args[0].type && args[0].type != ARGT_STR))
11770 return 0;
11771
11772 name = "";
11773 name_len = 0;
11774 if (args[0].type == ARGT_STR) {
11775 name = args[0].data.str.str;
11776 name_len = args[0].data.str.len;
11777 }
11778
11779 if (!smp->ctx.a[0]) { // first call, find the query string
11780 CHECK_HTTP_MESSAGE_FIRST();
11781
11782 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
11783 msg = &txn->req;
11784 else
11785 msg = &txn->rsp;
11786
11787 len = http_body_bytes(msg);
11788 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11789
11790 block1 = len;
11791 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11792 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11793
11794 if (block1 == len) {
11795 /* buffer is not wrapped (or empty) */
11796 smp->ctx.a[0] = body;
11797 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011798
11799 /* Assume that the context is filled with NULL pointer
11800 * before the first call.
11801 * smp->ctx.a[2] = NULL;
11802 * smp->ctx.a[3] = NULL;
11803 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011804 }
11805 else {
11806 /* buffer is wrapped, we need to defragment it */
11807 smp->ctx.a[0] = body;
11808 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011809 smp->ctx.a[2] = msg->chn->buf->data;
11810 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011811 }
11812 }
11813 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11814}
11815
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011816/* Return the signed integer value for the specified url parameter (see url_param
11817 * above).
11818 */
11819static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011820smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011821{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011822 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011823
11824 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011825 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011826 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011827 }
11828
11829 return ret;
11830}
11831
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011832/* This produces a 32-bit hash of the concatenation of the first occurrence of
11833 * the Host header followed by the path component if it begins with a slash ('/').
11834 * This means that '*' will not be added, resulting in exactly the first Host
11835 * entry. If no Host header is found, then the path is used. The resulting value
11836 * is hashed using the url hash followed by a full avalanche hash and provides a
11837 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11838 * high-traffic sites without having to store whole paths.
11839 * this differs from the base32 functions in that it includes the url parameters
11840 * as well as the path
11841 */
11842static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011843smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011844{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011845 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011846 struct hdr_ctx ctx;
11847 unsigned int hash = 0;
11848 char *ptr, *beg, *end;
11849 int len;
11850
11851 CHECK_HTTP_MESSAGE_FIRST();
11852
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011853 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011854 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011855 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011856 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11857 ptr = ctx.line + ctx.val;
11858 len = ctx.vlen;
11859 while (len--)
11860 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11861 }
11862
11863 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011864 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 +000011865 beg = http_get_path(txn);
11866 if (!beg)
11867 beg = end;
11868
11869 for (ptr = beg; ptr < end ; ptr++);
11870
11871 if (beg < ptr && *beg == '/') {
11872 while (beg < ptr)
11873 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11874 }
11875 hash = full_hash(hash);
11876
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011877 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011878 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011879 smp->flags = SMP_F_VOL_1ST;
11880 return 1;
11881}
11882
11883/* This concatenates the source address with the 32-bit hash of the Host and
11884 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11885 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11886 * on the source address length. The URL hash is stored before the address so
11887 * that in environments where IPv6 is insignificant, truncating the output to
11888 * 8 bytes would still work.
11889 */
11890static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011891smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011892{
11893 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011894 struct connection *cli_conn = objt_conn(smp->sess->origin);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011895 unsigned int hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011896
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011897 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011898 return 0;
11899
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011900 /* The returned hash is a 32 bytes integer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011901 hash = smp->data.u.sint;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011902
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011903 temp = get_trash_chunk();
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011904 memcpy(temp->str + temp->len, &hash, sizeof(hash));
11905 temp->len += sizeof(hash);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011906
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011907 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011908 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011909 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011910 temp->len += 4;
11911 break;
11912 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011913 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011914 temp->len += 16;
11915 break;
11916 default:
11917 return 0;
11918 }
11919
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011920 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011921 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011922 return 1;
11923}
11924
Willy Tarreau185b5c42012-04-26 15:11:51 +020011925/* This function is used to validate the arguments passed to any "hdr" fetch
11926 * keyword. These keywords support an optional positive or negative occurrence
11927 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11928 * is assumed that the types are already the correct ones. Returns 0 on error,
11929 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11930 * error message in case of error, that the caller is responsible for freeing.
11931 * The initial location must either be freeable or NULL.
11932 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011933int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011934{
11935 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011936 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011937 return 0;
11938 }
11939 return 1;
11940}
11941
Willy Tarreau276fae92013-07-25 14:36:01 +020011942/* takes an UINT value on input supposed to represent the time since EPOCH,
11943 * adds an optional offset found in args[0] and emits a string representing
11944 * the date in RFC-1123/5322 format.
11945 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011946static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011947{
11948 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11949 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11950 struct chunk *temp;
11951 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011952 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011953 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011954
11955 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011956 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011957 curr_date += args[0].data.sint;
11958
11959 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011960 if (!tm)
11961 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011962
11963 temp = get_trash_chunk();
11964 temp->len = snprintf(temp->str, temp->size - temp->len,
11965 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11966 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11967 tm->tm_hour, tm->tm_min, tm->tm_sec);
11968
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011969 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011970 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011971 return 1;
11972}
11973
Thierry FOURNIERad903512014-04-11 17:51:01 +020011974/* Match language range with language tag. RFC2616 14.4:
11975 *
11976 * A language-range matches a language-tag if it exactly equals
11977 * the tag, or if it exactly equals a prefix of the tag such
11978 * that the first tag character following the prefix is "-".
11979 *
11980 * Return 1 if the strings match, else return 0.
11981 */
11982static inline int language_range_match(const char *range, int range_len,
11983 const char *tag, int tag_len)
11984{
11985 const char *end = range + range_len;
11986 const char *tend = tag + tag_len;
11987 while (range < end) {
11988 if (*range == '-' && tag == tend)
11989 return 1;
11990 if (*range != *tag || tag == tend)
11991 return 0;
11992 range++;
11993 tag++;
11994 }
11995 /* Return true only if the last char of the tag is matched. */
11996 return tag == tend;
11997}
11998
11999/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012000static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012001{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012002 const char *al = smp->data.u.str.str;
12003 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012004 const char *token;
12005 int toklen;
12006 int qvalue;
12007 const char *str;
12008 const char *w;
12009 int best_q = 0;
12010
12011 /* Set the constant to the sample, because the output of the
12012 * function will be peek in the constant configuration string.
12013 */
12014 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012015 smp->data.u.str.size = 0;
12016 smp->data.u.str.str = "";
12017 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012018
12019 /* Parse the accept language */
12020 while (1) {
12021
12022 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012023 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012024 al++;
12025 if (al >= end)
12026 break;
12027
12028 /* Start of the fisrt word. */
12029 token = al;
12030
12031 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012032 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012033 al++;
12034 if (al == token)
12035 goto expect_comma;
12036
12037 /* Length of the token. */
12038 toklen = al - token;
12039 qvalue = 1000;
12040
12041 /* Check if the token exists in the list. If the token not exists,
12042 * jump to the next token.
12043 */
12044 str = args[0].data.str.str;
12045 w = str;
12046 while (1) {
12047 if (*str == ';' || *str == '\0') {
12048 if (language_range_match(token, toklen, w, str-w))
12049 goto look_for_q;
12050 if (*str == '\0')
12051 goto expect_comma;
12052 w = str + 1;
12053 }
12054 str++;
12055 }
12056 goto expect_comma;
12057
12058look_for_q:
12059
12060 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012061 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012062 al++;
12063 if (al >= end)
12064 goto process_value;
12065
12066 /* If ',' is found, process the result */
12067 if (*al == ',')
12068 goto process_value;
12069
12070 /* If the character is different from ';', look
12071 * for the end of the header part in best effort.
12072 */
12073 if (*al != ';')
12074 goto expect_comma;
12075
12076 /* Assumes that the char is ';', now expect "q=". */
12077 al++;
12078
12079 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012080 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012081 al++;
12082 if (al >= end)
12083 goto process_value;
12084
12085 /* Expect 'q'. If no 'q', continue in best effort */
12086 if (*al != 'q')
12087 goto process_value;
12088 al++;
12089
12090 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012091 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012092 al++;
12093 if (al >= end)
12094 goto process_value;
12095
12096 /* Expect '='. If no '=', continue in best effort */
12097 if (*al != '=')
12098 goto process_value;
12099 al++;
12100
12101 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012102 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012103 al++;
12104 if (al >= end)
12105 goto process_value;
12106
12107 /* Parse the q value. */
12108 qvalue = parse_qvalue(al, &al);
12109
12110process_value:
12111
12112 /* If the new q value is the best q value, then store the associated
12113 * language in the response. If qvalue is the biggest value (1000),
12114 * break the process.
12115 */
12116 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012117 smp->data.u.str.str = (char *)w;
12118 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012119 if (qvalue >= 1000)
12120 break;
12121 best_q = qvalue;
12122 }
12123
12124expect_comma:
12125
12126 /* Expect comma or end. If the end is detected, quit the loop. */
12127 while (al < end && *al != ',')
12128 al++;
12129 if (al >= end)
12130 break;
12131
12132 /* Comma is found, jump it and restart the analyzer. */
12133 al++;
12134 }
12135
12136 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012137 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12138 smp->data.u.str.str = args[1].data.str.str;
12139 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012140 }
12141
12142 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012143 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012144}
12145
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012146/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012147static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012148{
12149 /* If the constant flag is set or if not size is avalaible at
12150 * the end of the buffer, copy the string in other buffer
12151 * before decoding.
12152 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012153 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012154 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012155 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12156 smp->data.u.str.str = str->str;
12157 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012158 smp->flags &= ~SMP_F_CONST;
12159 }
12160
12161 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012162 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12163 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012164 return 1;
12165}
12166
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012167static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12168{
12169 struct proxy *fe = strm_fe(smp->strm);
12170 int idx, i;
12171 struct cap_hdr *hdr;
12172 int len;
12173
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012174 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012175 return 0;
12176
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012177 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012178
12179 /* Check the availibity of the capture id. */
12180 if (idx > fe->nb_req_cap - 1)
12181 return 0;
12182
12183 /* Look for the original configuration. */
12184 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12185 hdr != NULL && i != idx ;
12186 i--, hdr = hdr->next);
12187 if (!hdr)
12188 return 0;
12189
12190 /* check for the memory allocation */
12191 if (smp->strm->req_cap[hdr->index] == NULL)
12192 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12193 if (smp->strm->req_cap[hdr->index] == NULL)
12194 return 0;
12195
12196 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012197 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012198 if (len > hdr->len)
12199 len = hdr->len;
12200
12201 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012202 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012203 smp->strm->req_cap[idx][len] = '\0';
12204
12205 return 1;
12206}
12207
12208static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12209{
12210 struct proxy *fe = strm_fe(smp->strm);
12211 int idx, i;
12212 struct cap_hdr *hdr;
12213 int len;
12214
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012215 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012216 return 0;
12217
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012218 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012219
12220 /* Check the availibity of the capture id. */
12221 if (idx > fe->nb_rsp_cap - 1)
12222 return 0;
12223
12224 /* Look for the original configuration. */
12225 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12226 hdr != NULL && i != idx ;
12227 i--, hdr = hdr->next);
12228 if (!hdr)
12229 return 0;
12230
12231 /* check for the memory allocation */
12232 if (smp->strm->res_cap[hdr->index] == NULL)
12233 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12234 if (smp->strm->res_cap[hdr->index] == NULL)
12235 return 0;
12236
12237 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012238 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012239 if (len > hdr->len)
12240 len = hdr->len;
12241
12242 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012243 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012244 smp->strm->res_cap[idx][len] = '\0';
12245
12246 return 1;
12247}
12248
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012249/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012250 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012251 * the relevant part of the request line accordingly. Then it updates various
12252 * pointers to the next elements which were moved, and the total buffer length.
12253 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012254 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12255 * error, though this can be revisited when this code is finally exploited.
12256 *
12257 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12258 * query string and 3 to replace uri.
12259 *
12260 * In query string case, the mark question '?' must be set at the start of the
12261 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012262 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012263int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012264 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012265{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012266 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012267 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012268 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012269 int delta;
12270
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012271 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012272 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012273 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012274 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12275
12276 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012277 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012278 txn->req.sl.rq.m_l += delta;
12279 txn->req.sl.rq.u += delta;
12280 txn->req.sl.rq.v += delta;
12281 break;
12282
12283 case 1: // path
12284 cur_ptr = http_get_path(txn);
12285 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012286 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012287
12288 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012289 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 +010012290 cur_end++;
12291
12292 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012293 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012294 txn->req.sl.rq.u_l += delta;
12295 txn->req.sl.rq.v += delta;
12296 break;
12297
12298 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012299 offset = 1;
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 while (cur_ptr < cur_end && *cur_ptr != '?')
12303 cur_ptr++;
12304
12305 /* skip the question mark or indicate that we must insert it
12306 * (but only if the format string is not empty then).
12307 */
12308 if (cur_ptr < cur_end)
12309 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012310 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012311 offset = 0;
12312
12313 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012314 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012315 txn->req.sl.rq.u_l += delta;
12316 txn->req.sl.rq.v += delta;
12317 break;
12318
12319 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012320 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012321 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12322
12323 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012324 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012325 txn->req.sl.rq.u_l += delta;
12326 txn->req.sl.rq.v += delta;
12327 break;
12328
12329 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012330 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012331 }
12332
12333 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012334 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012335 txn->req.sl.rq.l += delta;
12336 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012337 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012338 return 0;
12339}
12340
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012341/* This function replace the HTTP status code and the associated message. The
12342 * variable <status> contains the new status code. This function never fails.
12343 */
12344void http_set_status(unsigned int status, struct stream *s)
12345{
12346 struct http_txn *txn = s->txn;
12347 char *cur_ptr, *cur_end;
12348 int delta;
12349 char *res;
12350 int c_l;
12351 const char *msg;
12352 int msg_len;
12353
12354 chunk_reset(&trash);
12355
12356 res = ultoa_o(status, trash.str, trash.size);
12357 c_l = res - trash.str;
12358
12359 trash.str[c_l] = ' ';
12360 trash.len = c_l + 1;
12361
12362 msg = get_reason(status);
12363 msg_len = strlen(msg);
12364
12365 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12366 trash.len += msg_len;
12367
12368 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12369 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12370
12371 /* commit changes and adjust message */
12372 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12373
12374 /* adjust res line offsets and lengths */
12375 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12376 txn->rsp.sl.st.c_l = c_l;
12377 txn->rsp.sl.st.r_l = msg_len;
12378
12379 delta = trash.len - (cur_end - cur_ptr);
12380 txn->rsp.sl.st.l += delta;
12381 txn->hdr_idx.v[0].len += delta;
12382 http_msg_move_end(&txn->rsp, delta);
12383}
12384
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012385/* This function executes one of the set-{method,path,query,uri} actions. It
12386 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012387 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012388 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012389 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12390 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012391 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012392enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
12393 struct session *sess, struct stream *s)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012394{
12395 chunk_reset(&trash);
12396
12397 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012398 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012399 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012400 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012401
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012402 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012403 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012404}
12405
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012406/* This function is just a compliant action wrapper for "set-status". */
12407enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
12408 struct session *sess, struct stream *s)
12409{
12410 http_set_status(rule->arg.status.code, s);
12411 return ACT_RET_CONT;
12412}
12413
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012414/* parse an http-request action among :
12415 * set-method
12416 * set-path
12417 * set-query
12418 * set-uri
12419 *
12420 * All of them accept a single argument of type string representing a log-format.
12421 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12422 * 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 +020012423 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012424 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012425enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12426 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012427{
12428 int cur_arg = *orig_arg;
12429
Thierry FOURNIER91f6ba02015-08-06 08:30:11 +020012430 rule->action = ACT_ACTION_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012431
12432 switch (args[0][4]) {
12433 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012434 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012435 rule->action_ptr = http_action_set_req_line;
12436 break;
12437 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012438 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012439 rule->action_ptr = http_action_set_req_line;
12440 break;
12441 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012442 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012443 rule->action_ptr = http_action_set_req_line;
12444 break;
12445 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012446 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012447 rule->action_ptr = http_action_set_req_line;
12448 break;
12449 default:
12450 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012451 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012452 }
12453
12454 if (!*args[cur_arg] ||
12455 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12456 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012457 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012458 }
12459
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012460 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012461 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012462 parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012463 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12464 proxy->conf.args.file, proxy->conf.args.line);
12465
12466 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012467 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012468}
12469
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012470/* parse set-status action:
12471 * This action accepts a single argument of type int representing
12472 * an http status code. It returns ACT_RET_PRS_OK on success,
12473 * ACT_RET_PRS_ERR on error.
12474 */
12475enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12476 struct act_rule *rule, char **err)
12477{
12478 char *error;
12479
12480 rule->action = ACT_ACTION_CONT;
12481 rule->action_ptr = action_http_set_status;
12482
12483 /* Check if an argument is available */
12484 if (!*args[*orig_arg]) {
12485 memprintf(err, "expects exactly 1 argument <status>");
12486 return ACT_RET_PRS_ERR;
12487 }
12488
12489 /* convert status code as integer */
12490 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12491 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12492 memprintf(err, "expects an integer status code between 100 and 999");
12493 return ACT_RET_PRS_ERR;
12494 }
12495
12496 (*orig_arg)++;
12497 return ACT_RET_PRS_OK;
12498}
12499
Willy Tarreaua9083d02015-05-08 15:27:59 +020012500/* This function executes the "capture" action. It executes a fetch expression,
12501 * turns the result into a string and puts it in a capture slot. It always
12502 * returns 1. If an error occurs the action is cancelled, but the rule
12503 * processing continues.
12504 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012505enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
12506 struct session *sess, struct stream *s)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012507{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012508 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012509 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012510 char **cap = s->req_cap;
12511 int len;
12512
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012513 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 +020012514 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012515 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012516
12517 if (cap[h->index] == NULL)
12518 cap[h->index] = pool_alloc2(h->pool);
12519
12520 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012521 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012522
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012523 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012524 if (len > h->len)
12525 len = h->len;
12526
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012527 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012528 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012529 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012530}
12531
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012532/* This function executes the "capture" action and store the result in a
12533 * capture slot if exists. It executes a fetch expression, turns the result
12534 * into a string and puts it in a capture slot. It always returns 1. If an
12535 * error occurs the action is cancelled, but the rule processing continues.
12536 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012537enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
12538 struct session *sess, struct stream *s)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012539{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012540 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012541 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012542 char **cap = s->req_cap;
12543 struct proxy *fe = strm_fe(s);
12544 int len;
12545 int i;
12546
12547 /* Look for the original configuration. */
12548 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012549 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012550 i--, h = h->next);
12551 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012552 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012553
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012554 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 +020012555 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012556 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012557
12558 if (cap[h->index] == NULL)
12559 cap[h->index] = pool_alloc2(h->pool);
12560
12561 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012562 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012563
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012564 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012565 if (len > h->len)
12566 len = h->len;
12567
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012568 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012569 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012570 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012571}
12572
Willy Tarreaua9083d02015-05-08 15:27:59 +020012573/* parse an "http-request capture" action. It takes a single argument which is
12574 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012575 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012576 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012577 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012578enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12579 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012580{
12581 struct sample_expr *expr;
12582 struct cap_hdr *hdr;
12583 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012584 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012585
12586 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12587 if (strcmp(args[cur_arg], "if") == 0 ||
12588 strcmp(args[cur_arg], "unless") == 0)
12589 break;
12590
12591 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012592 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012593 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012594 }
12595
Willy Tarreaua9083d02015-05-08 15:27:59 +020012596 cur_arg = *orig_arg;
12597 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12598 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012599 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012600
12601 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12602 memprintf(err,
12603 "fetch method '%s' extracts information from '%s', none of which is available here",
12604 args[cur_arg-1], sample_src_names(expr->fetch->use));
12605 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012606 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012607 }
12608
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012609 if (!args[cur_arg] || !*args[cur_arg]) {
12610 memprintf(err, "expects 'len or 'id'");
12611 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012612 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012613 }
12614
Willy Tarreaua9083d02015-05-08 15:27:59 +020012615 if (strcmp(args[cur_arg], "len") == 0) {
12616 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012617
12618 if (!(px->cap & PR_CAP_FE)) {
12619 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012620 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012621 }
12622
12623 proxy->conf.args.ctx = ARGC_CAP;
12624
Willy Tarreaua9083d02015-05-08 15:27:59 +020012625 if (!args[cur_arg]) {
12626 memprintf(err, "missing length value");
12627 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012628 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012629 }
12630 /* we copy the table name for now, it will be resolved later */
12631 len = atoi(args[cur_arg]);
12632 if (len <= 0) {
12633 memprintf(err, "length must be > 0");
12634 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012635 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012636 }
12637 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012638
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012639 if (!len) {
12640 memprintf(err, "a positive 'len' argument is mandatory");
12641 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012642 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012643 }
12644
12645 hdr = calloc(sizeof(struct cap_hdr), 1);
12646 hdr->next = px->req_cap;
12647 hdr->name = NULL; /* not a header capture */
12648 hdr->namelen = 0;
12649 hdr->len = len;
12650 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12651 hdr->index = px->nb_req_cap++;
12652
12653 px->req_cap = hdr;
12654 px->to_log |= LW_REQHDR;
12655
Thierry FOURNIER91f6ba02015-08-06 08:30:11 +020012656 rule->action = ACT_ACTION_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012657 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012658 rule->arg.cap.expr = expr;
12659 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012660 }
12661
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012662 else if (strcmp(args[cur_arg], "id") == 0) {
12663 int id;
12664 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012665
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012666 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012667
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012668 if (!args[cur_arg]) {
12669 memprintf(err, "missing id value");
12670 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012671 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012672 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012673
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012674 id = strtol(args[cur_arg], &error, 10);
12675 if (*error != '\0') {
12676 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12677 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012678 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012679 }
12680 cur_arg++;
12681
12682 proxy->conf.args.ctx = ARGC_CAP;
12683
Thierry FOURNIER91f6ba02015-08-06 08:30:11 +020012684 rule->action = ACT_ACTION_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012685 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012686 rule->arg.capid.expr = expr;
12687 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012688 }
12689
12690 else {
12691 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12692 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012693 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012694 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012695
12696 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012697 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012698}
12699
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012700/* This function executes the "capture" action and store the result in a
12701 * capture slot if exists. It executes a fetch expression, turns the result
12702 * into a string and puts it in a capture slot. It always returns 1. If an
12703 * error occurs the action is cancelled, but the rule processing continues.
12704 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012705enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
12706 struct session *sess, struct stream *s)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012707{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012708 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012709 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012710 char **cap = s->res_cap;
12711 struct proxy *fe = strm_fe(s);
12712 int len;
12713 int i;
12714
12715 /* Look for the original configuration. */
12716 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012717 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012718 i--, h = h->next);
12719 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012720 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012721
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012722 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 +020012723 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012724 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012725
12726 if (cap[h->index] == NULL)
12727 cap[h->index] = pool_alloc2(h->pool);
12728
12729 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012730 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012731
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012732 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012733 if (len > h->len)
12734 len = h->len;
12735
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012736 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012737 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012738 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012739}
12740
12741/* parse an "http-response capture" action. It takes a single argument which is
12742 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12743 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012744 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012745 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012746enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12747 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012748{
12749 struct sample_expr *expr;
12750 int cur_arg;
12751 int id;
12752 char *error;
12753
12754 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12755 if (strcmp(args[cur_arg], "if") == 0 ||
12756 strcmp(args[cur_arg], "unless") == 0)
12757 break;
12758
12759 if (cur_arg < *orig_arg + 3) {
12760 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012761 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012762 }
12763
12764 cur_arg = *orig_arg;
12765 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12766 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012767 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012768
12769 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12770 memprintf(err,
12771 "fetch method '%s' extracts information from '%s', none of which is available here",
12772 args[cur_arg-1], sample_src_names(expr->fetch->use));
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 if (!args[cur_arg] || !*args[cur_arg]) {
12778 memprintf(err, "expects 'len or 'id'");
12779 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012780 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012781 }
12782
12783 if (strcmp(args[cur_arg], "id") != 0) {
12784 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12785 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012786 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012787 }
12788
12789 cur_arg++;
12790
12791 if (!args[cur_arg]) {
12792 memprintf(err, "missing id value");
12793 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012794 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012795 }
12796
12797 id = strtol(args[cur_arg], &error, 10);
12798 if (*error != '\0') {
12799 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12800 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012801 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012802 }
12803 cur_arg++;
12804
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012805 proxy->conf.args.ctx = ARGC_CAP;
12806
Thierry FOURNIER91f6ba02015-08-06 08:30:11 +020012807 rule->action = ACT_ACTION_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012808 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012809 rule->arg.capid.expr = expr;
12810 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012811
12812 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012813 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012814}
12815
William Lallemand73025dd2014-04-24 14:38:37 +020012816/*
12817 * Return the struct http_req_action_kw associated to a keyword.
12818 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012819struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012820{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012821 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012822}
12823
12824/*
12825 * Return the struct http_res_action_kw associated to a keyword.
12826 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012827struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012828{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012829 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012830}
12831
Willy Tarreau4a568972010-05-12 08:08:50 +020012832/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012833/* All supported ACL keywords must be declared here. */
12834/************************************************************************/
12835
12836/* Note: must not be declared <const> as its list will be overwritten.
12837 * Please take care of keeping this list alphabetically sorted.
12838 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012839static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012840 { "base", "base", PAT_MATCH_STR },
12841 { "base_beg", "base", PAT_MATCH_BEG },
12842 { "base_dir", "base", PAT_MATCH_DIR },
12843 { "base_dom", "base", PAT_MATCH_DOM },
12844 { "base_end", "base", PAT_MATCH_END },
12845 { "base_len", "base", PAT_MATCH_LEN },
12846 { "base_reg", "base", PAT_MATCH_REG },
12847 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012848
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012849 { "cook", "req.cook", PAT_MATCH_STR },
12850 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12851 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12852 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12853 { "cook_end", "req.cook", PAT_MATCH_END },
12854 { "cook_len", "req.cook", PAT_MATCH_LEN },
12855 { "cook_reg", "req.cook", PAT_MATCH_REG },
12856 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012857
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012858 { "hdr", "req.hdr", PAT_MATCH_STR },
12859 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12860 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12861 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12862 { "hdr_end", "req.hdr", PAT_MATCH_END },
12863 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12864 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12865 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012866
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012867 /* these two declarations uses strings with list storage (in place
12868 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12869 * and delete functions are relative to the list management. The parse
12870 * and match method are related to the corresponding fetch methods. This
12871 * is very particular ACL declaration mode.
12872 */
12873 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12874 { "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 +020012875
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012876 { "path", "path", PAT_MATCH_STR },
12877 { "path_beg", "path", PAT_MATCH_BEG },
12878 { "path_dir", "path", PAT_MATCH_DIR },
12879 { "path_dom", "path", PAT_MATCH_DOM },
12880 { "path_end", "path", PAT_MATCH_END },
12881 { "path_len", "path", PAT_MATCH_LEN },
12882 { "path_reg", "path", PAT_MATCH_REG },
12883 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012884
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012885 { "req_ver", "req.ver", PAT_MATCH_STR },
12886 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012887
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012888 { "scook", "res.cook", PAT_MATCH_STR },
12889 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12890 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12891 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12892 { "scook_end", "res.cook", PAT_MATCH_END },
12893 { "scook_len", "res.cook", PAT_MATCH_LEN },
12894 { "scook_reg", "res.cook", PAT_MATCH_REG },
12895 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012896
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012897 { "shdr", "res.hdr", PAT_MATCH_STR },
12898 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12899 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12900 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12901 { "shdr_end", "res.hdr", PAT_MATCH_END },
12902 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12903 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12904 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012905
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012906 { "url", "url", PAT_MATCH_STR },
12907 { "url_beg", "url", PAT_MATCH_BEG },
12908 { "url_dir", "url", PAT_MATCH_DIR },
12909 { "url_dom", "url", PAT_MATCH_DOM },
12910 { "url_end", "url", PAT_MATCH_END },
12911 { "url_len", "url", PAT_MATCH_LEN },
12912 { "url_reg", "url", PAT_MATCH_REG },
12913 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012914
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012915 { "urlp", "urlp", PAT_MATCH_STR },
12916 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12917 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12918 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12919 { "urlp_end", "urlp", PAT_MATCH_END },
12920 { "urlp_len", "urlp", PAT_MATCH_LEN },
12921 { "urlp_reg", "urlp", PAT_MATCH_REG },
12922 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012923
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012924 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012925}};
12926
12927/************************************************************************/
12928/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012929/************************************************************************/
12930/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012931static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012932 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012933 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012934 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12935
Willy Tarreau87b09662015-04-03 00:22:06 +020012936 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012937 { "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 +020012938
12939 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012940 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12941 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12942 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012943
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012944 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12945 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012946
Willy Tarreau409bcde2013-01-08 00:31:00 +010012947 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12948 * are only here to match the ACL's name, are request-only and are used
12949 * for ACL compatibility only.
12950 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012951 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12952 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012953 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12954 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012955
12956 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12957 * only here to match the ACL's name, are request-only and are used for
12958 * ACL compatibility only.
12959 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012960 { "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 +020012961 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012962 { "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 +020012963 { "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 +010012964
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012965 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012966 { "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 +010012967 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012968 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012969 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012970 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012971
12972 /* HTTP protocol on the request path */
12973 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012974 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012975
12976 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012977 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12978 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012979
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012980 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012981 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12982 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012983 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012984
Willy Tarreau18ed2562013-01-14 15:56:36 +010012985 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012986 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12987 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012988
Willy Tarreau18ed2562013-01-14 15:56:36 +010012989 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012990 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012991 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12992 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012993
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012994 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012995 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012996 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012997 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012998 { "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 +010012999 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013000 { "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 +010013001
13002 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013003 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013004 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13005 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013006
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013007 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013008 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013009 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013010 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013011 { "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 +010013012 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013013 { "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 +010013014
Willy Tarreau409bcde2013-01-08 00:31:00 +010013015 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013016 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013017 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13018 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013019 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013020
13021 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013022 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013023 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013024 { "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 +020013025 { "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 +010013026
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013027 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013028 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013029 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013030 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013031 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013032 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013033 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13034 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013035 { "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 +010013036 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013037}};
13038
Willy Tarreau8797c062007-05-07 00:55:35 +020013039
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013040/************************************************************************/
13041/* All supported converter keywords must be declared here. */
13042/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013043/* Note: must not be declared <const> as its list will be overwritten */
13044static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013045 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013046 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013047 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13048 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013049 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013050 { NULL, NULL, 0, 0, 0 },
13051}};
13052
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013053
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013054/************************************************************************/
13055/* All supported http-request action keywords must be declared here. */
13056/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013057struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013058 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013059 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013060 { "set-method", parse_set_req_line },
13061 { "set-path", parse_set_req_line },
13062 { "set-query", parse_set_req_line },
13063 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013064 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013065 }
13066};
13067
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013068struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013069 .kw = {
13070 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013071 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013072 { NULL, NULL }
13073 }
13074};
13075
Willy Tarreau8797c062007-05-07 00:55:35 +020013076__attribute__((constructor))
13077static void __http_protocol_init(void)
13078{
13079 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013080 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013081 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013082 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013083 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020013084}
13085
13086
Willy Tarreau58f10d72006-12-04 02:26:12 +010013087/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013088 * Local variables:
13089 * c-indent-level: 8
13090 * c-basic-offset: 8
13091 * End:
13092 */