blob: 247c3b6120abab7850341293ff9fdad1ba774f2a [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreau8797c062007-05-07 00:55:35 +020044#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020045#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020064#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020068#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020069
Willy Tarreau522d6c02009-12-06 18:49:18 +010070const char HTTP_100[] =
71 "HTTP/1.1 100 Continue\r\n\r\n";
72
73const struct chunk http_100_chunk = {
74 .str = (char *)&HTTP_100,
75 .len = sizeof(HTTP_100)-1
76};
77
Willy Tarreaua9679ac2010-01-03 17:32:57 +010078/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020079const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010081 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020082 "Location: "; /* not terminated since it will be concatenated with the URL */
83
Willy Tarreau0f772532006-12-23 20:51:41 +010084const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010088 "Location: "; /* not terminated since it will be concatenated with the URL */
89
90/* same as 302 except that the browser MUST retry with the GET method */
91const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010094 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010095 "Location: "; /* not terminated since it will be concatenated with the URL */
96
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040097
98/* same as 302 except that the browser MUST retry with the same method */
99const char *HTTP_307 =
100 "HTTP/1.1 307 Temporary Redirect\r\n"
101 "Cache-Control: no-cache\r\n"
102 "Content-length: 0\r\n"
103 "Location: "; /* not terminated since it will be concatenated with the URL */
104
105/* same as 301 except that the browser MUST retry with the same method */
106const char *HTTP_308 =
107 "HTTP/1.1 308 Permanent Redirect\r\n"
108 "Content-length: 0\r\n"
109 "Location: "; /* not terminated since it will be concatenated with the URL */
110
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
112const char *HTTP_401_fmt =
113 "HTTP/1.0 401 Unauthorized\r\n"
114 "Cache-Control: no-cache\r\n"
115 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200116 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
118 "\r\n"
119 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
120
Willy Tarreau844a7e72010-01-31 21:46:18 +0100121const char *HTTP_407_fmt =
122 "HTTP/1.0 407 Unauthorized\r\n"
123 "Cache-Control: no-cache\r\n"
124 "Connection: close\r\n"
125 "Content-Type: text/html\r\n"
126 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
127 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800128 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100129
Willy Tarreau0f772532006-12-23 20:51:41 +0100130
131const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200132 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100133 [HTTP_ERR_400] = 400,
134 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400135 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100136 [HTTP_ERR_408] = 408,
CJ Ess108b1dd2015-04-07 12:03:37 -0400137 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 [HTTP_ERR_500] = 500,
139 [HTTP_ERR_502] = 502,
140 [HTTP_ERR_503] = 503,
141 [HTTP_ERR_504] = 504,
142};
143
Willy Tarreau80587432006-12-24 17:47:20 +0100144static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200145 [HTTP_ERR_200] =
146 "HTTP/1.0 200 OK\r\n"
147 "Cache-Control: no-cache\r\n"
148 "Connection: close\r\n"
149 "Content-Type: text/html\r\n"
150 "\r\n"
151 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
152
Willy Tarreau0f772532006-12-23 20:51:41 +0100153 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100154 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100155 "Cache-Control: no-cache\r\n"
156 "Connection: close\r\n"
157 "Content-Type: text/html\r\n"
158 "\r\n"
159 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
160
161 [HTTP_ERR_403] =
162 "HTTP/1.0 403 Forbidden\r\n"
163 "Cache-Control: no-cache\r\n"
164 "Connection: close\r\n"
165 "Content-Type: text/html\r\n"
166 "\r\n"
167 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
168
CJ Ess108b1dd2015-04-07 12:03:37 -0400169 [HTTP_ERR_405] =
170 "HTTP/1.0 405 Method Not Allowed\r\n"
171 "Cache-Control: no-cache\r\n"
172 "Connection: close\r\n"
173 "Content-Type: text/html\r\n"
174 "\r\n"
175 "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n",
176
Willy Tarreau0f772532006-12-23 20:51:41 +0100177 [HTTP_ERR_408] =
178 "HTTP/1.0 408 Request Time-out\r\n"
179 "Cache-Control: no-cache\r\n"
180 "Connection: close\r\n"
181 "Content-Type: text/html\r\n"
182 "\r\n"
183 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
184
CJ Ess108b1dd2015-04-07 12:03:37 -0400185 [HTTP_ERR_429] =
186 "HTTP/1.0 429 Too Many Requests\r\n"
187 "Cache-Control: no-cache\r\n"
188 "Connection: close\r\n"
189 "Content-Type: text/html\r\n"
190 "\r\n"
191 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
192
Willy Tarreau0f772532006-12-23 20:51:41 +0100193 [HTTP_ERR_500] =
194 "HTTP/1.0 500 Server Error\r\n"
195 "Cache-Control: no-cache\r\n"
196 "Connection: close\r\n"
197 "Content-Type: text/html\r\n"
198 "\r\n"
199 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
200
201 [HTTP_ERR_502] =
202 "HTTP/1.0 502 Bad Gateway\r\n"
203 "Cache-Control: no-cache\r\n"
204 "Connection: close\r\n"
205 "Content-Type: text/html\r\n"
206 "\r\n"
207 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
208
209 [HTTP_ERR_503] =
210 "HTTP/1.0 503 Service Unavailable\r\n"
211 "Cache-Control: no-cache\r\n"
212 "Connection: close\r\n"
213 "Content-Type: text/html\r\n"
214 "\r\n"
215 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
216
217 [HTTP_ERR_504] =
218 "HTTP/1.0 504 Gateway Time-out\r\n"
219 "Cache-Control: no-cache\r\n"
220 "Connection: close\r\n"
221 "Content-Type: text/html\r\n"
222 "\r\n"
223 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
224
225};
226
Cyril Bonté19979e12012-04-04 12:57:21 +0200227/* status codes available for the stats admin page (strictly 4 chars length) */
228const char *stat_status_codes[STAT_STATUS_SIZE] = {
229 [STAT_STATUS_DENY] = "DENY",
230 [STAT_STATUS_DONE] = "DONE",
231 [STAT_STATUS_ERRP] = "ERRP",
232 [STAT_STATUS_EXCD] = "EXCD",
233 [STAT_STATUS_NONE] = "NONE",
234 [STAT_STATUS_PART] = "PART",
235 [STAT_STATUS_UNKN] = "UNKN",
236};
237
238
William Lallemand73025dd2014-04-24 14:38:37 +0200239/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200240struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200241 .list = LIST_HEAD_INIT(http_req_keywords.list)
242};
243
244/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200245struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200246 .list = LIST_HEAD_INIT(http_res_keywords.list)
247};
248
Willy Tarreau80587432006-12-24 17:47:20 +0100249/* We must put the messages here since GCC cannot initialize consts depending
250 * on strlen().
251 */
252struct chunk http_err_chunks[HTTP_ERR_SIZE];
253
Willy Tarreaua890d072013-04-02 12:01:06 +0200254/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
255static struct hdr_ctx static_hdr_ctx;
256
Willy Tarreau42250582007-04-01 01:30:43 +0200257#define FD_SETS_ARE_BITFIELDS
258#ifdef FD_SETS_ARE_BITFIELDS
259/*
260 * This map is used with all the FD_* macros to check whether a particular bit
261 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
262 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
263 * byte should be encoded. Be careful to always pass bytes from 0 to 255
264 * exclusively to the macros.
265 */
266fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
267fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100268fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200269
270#else
271#error "Check if your OS uses bitfields for fd_sets"
272#endif
273
Willy Tarreau87b09662015-04-03 00:22:06 +0200274static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200275
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200276/* This function returns a reason associated with the HTTP status.
277 * This function never fails, a message is always returned.
278 */
279const char *get_reason(unsigned int status)
280{
281 switch (status) {
282 case 100: return "Continue";
283 case 101: return "Switching Protocols";
284 case 102: return "Processing";
285 case 200: return "OK";
286 case 201: return "Created";
287 case 202: return "Accepted";
288 case 203: return "Non-Authoritative Information";
289 case 204: return "No Content";
290 case 205: return "Reset Content";
291 case 206: return "Partial Content";
292 case 207: return "Multi-Status";
293 case 210: return "Content Different";
294 case 226: return "IM Used";
295 case 300: return "Multiple Choices";
296 case 301: return "Moved Permanently";
297 case 302: return "Moved Temporarily";
298 case 303: return "See Other";
299 case 304: return "Not Modified";
300 case 305: return "Use Proxy";
301 case 307: return "Temporary Redirect";
302 case 308: return "Permanent Redirect";
303 case 310: return "Too many Redirects";
304 case 400: return "Bad Request";
305 case 401: return "Unauthorized";
306 case 402: return "Payment Required";
307 case 403: return "Forbidden";
308 case 404: return "Not Found";
309 case 405: return "Method Not Allowed";
310 case 406: return "Not Acceptable";
311 case 407: return "Proxy Authentication Required";
312 case 408: return "Request Time-out";
313 case 409: return "Conflict";
314 case 410: return "Gone";
315 case 411: return "Length Required";
316 case 412: return "Precondition Failed";
317 case 413: return "Request Entity Too Large";
318 case 414: return "Request-URI Too Long";
319 case 415: return "Unsupported Media Type";
320 case 416: return "Requested range unsatisfiable";
321 case 417: return "Expectation failed";
322 case 418: return "I'm a teapot";
323 case 422: return "Unprocessable entity";
324 case 423: return "Locked";
325 case 424: return "Method failure";
326 case 425: return "Unordered Collection";
327 case 426: return "Upgrade Required";
328 case 428: return "Precondition Required";
329 case 429: return "Too Many Requests";
330 case 431: return "Request Header Fields Too Large";
331 case 449: return "Retry With";
332 case 450: return "Blocked by Windows Parental Controls";
333 case 451: return "Unavailable For Legal Reasons";
334 case 456: return "Unrecoverable Error";
335 case 499: return "client has closed connection";
336 case 500: return "Internal Server Error";
337 case 501: return "Not Implemented";
338 case 502: return "Bad Gateway ou Proxy Error";
339 case 503: return "Service Unavailable";
340 case 504: return "Gateway Time-out";
341 case 505: return "HTTP Version not supported";
342 case 506: return "Variant also negociate";
343 case 507: return "Insufficient storage";
344 case 508: return "Loop detected";
345 case 509: return "Bandwidth Limit Exceeded";
346 case 510: return "Not extended";
347 case 511: return "Network authentication required";
348 case 520: return "Web server is returning an unknown error";
349 default:
350 switch (status) {
351 case 100 ... 199: return "Informational";
352 case 200 ... 299: return "Success";
353 case 300 ... 399: return "Redirection";
354 case 400 ... 499: return "Client Error";
355 case 500 ... 599: return "Server Error";
356 default: return "Other";
357 }
358 }
359}
360
Willy Tarreau80587432006-12-24 17:47:20 +0100361void init_proto_http()
362{
Willy Tarreau42250582007-04-01 01:30:43 +0200363 int i;
364 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100365 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200366
Willy Tarreau80587432006-12-24 17:47:20 +0100367 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
368 if (!http_err_msgs[msg]) {
369 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
370 abort();
371 }
372
373 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
374 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
375 }
Willy Tarreau42250582007-04-01 01:30:43 +0200376
377 /* initialize the log header encoding map : '{|}"#' should be encoded with
378 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
379 * URL encoding only requires '"', '#' to be encoded as well as non-
380 * printable characters above.
381 */
382 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
383 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100384 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200385 for (i = 0; i < 32; i++) {
386 FD_SET(i, hdr_encode_map);
387 FD_SET(i, url_encode_map);
388 }
389 for (i = 127; i < 256; i++) {
390 FD_SET(i, hdr_encode_map);
391 FD_SET(i, url_encode_map);
392 }
393
394 tmp = "\"#{|}";
395 while (*tmp) {
396 FD_SET(*tmp, hdr_encode_map);
397 tmp++;
398 }
399
400 tmp = "\"#";
401 while (*tmp) {
402 FD_SET(*tmp, url_encode_map);
403 tmp++;
404 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200405
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100406 /* initialize the http header encoding map. The draft httpbis define the
407 * header content as:
408 *
409 * HTTP-message = start-line
410 * *( header-field CRLF )
411 * CRLF
412 * [ message-body ]
413 * header-field = field-name ":" OWS field-value OWS
414 * field-value = *( field-content / obs-fold )
415 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
416 * obs-fold = CRLF 1*( SP / HTAB )
417 * field-vchar = VCHAR / obs-text
418 * VCHAR = %x21-7E
419 * obs-text = %x80-FF
420 *
421 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
422 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
423 * "obs-fold" is volontary forgotten because haproxy remove this.
424 */
425 memset(http_encode_map, 0, sizeof(http_encode_map));
426 for (i = 0x00; i <= 0x08; i++)
427 FD_SET(i, http_encode_map);
428 for (i = 0x0a; i <= 0x1f; i++)
429 FD_SET(i, http_encode_map);
430 FD_SET(0x7f, http_encode_map);
431
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200432 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200433 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200434 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100435 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100436}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200437
Willy Tarreau53b6c742006-12-17 13:37:46 +0100438/*
439 * We have 26 list of methods (1 per first letter), each of which can have
440 * up to 3 entries (2 valid, 1 null).
441 */
442struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100443 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100444 int len;
445 const char text[8];
446};
447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100448const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100449 ['C' - 'A'] = {
450 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
451 },
452 ['D' - 'A'] = {
453 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
454 },
455 ['G' - 'A'] = {
456 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
457 },
458 ['H' - 'A'] = {
459 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
460 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200461 ['O' - 'A'] = {
462 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
463 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100464 ['P' - 'A'] = {
465 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
466 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
467 },
468 ['T' - 'A'] = {
469 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
470 },
471 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200472 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100473 */
474};
475
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100476const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100477 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
478 [HTTP_METH_GET] = { "GET", 3 },
479 [HTTP_METH_HEAD] = { "HEAD", 4 },
480 [HTTP_METH_POST] = { "POST", 4 },
481 [HTTP_METH_PUT] = { "PUT", 3 },
482 [HTTP_METH_DELETE] = { "DELETE", 6 },
483 [HTTP_METH_TRACE] = { "TRACE", 5 },
484 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
485};
486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100487/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200488 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100489 * RFC2616 for those chars.
490 */
491
492const char http_is_spht[256] = {
493 [' '] = 1, ['\t'] = 1,
494};
495
496const char http_is_crlf[256] = {
497 ['\r'] = 1, ['\n'] = 1,
498};
499
500const char http_is_lws[256] = {
501 [' '] = 1, ['\t'] = 1,
502 ['\r'] = 1, ['\n'] = 1,
503};
504
505const char http_is_sep[256] = {
506 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
507 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
508 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
509 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
510 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
511};
512
513const char http_is_ctl[256] = {
514 [0 ... 31] = 1,
515 [127] = 1,
516};
517
518/*
519 * A token is any ASCII char that is neither a separator nor a CTL char.
520 * Do not overwrite values in assignment since gcc-2.95 will not handle
521 * them correctly. Instead, define every non-CTL char's status.
522 */
523const char http_is_token[256] = {
524 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
525 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
526 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
527 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
528 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
529 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
530 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
531 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
532 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
533 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
534 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
535 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
536 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
537 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
538 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
539 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
540 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
541 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
542 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
543 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
544 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
545 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
546 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
547 ['|'] = 1, ['}'] = 0, ['~'] = 1,
548};
549
550
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100551/*
552 * An http ver_token is any ASCII which can be found in an HTTP version,
553 * which includes 'H', 'T', 'P', '/', '.' and any digit.
554 */
555const char http_is_ver_token[256] = {
556 ['.'] = 1, ['/'] = 1,
557 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
558 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
Thierry FOURNIER63d692c2015-02-28 19:03:56 +0100559 ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1,
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100560};
561
562
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100563/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100564 * Adds a header and its CRLF at the tail of the message's buffer, just before
565 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100566 * The header is also automatically added to the index <hdr_idx>, and the end
567 * of headers is automatically adjusted. The number of bytes added is returned
568 * on success, otherwise <0 is returned indicating an error.
569 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100570int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100571{
572 int bytes, len;
573
574 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200575 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100576 if (!bytes)
577 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100578 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100579 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
580}
581
582/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100583 * Adds a header and its CRLF at the tail of the message's buffer, just before
584 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100585 * the buffer is only opened and the space reserved, but nothing is copied.
586 * The header is also automatically added to the index <hdr_idx>, and the end
587 * of headers is automatically adjusted. The number of bytes added is returned
588 * on success, otherwise <0 is returned indicating an error.
589 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100590int http_header_add_tail2(struct http_msg *msg,
591 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100592{
593 int bytes;
594
Willy Tarreau9b28e032012-10-12 23:49:43 +0200595 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100596 if (!bytes)
597 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100598 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100599 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
600}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200601
602/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100603 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
604 * If so, returns the position of the first non-space character relative to
605 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
606 * to return a pointer to the place after the first space. Returns 0 if the
607 * header name does not match. Checks are case-insensitive.
608 */
609int http_header_match2(const char *hdr, const char *end,
610 const char *name, int len)
611{
612 const char *val;
613
614 if (hdr + len >= end)
615 return 0;
616 if (hdr[len] != ':')
617 return 0;
618 if (strncasecmp(hdr, name, len) != 0)
619 return 0;
620 val = hdr + len + 1;
621 while (val < end && HTTP_IS_SPHT(*val))
622 val++;
623 if ((val >= end) && (len + 2 <= end - hdr))
624 return len + 2; /* we may replace starting from second space */
625 return val - hdr;
626}
627
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200628/* Find the first or next occurrence of header <name> in message buffer <sol>
629 * using headers index <idx>, and return it in the <ctx> structure. This
630 * structure holds everything necessary to use the header and find next
631 * occurrence. If its <idx> member is 0, the header is searched from the
632 * beginning. Otherwise, the next occurrence is returned. The function returns
633 * 1 when it finds a value, and 0 when there is no more. It is very similar to
634 * http_find_header2() except that it is designed to work with full-line headers
635 * whose comma is not a delimiter but is part of the syntax. As a special case,
636 * if ctx->val is NULL when searching for a new values of a header, the current
637 * header is rescanned. This allows rescanning after a header deletion.
638 */
639int http_find_full_header2(const char *name, int len,
640 char *sol, struct hdr_idx *idx,
641 struct hdr_ctx *ctx)
642{
643 char *eol, *sov;
644 int cur_idx, old_idx;
645
646 cur_idx = ctx->idx;
647 if (cur_idx) {
648 /* We have previously returned a header, let's search another one */
649 sol = ctx->line;
650 eol = sol + idx->v[cur_idx].len;
651 goto next_hdr;
652 }
653
654 /* first request for this header */
655 sol += hdr_idx_first_pos(idx);
656 old_idx = 0;
657 cur_idx = hdr_idx_first_idx(idx);
658 while (cur_idx) {
659 eol = sol + idx->v[cur_idx].len;
660
661 if (len == 0) {
662 /* No argument was passed, we want any header.
663 * To achieve this, we simply build a fake request. */
664 while (sol + len < eol && sol[len] != ':')
665 len++;
666 name = sol;
667 }
668
669 if ((len < eol - sol) &&
670 (sol[len] == ':') &&
671 (strncasecmp(sol, name, len) == 0)) {
672 ctx->del = len;
673 sov = sol + len + 1;
674 while (sov < eol && http_is_lws[(unsigned char)*sov])
675 sov++;
676
677 ctx->line = sol;
678 ctx->prev = old_idx;
679 ctx->idx = cur_idx;
680 ctx->val = sov - sol;
681 ctx->tws = 0;
682 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
683 eol--;
684 ctx->tws++;
685 }
686 ctx->vlen = eol - sov;
687 return 1;
688 }
689 next_hdr:
690 sol = eol + idx->v[cur_idx].cr + 1;
691 old_idx = cur_idx;
692 cur_idx = idx->v[cur_idx].next;
693 }
694 return 0;
695}
696
Willy Tarreauc90dc232015-02-20 13:51:36 +0100697/* Find the first or next header field in message buffer <sol> using headers
698 * index <idx>, and return it in the <ctx> structure. This structure holds
699 * everything necessary to use the header and find next occurrence. If its
700 * <idx> member is 0, the first header is retrieved. Otherwise, the next
701 * occurrence is returned. The function returns 1 when it finds a value, and
702 * 0 when there is no more. It is equivalent to http_find_full_header2() with
703 * no header name.
704 */
705int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
706{
707 char *eol, *sov;
708 int cur_idx, old_idx;
709 int len;
710
711 cur_idx = ctx->idx;
712 if (cur_idx) {
713 /* We have previously returned a header, let's search another one */
714 sol = ctx->line;
715 eol = sol + idx->v[cur_idx].len;
716 goto next_hdr;
717 }
718
719 /* first request for this header */
720 sol += hdr_idx_first_pos(idx);
721 old_idx = 0;
722 cur_idx = hdr_idx_first_idx(idx);
723 while (cur_idx) {
724 eol = sol + idx->v[cur_idx].len;
725
726 len = 0;
727 while (1) {
728 if (len >= eol - sol)
729 goto next_hdr;
730 if (sol[len] == ':')
731 break;
732 len++;
733 }
734
735 ctx->del = len;
736 sov = sol + len + 1;
737 while (sov < eol && http_is_lws[(unsigned char)*sov])
738 sov++;
739
740 ctx->line = sol;
741 ctx->prev = old_idx;
742 ctx->idx = cur_idx;
743 ctx->val = sov - sol;
744 ctx->tws = 0;
745
746 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
747 eol--;
748 ctx->tws++;
749 }
750 ctx->vlen = eol - sov;
751 return 1;
752
753 next_hdr:
754 sol = eol + idx->v[cur_idx].cr + 1;
755 old_idx = cur_idx;
756 cur_idx = idx->v[cur_idx].next;
757 }
758 return 0;
759}
760
Willy Tarreau68085d82010-01-18 14:54:04 +0100761/* Find the end of the header value contained between <s> and <e>. See RFC2616,
762 * par 2.2 for more information. Note that it requires a valid header to return
763 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200764 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100765char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200766{
767 int quoted, qdpair;
768
769 quoted = qdpair = 0;
770 for (; s < e; s++) {
771 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200772 else if (quoted) {
773 if (*s == '\\') qdpair = 1;
774 else if (*s == '"') quoted = 0;
775 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200776 else if (*s == '"') quoted = 1;
777 else if (*s == ',') return s;
778 }
779 return s;
780}
781
782/* Find the first or next occurrence of header <name> in message buffer <sol>
783 * using headers index <idx>, and return it in the <ctx> structure. This
784 * structure holds everything necessary to use the header and find next
785 * occurrence. If its <idx> member is 0, the header is searched from the
786 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100787 * 1 when it finds a value, and 0 when there is no more. It is designed to work
788 * with headers defined as comma-separated lists. As a special case, if ctx->val
789 * is NULL when searching for a new values of a header, the current header is
790 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200791 */
792int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100793 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200794 struct hdr_ctx *ctx)
795{
Willy Tarreau68085d82010-01-18 14:54:04 +0100796 char *eol, *sov;
797 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200798
Willy Tarreau68085d82010-01-18 14:54:04 +0100799 cur_idx = ctx->idx;
800 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200801 /* We have previously returned a value, let's search
802 * another one on the same line.
803 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200804 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200805 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100806 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200807 eol = sol + idx->v[cur_idx].len;
808
809 if (sov >= eol)
810 /* no more values in this header */
811 goto next_hdr;
812
Willy Tarreau68085d82010-01-18 14:54:04 +0100813 /* values remaining for this header, skip the comma but save it
814 * for later use (eg: for header deletion).
815 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200816 sov++;
817 while (sov < eol && http_is_lws[(unsigned char)*sov])
818 sov++;
819
820 goto return_hdr;
821 }
822
823 /* first request for this header */
824 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100825 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200826 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200827 while (cur_idx) {
828 eol = sol + idx->v[cur_idx].len;
829
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200830 if (len == 0) {
831 /* No argument was passed, we want any header.
832 * To achieve this, we simply build a fake request. */
833 while (sol + len < eol && sol[len] != ':')
834 len++;
835 name = sol;
836 }
837
Willy Tarreau33a7e692007-06-10 19:45:56 +0200838 if ((len < eol - sol) &&
839 (sol[len] == ':') &&
840 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100841 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200842 sov = sol + len + 1;
843 while (sov < eol && http_is_lws[(unsigned char)*sov])
844 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100845
Willy Tarreau33a7e692007-06-10 19:45:56 +0200846 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100847 ctx->prev = old_idx;
848 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200849 ctx->idx = cur_idx;
850 ctx->val = sov - sol;
851
852 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200853 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200854 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200855 eol--;
856 ctx->tws++;
857 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200858 ctx->vlen = eol - sov;
859 return 1;
860 }
861 next_hdr:
862 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100863 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200864 cur_idx = idx->v[cur_idx].next;
865 }
866 return 0;
867}
868
869int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100870 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200871 struct hdr_ctx *ctx)
872{
873 return http_find_header2(name, strlen(name), sol, idx, ctx);
874}
875
Willy Tarreau68085d82010-01-18 14:54:04 +0100876/* Remove one value of a header. This only works on a <ctx> returned by one of
877 * the http_find_header functions. The value is removed, as well as surrounding
878 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100879 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100880 * message <msg>. The new index is returned. If it is zero, it means there is
881 * no more header, so any processing may stop. The ctx is always left in a form
882 * that can be handled by http_find_header2() to find next occurrence.
883 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100884int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100885{
886 int cur_idx = ctx->idx;
887 char *sol = ctx->line;
888 struct hdr_idx_elem *hdr;
889 int delta, skip_comma;
890
891 if (!cur_idx)
892 return 0;
893
894 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200895 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100896 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200897 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100898 http_msg_move_end(msg, delta);
899 idx->used--;
900 hdr->len = 0; /* unused entry */
901 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100902 if (idx->tail == ctx->idx)
903 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100904 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100905 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100906 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200907 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100908 return ctx->idx;
909 }
910
911 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200912 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
913 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100914 */
915
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200916 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200917 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200918 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100919 NULL, 0);
920 hdr->len += delta;
921 http_msg_move_end(msg, delta);
922 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200923 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100924 return ctx->idx;
925}
926
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100927/* This function handles a server error at the stream interface level. The
928 * stream interface is assumed to be already in a closed state. An optional
929 * message is copied into the input buffer, and an HTTP status code stored.
930 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100931 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200933static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100934 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200935{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100936 channel_auto_read(si_oc(si));
937 channel_abort(si_oc(si));
938 channel_auto_close(si_oc(si));
939 channel_erase(si_oc(si));
940 channel_auto_close(si_ic(si));
941 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100942 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200943 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100944 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200945 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200946 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200947 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200948 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200949 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200950}
951
Willy Tarreau87b09662015-04-03 00:22:06 +0200952/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100953 * and message.
954 */
955
Willy Tarreau87b09662015-04-03 00:22:06 +0200956struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100957{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200958 if (s->be->errmsg[msgnum].str)
959 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200960 else if (strm_fe(s)->errmsg[msgnum].str)
961 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100962 else
963 return &http_err_chunks[msgnum];
964}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200965
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100966void
967http_reply_and_close(struct stream *s, short status, struct chunk *msg)
968{
969 stream_int_retnclose(&s->si[0], msg);
970}
971
Willy Tarreau53b6c742006-12-17 13:37:46 +0100972/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200973 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
974 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100975 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100976enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100977{
978 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100979 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100980
981 m = ((unsigned)*str - 'A');
982
983 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100984 for (h = http_methods[m]; h->len > 0; h++) {
985 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100986 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100987 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100988 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100989 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100990 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200991 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100992}
993
Willy Tarreau21d2af32008-02-14 20:25:24 +0100994/* Parse the URI from the given transaction (which is assumed to be in request
995 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
996 * It is returned otherwise.
997 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +0200998char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100999{
1000 char *ptr, *end;
1001
Willy Tarreau9b28e032012-10-12 23:49:43 +02001002 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +01001003 end = ptr + txn->req.sl.rq.u_l;
1004
1005 if (ptr >= end)
1006 return NULL;
1007
1008 /* RFC2616, par. 5.1.2 :
1009 * Request-URI = "*" | absuri | abspath | authority
1010 */
1011
1012 if (*ptr == '*')
1013 return NULL;
1014
1015 if (isalpha((unsigned char)*ptr)) {
1016 /* this is a scheme as described by RFC3986, par. 3.1 */
1017 ptr++;
1018 while (ptr < end &&
1019 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1020 ptr++;
1021 /* skip '://' */
1022 if (ptr == end || *ptr++ != ':')
1023 return NULL;
1024 if (ptr == end || *ptr++ != '/')
1025 return NULL;
1026 if (ptr == end || *ptr++ != '/')
1027 return NULL;
1028 }
1029 /* skip [user[:passwd]@]host[:[port]] */
1030
1031 while (ptr < end && *ptr != '/')
1032 ptr++;
1033
1034 if (ptr == end)
1035 return NULL;
1036
1037 /* OK, we got the '/' ! */
1038 return ptr;
1039}
1040
William Lallemand65ad6e12014-01-31 15:08:02 +01001041/* Parse the URI from the given string and look for the "/" beginning the PATH.
1042 * If not found, return NULL. It is returned otherwise.
1043 */
1044static char *
1045http_get_path_from_string(char *str)
1046{
1047 char *ptr = str;
1048
1049 /* RFC2616, par. 5.1.2 :
1050 * Request-URI = "*" | absuri | abspath | authority
1051 */
1052
1053 if (*ptr == '*')
1054 return NULL;
1055
1056 if (isalpha((unsigned char)*ptr)) {
1057 /* this is a scheme as described by RFC3986, par. 3.1 */
1058 ptr++;
1059 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1060 ptr++;
1061 /* skip '://' */
1062 if (*ptr == '\0' || *ptr++ != ':')
1063 return NULL;
1064 if (*ptr == '\0' || *ptr++ != '/')
1065 return NULL;
1066 if (*ptr == '\0' || *ptr++ != '/')
1067 return NULL;
1068 }
1069 /* skip [user[:passwd]@]host[:[port]] */
1070
1071 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1072 ptr++;
1073
1074 if (*ptr == '\0' || *ptr == ' ')
1075 return NULL;
1076
1077 /* OK, we got the '/' ! */
1078 return ptr;
1079}
1080
Willy Tarreau71241ab2012-12-27 11:30:54 +01001081/* Returns a 302 for a redirectable request that reaches a server working in
1082 * in redirect mode. This may only be called just after the stream interface
1083 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1084 * follow normal proxy processing. NOTE: this function is designed to support
1085 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001086 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001087void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001088{
1089 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001090 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001091 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001092 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001093
1094 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001095 trash.len = strlen(HTTP_302);
1096 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001097
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001098 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001099
Willy Tarreauefb453c2008-10-26 20:49:47 +01001100 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001101 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001102 return;
1103
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001104 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001105 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001106 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1107 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001108 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001109
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001110 /* 3: add the request URI. Since it was already forwarded, we need
1111 * to temporarily rewind the buffer.
1112 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001113 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001114 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001115
Willy Tarreauefb453c2008-10-26 20:49:47 +01001116 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001117 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 +02001118
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001119 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001120
Willy Tarreauefb453c2008-10-26 20:49:47 +01001121 if (!path)
1122 return;
1123
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001124 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001125 return;
1126
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001127 memcpy(trash.str + trash.len, path, len);
1128 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001129
1130 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001131 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1132 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001133 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001134 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1135 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001136 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001137
1138 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001139 si_shutr(si);
1140 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001141 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001142 si->state = SI_ST_CLO;
1143
1144 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001145 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001146
1147 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001148 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001149 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001150}
1151
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001152/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001153 * that the server side is closed. Note that err_type is actually a
1154 * bitmask, where almost only aborts may be cumulated with other
1155 * values. We consider that aborted operations are more important
1156 * than timeouts or errors due to the fact that nobody else in the
1157 * logs might explain incomplete retries. All others should avoid
1158 * being cumulated. It should normally not be possible to have multiple
1159 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001160 * Note that connection errors appearing on the second request of a keep-alive
1161 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001162 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001163void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001164{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001165 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001166
1167 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001168 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001169 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001170 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001171 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001172 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001173 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001174 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001175 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001176 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001177 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001178 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001179 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001180 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001181 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001182 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001183 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001184 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001185 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1186 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001187 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001188 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001189 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001190 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001191 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001192 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001193 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001194 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001195}
1196
Willy Tarreau42250582007-04-01 01:30:43 +02001197extern const char sess_term_cond[8];
1198extern const char sess_fin_state[8];
1199extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001200struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001201struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001202struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001203struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001204
Willy Tarreau117f59e2007-03-04 18:17:17 +01001205/*
1206 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001207 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001208 */
1209void capture_headers(char *som, struct hdr_idx *idx,
1210 char **cap, struct cap_hdr *cap_hdr)
1211{
1212 char *eol, *sol, *col, *sov;
1213 int cur_idx;
1214 struct cap_hdr *h;
1215 int len;
1216
1217 sol = som + hdr_idx_first_pos(idx);
1218 cur_idx = hdr_idx_first_idx(idx);
1219
1220 while (cur_idx) {
1221 eol = sol + idx->v[cur_idx].len;
1222
1223 col = sol;
1224 while (col < eol && *col != ':')
1225 col++;
1226
1227 sov = col + 1;
1228 while (sov < eol && http_is_lws[(unsigned char)*sov])
1229 sov++;
1230
1231 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001232 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001233 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1234 if (cap[h->index] == NULL)
1235 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001236 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001237
1238 if (cap[h->index] == NULL) {
1239 Alert("HTTP capture : out of memory.\n");
1240 continue;
1241 }
1242
1243 len = eol - sov;
1244 if (len > h->len)
1245 len = h->len;
1246
1247 memcpy(cap[h->index], sov, len);
1248 cap[h->index][len]=0;
1249 }
1250 }
1251 sol = eol + idx->v[cur_idx].cr + 1;
1252 cur_idx = idx->v[cur_idx].next;
1253 }
1254}
1255
1256
Willy Tarreau42250582007-04-01 01:30:43 +02001257/* either we find an LF at <ptr> or we jump to <bad>.
1258 */
1259#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1260
1261/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1262 * otherwise to <http_msg_ood> with <state> set to <st>.
1263 */
1264#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1265 ptr++; \
1266 if (likely(ptr < end)) \
1267 goto good; \
1268 else { \
1269 state = (st); \
1270 goto http_msg_ood; \
1271 } \
1272 } while (0)
1273
1274
Willy Tarreaubaaee002006-06-26 02:48:02 +02001275/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001276 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001277 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1278 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1279 * will give undefined results.
1280 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1281 * and that msg->sol points to the beginning of the response.
1282 * If a complete line is found (which implies that at least one CR or LF is
1283 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1284 * returned indicating an incomplete line (which does not mean that parts have
1285 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1286 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1287 * upon next call.
1288 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001289 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001290 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1291 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001292 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001293 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001294const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001295 enum ht_state state, const char *ptr, const char *end,
1296 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001297{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001298 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001299
Willy Tarreau8973c702007-01-21 23:58:29 +01001300 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001301 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001302 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001303 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001304 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1305
1306 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001307 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001308 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1309 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001310 state = HTTP_MSG_ERROR;
1311 break;
1312
Willy Tarreau8973c702007-01-21 23:58:29 +01001313 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001314 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001315 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001316 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001317 goto http_msg_rpcode;
1318 }
1319 if (likely(HTTP_IS_SPHT(*ptr)))
1320 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1321 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001322 state = HTTP_MSG_ERROR;
1323 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001324
Willy Tarreau8973c702007-01-21 23:58:29 +01001325 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001326 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001327 if (likely(!HTTP_IS_LWS(*ptr)))
1328 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1329
1330 if (likely(HTTP_IS_SPHT(*ptr))) {
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 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1333 }
1334
1335 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001336 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001337 http_msg_rsp_reason:
1338 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001339 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001340 msg->sl.st.r_l = 0;
1341 goto http_msg_rpline_eol;
1342
Willy Tarreau8973c702007-01-21 23:58:29 +01001343 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001344 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001345 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001346 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001347 goto http_msg_rpreason;
1348 }
1349 if (likely(HTTP_IS_SPHT(*ptr)))
1350 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1351 /* so it's a CR/LF, so there is no reason phrase */
1352 goto http_msg_rsp_reason;
1353
Willy Tarreau8973c702007-01-21 23:58:29 +01001354 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001355 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001356 if (likely(!HTTP_IS_CRLF(*ptr)))
1357 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001358 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001359 http_msg_rpline_eol:
1360 /* We have seen the end of line. Note that we do not
1361 * necessarily have the \n yet, but at least we know that we
1362 * have EITHER \r OR \n, otherwise the response would not be
1363 * complete. We can then record the response length and return
1364 * to the caller which will be able to register it.
1365 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001366 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001367 return ptr;
1368
Willy Tarreau8973c702007-01-21 23:58:29 +01001369 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001370#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001371 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1372 exit(1);
1373#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001374 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001375 }
1376
1377 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001378 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001379 if (ret_state)
1380 *ret_state = state;
1381 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001382 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001383 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001384}
1385
Willy Tarreau8973c702007-01-21 23:58:29 +01001386/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 * This function parses a request line between <ptr> and <end>, starting with
1388 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1389 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1390 * will give undefined results.
1391 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1392 * and that msg->sol points to the beginning of the request.
1393 * If a complete line is found (which implies that at least one CR or LF is
1394 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1395 * returned indicating an incomplete line (which does not mean that parts have
1396 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1397 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1398 * upon next call.
1399 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001400 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1402 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001403 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001404 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001405const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001406 enum ht_state state, const char *ptr, const char *end,
1407 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001408{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001409 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001410
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001413 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 if (likely(HTTP_IS_TOKEN(*ptr)))
1415 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001416
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001418 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1420 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001421
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 if (likely(HTTP_IS_CRLF(*ptr))) {
1423 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001424 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001426 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001428 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001430 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 msg->sl.rq.v_l = 0;
1432 goto http_msg_rqline_eol;
1433 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001434 state = HTTP_MSG_ERROR;
1435 break;
1436
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001438 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001440 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 goto http_msg_rquri;
1442 }
1443 if (likely(HTTP_IS_SPHT(*ptr)))
1444 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1445 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1446 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001449 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001450 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001452
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001454 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1456 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001457
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001458 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001459 /* non-ASCII chars are forbidden unless option
1460 * accept-invalid-http-request is enabled in the frontend.
1461 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001462 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001463 if (msg->err_pos < -1)
1464 goto invalid_char;
1465 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001466 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001467 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1468 }
1469
1470 if (likely(HTTP_IS_CRLF(*ptr))) {
1471 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1472 goto http_msg_req09_uri_e;
1473 }
1474
1475 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001476 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001477 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001478 state = HTTP_MSG_ERROR;
1479 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001482 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001484 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001485 goto http_msg_rqver;
1486 }
1487 if (likely(HTTP_IS_SPHT(*ptr)))
1488 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1489 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1490 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001491
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001493 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001494 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001495 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001496
1497 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001498 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001499 http_msg_rqline_eol:
1500 /* We have seen the end of line. Note that we do not
1501 * necessarily have the \n yet, but at least we know that we
1502 * have EITHER \r OR \n, otherwise the request would not be
1503 * complete. We can then record the request length and return
1504 * to the caller which will be able to register it.
1505 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001506 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001507 return ptr;
1508 }
1509
1510 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001511 state = HTTP_MSG_ERROR;
1512 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001514 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001515#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1517 exit(1);
1518#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001519 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001521
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001523 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 if (ret_state)
1525 *ret_state = state;
1526 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001527 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001530
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001531/*
1532 * Returns the data from Authorization header. Function may be called more
1533 * than once so data is stored in txn->auth_data. When no header is found
1534 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001535 * searching again for something we are unable to find anyway. However, if
1536 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001537 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001538 */
1539
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001540/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1541 * set according to global.tune.bufsize.
1542 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001543char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001544
1545int
Willy Tarreau87b09662015-04-03 00:22:06 +02001546get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001547{
1548
Willy Tarreaueee5b512015-04-03 23:46:31 +02001549 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001550 struct chunk auth_method;
1551 struct hdr_ctx ctx;
1552 char *h, *p;
1553 int len;
1554
1555#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001556 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001557#endif
1558
1559 if (txn->auth.method == HTTP_AUTH_WRONG)
1560 return 0;
1561
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001562 txn->auth.method = HTTP_AUTH_WRONG;
1563
1564 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001565
1566 if (txn->flags & TX_USE_PX_CONN) {
1567 h = "Proxy-Authorization";
1568 len = strlen(h);
1569 } else {
1570 h = "Authorization";
1571 len = strlen(h);
1572 }
1573
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001574 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001575 return 0;
1576
1577 h = ctx.line + ctx.val;
1578
1579 p = memchr(h, ' ', ctx.vlen);
1580 if (!p || p == h)
1581 return 0;
1582
1583 chunk_initlen(&auth_method, h, 0, p-h);
1584 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1585
1586 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1587
1588 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001589 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001590
1591 if (len < 0)
1592 return 0;
1593
1594
1595 get_http_auth_buff[len] = '\0';
1596
1597 p = strchr(get_http_auth_buff, ':');
1598
1599 if (!p)
1600 return 0;
1601
1602 txn->auth.user = get_http_auth_buff;
1603 *p = '\0';
1604 txn->auth.pass = p+1;
1605
1606 txn->auth.method = HTTP_AUTH_BASIC;
1607 return 1;
1608 }
1609
1610 return 0;
1611}
1612
Willy Tarreau58f10d72006-12-04 02:26:12 +01001613
Willy Tarreau8973c702007-01-21 23:58:29 +01001614/*
1615 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001616 * depending on the initial msg->msg_state. The caller is responsible for
1617 * ensuring that the message does not wrap. The function can be preempted
1618 * everywhere when data are missing and recalled at the exact same location
1619 * with no information loss. The message may even be realigned between two
1620 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001621 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001622 * fields. Note that msg->sol will be initialized after completing the first
1623 * state, so that none of the msg pointers has to be initialized prior to the
1624 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001625 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001626void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001627{
Willy Tarreau3770f232013-12-07 00:01:53 +01001628 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001630 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001631
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001632 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001633 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001634 ptr = buf->p + msg->next;
1635 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001636
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637 if (unlikely(ptr >= end))
1638 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001641 /*
1642 * First, states that are specific to the response only.
1643 * We check them first so that request and headers are
1644 * closer to each other (accessed more often).
1645 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001646 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001647 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001648 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001649 /* we have a start of message, but we have to check
1650 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001651 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001652 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001653 if (unlikely(ptr != buf->p)) {
1654 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001655 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001656 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001657 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001658 }
Willy Tarreau26927362012-05-18 23:22:52 +02001659 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001660 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001661 hdr_idx_init(idx);
1662 state = HTTP_MSG_RPVER;
1663 goto http_msg_rpver;
1664 }
1665
1666 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1667 goto http_msg_invalid;
1668
1669 if (unlikely(*ptr == '\n'))
1670 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1671 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1672 /* stop here */
1673
Willy Tarreau8973c702007-01-21 23:58:29 +01001674 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001675 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001676 EXPECT_LF_HERE(ptr, http_msg_invalid);
1677 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1678 /* stop here */
1679
Willy Tarreau8973c702007-01-21 23:58:29 +01001680 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001681 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001682 case HTTP_MSG_RPVER_SP:
1683 case HTTP_MSG_RPCODE:
1684 case HTTP_MSG_RPCODE_SP:
1685 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001686 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001687 state, ptr, end,
1688 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001689 if (unlikely(!ptr))
1690 return;
1691
1692 /* we have a full response and we know that we have either a CR
1693 * or an LF at <ptr>.
1694 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001695 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1696
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001697 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001698 if (likely(*ptr == '\r'))
1699 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1700 goto http_msg_rpline_end;
1701
Willy Tarreau8973c702007-01-21 23:58:29 +01001702 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001703 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001704 /* msg->sol must point to the first of CR or LF. */
1705 EXPECT_LF_HERE(ptr, http_msg_invalid);
1706 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1707 /* stop here */
1708
1709 /*
1710 * Second, states that are specific to the request only
1711 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001712 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001713 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001714 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001715 /* we have a start of message, but we have to check
1716 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001717 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001718 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001719 if (likely(ptr != buf->p)) {
1720 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001721 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001722 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001723 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 }
Willy Tarreau26927362012-05-18 23:22:52 +02001725 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001726 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001727 state = HTTP_MSG_RQMETH;
1728 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001730
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001731 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1732 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001733
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 if (unlikely(*ptr == '\n'))
1735 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1736 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001737 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001738
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001740 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001741 EXPECT_LF_HERE(ptr, http_msg_invalid);
1742 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001743 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001744
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001745 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001746 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001747 case HTTP_MSG_RQMETH_SP:
1748 case HTTP_MSG_RQURI:
1749 case HTTP_MSG_RQURI_SP:
1750 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001751 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001752 state, ptr, end,
1753 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 if (unlikely(!ptr))
1755 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001756
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 /* we have a full request and we know that we have either a CR
1758 * or an LF at <ptr>.
1759 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001760 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001761
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001762 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001763 if (likely(*ptr == '\r'))
1764 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001765 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001766
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001767 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001768 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001769 /* check for HTTP/0.9 request : no version information available.
1770 * msg->sol must point to the first of CR or LF.
1771 */
1772 if (unlikely(msg->sl.rq.v_l == 0))
1773 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001774
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001775 EXPECT_LF_HERE(ptr, http_msg_invalid);
1776 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001777 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001778
Willy Tarreau8973c702007-01-21 23:58:29 +01001779 /*
1780 * Common states below
1781 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001782 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001783 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001784 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001785 if (likely(!HTTP_IS_CRLF(*ptr))) {
1786 goto http_msg_hdr_name;
1787 }
1788
1789 if (likely(*ptr == '\r'))
1790 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1791 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001792
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001793 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001794 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001795 /* assumes msg->sol points to the first char */
1796 if (likely(HTTP_IS_TOKEN(*ptr)))
1797 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001798
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001799 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001800 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001801
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001802 if (likely(msg->err_pos < -1) || *ptr == '\n')
1803 goto http_msg_invalid;
1804
1805 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001806 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001807
1808 /* and we still accept this non-token character */
1809 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001810
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001811 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001812 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001813 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001814 if (likely(HTTP_IS_SPHT(*ptr)))
1815 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001816
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001818 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001819
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001820 if (likely(!HTTP_IS_CRLF(*ptr))) {
1821 goto http_msg_hdr_val;
1822 }
1823
1824 if (likely(*ptr == '\r'))
1825 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1826 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001827
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001828 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001829 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001830 EXPECT_LF_HERE(ptr, http_msg_invalid);
1831 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001832
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001833 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001834 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001835 if (likely(HTTP_IS_SPHT(*ptr))) {
1836 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001837 for (; buf->p + msg->sov < ptr; msg->sov++)
1838 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001839 goto http_msg_hdr_l1_sp;
1840 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001841 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001842 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001843 goto http_msg_complete_header;
1844
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001845 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001846 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001847 /* assumes msg->sol points to the first char, and msg->sov
1848 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001849 */
1850 if (likely(!HTTP_IS_CRLF(*ptr)))
1851 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001852
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001853 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001854 /* Note: we could also copy eol into ->eoh so that we have the
1855 * real header end in case it ends with lots of LWS, but is this
1856 * really needed ?
1857 */
1858 if (likely(*ptr == '\r'))
1859 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1860 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001861
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001862 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001863 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001864 EXPECT_LF_HERE(ptr, http_msg_invalid);
1865 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001866
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001868 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001869 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1870 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001871 for (; buf->p + msg->eol < ptr; msg->eol++)
1872 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001873 goto http_msg_hdr_val;
1874 }
1875 http_msg_complete_header:
1876 /*
1877 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001878 * Assumes msg->sol points to the first char, msg->sov points
1879 * to the first character of the value and msg->eol to the
1880 * first CR or LF so we know how the line ends. We insert last
1881 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001882 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001883 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001884 idx, idx->tail) < 0))
1885 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001886
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001887 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001888 if (likely(!HTTP_IS_CRLF(*ptr))) {
1889 goto http_msg_hdr_name;
1890 }
1891
1892 if (likely(*ptr == '\r'))
1893 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1894 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001895
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001897 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001898 /* Assumes msg->sol points to the first of either CR or LF.
1899 * Sets ->sov and ->next to the total header length, ->eoh to
1900 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1901 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001902 EXPECT_LF_HERE(ptr, http_msg_invalid);
1903 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001904 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001905 msg->eoh = msg->sol;
1906 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001907 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001908 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001909 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001910
1911 case HTTP_MSG_ERROR:
1912 /* this may only happen if we call http_msg_analyser() twice with an error */
1913 break;
1914
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001915 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001916#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001917 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1918 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001919#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001920 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001921 }
1922 http_msg_ood:
1923 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001924 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001925 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001926 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001927
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001928 http_msg_invalid:
1929 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001930 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001931 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001932 return;
1933}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001934
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001935/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1936 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1937 * nothing is done and 1 is returned.
1938 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001939static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001940{
1941 int delta;
1942 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001943 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001944
1945 if (msg->sl.rq.v_l != 0)
1946 return 1;
1947
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001948 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1949 if (txn->meth != HTTP_METH_GET)
1950 return 0;
1951
Willy Tarreau9b28e032012-10-12 23:49:43 +02001952 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001953 delta = 0;
1954
1955 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001956 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1957 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001958 }
1959 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001960 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001961 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001962 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001963 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001964 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001965 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001966 NULL, NULL);
1967 if (unlikely(!cur_end))
1968 return 0;
1969
1970 /* we have a full HTTP/1.0 request now and we know that
1971 * we have either a CR or an LF at <ptr>.
1972 */
1973 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1974 return 1;
1975}
1976
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001977/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001978 * and "keep-alive" values. If we already know that some headers may safely
1979 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001980 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1981 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001982 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001983 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1984 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1985 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001986 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001987 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001988void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001989{
Willy Tarreau5b154472009-12-21 20:11:07 +01001990 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001991 const char *hdr_val = "Connection";
1992 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001993
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001994 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001995 return;
1996
Willy Tarreau88d349d2010-01-25 12:15:43 +01001997 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1998 hdr_val = "Proxy-Connection";
1999 hdr_len = 16;
2000 }
2001
Willy Tarreau5b154472009-12-21 20:11:07 +01002002 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002003 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002004 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002005 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2006 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002007 if (to_del & 2)
2008 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002009 else
2010 txn->flags |= TX_CON_KAL_SET;
2011 }
2012 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2013 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002014 if (to_del & 1)
2015 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002016 else
2017 txn->flags |= TX_CON_CLO_SET;
2018 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002019 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2020 txn->flags |= TX_HDR_CONN_UPG;
2021 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002022 }
2023
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002024 txn->flags |= TX_HDR_CONN_PRS;
2025 return;
2026}
Willy Tarreau5b154472009-12-21 20:11:07 +01002027
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002028/* Apply desired changes on the Connection: header. Values may be removed and/or
2029 * added depending on the <wanted> flags, which are exclusively composed of
2030 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2031 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2032 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002033void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002034{
2035 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002036 const char *hdr_val = "Connection";
2037 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002038
2039 ctx.idx = 0;
2040
Willy Tarreau88d349d2010-01-25 12:15:43 +01002041
2042 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2043 hdr_val = "Proxy-Connection";
2044 hdr_len = 16;
2045 }
2046
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002047 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002048 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002049 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2050 if (wanted & TX_CON_KAL_SET)
2051 txn->flags |= TX_CON_KAL_SET;
2052 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002053 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002054 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002055 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2056 if (wanted & TX_CON_CLO_SET)
2057 txn->flags |= TX_CON_CLO_SET;
2058 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002059 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002060 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002061 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002062
2063 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2064 return;
2065
2066 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2067 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002068 hdr_val = "Connection: close";
2069 hdr_len = 17;
2070 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2071 hdr_val = "Proxy-Connection: close";
2072 hdr_len = 23;
2073 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002074 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002075 }
2076
2077 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2078 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002079 hdr_val = "Connection: keep-alive";
2080 hdr_len = 22;
2081 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2082 hdr_val = "Proxy-Connection: keep-alive";
2083 hdr_len = 28;
2084 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002085 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002086 }
2087 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002088}
2089
Willy Tarreauc24715e2014-04-17 15:21:20 +02002090/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
2091 * the first byte of data after the chunk size, so that we know we can forward
2092 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
2093 * the chunk size. That way it is always possible to differentiate between the
2094 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01002095 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002096 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002097 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002098static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002099{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002100 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002101 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002102 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002103 const char *end = buf->data + buf->size;
2104 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002105 unsigned int chunk = 0;
2106
2107 /* The chunk size is in the following form, though we are only
2108 * interested in the size and CRLF :
2109 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2110 */
2111 while (1) {
2112 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002113 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002114 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002115 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002116 if (c < 0) /* not a hex digit anymore */
2117 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002118 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002119 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002120 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002121 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002122 chunk = (chunk << 4) + c;
2123 }
2124
Willy Tarreaud98cf932009-12-27 22:54:55 +01002125 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002126 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002127 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002128
2129 while (http_is_spht[(unsigned char)*ptr]) {
2130 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002131 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002132 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002133 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002134 }
2135
Willy Tarreaud98cf932009-12-27 22:54:55 +01002136 /* Up to there, we know that at least one byte is present at *ptr. Check
2137 * for the end of chunk size.
2138 */
2139 while (1) {
2140 if (likely(HTTP_IS_CRLF(*ptr))) {
2141 /* we now have a CR or an LF at ptr */
2142 if (likely(*ptr == '\r')) {
2143 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002144 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002145 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002146 return 0;
2147 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002148
Willy Tarreaud98cf932009-12-27 22:54:55 +01002149 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002150 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002151 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002152 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002153 /* done */
2154 break;
2155 }
2156 else if (*ptr == ';') {
2157 /* chunk extension, ends at next CRLF */
2158 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002159 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002160 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002161 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002162
2163 while (!HTTP_IS_CRLF(*ptr)) {
2164 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002165 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002166 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002167 return 0;
2168 }
2169 /* we have a CRLF now, loop above */
2170 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002171 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002172 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002173 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002174 }
2175
Willy Tarreaud98cf932009-12-27 22:54:55 +01002176 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002177 * which may or may not be present. We save that into ->next,
2178 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002179 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002180 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002181 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002182 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002183 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002184 msg->chunk_len = chunk;
2185 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002186 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002187 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002188 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002189 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002190 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002191}
2192
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002193/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002194 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002195 * the trailers is found, it is automatically scheduled to be forwarded,
2196 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2197 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002198 * except maybe msg->next if it could parse some lines, and returns zero.
2199 * If a parse error is encountered, the function returns < 0 and does not
2200 * change anything except maybe msg->next. Note that the message must
2201 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002202 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002203 * forwarding, and that msg->next exactly matches the length of trailers
2204 * already parsed and not forwarded. It is also important to note that this
2205 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002206 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002207static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002208{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002209 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002210
Willy Tarreaua458b672012-03-05 11:17:50 +01002211 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002212 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002213 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002214 const char *ptr = b_ptr(buf, msg->next);
2215 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002216 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002217
2218 /* scan current line and stop at LF or CRLF */
2219 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002220 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002221 return 0;
2222
2223 if (*ptr == '\n') {
2224 if (!p1)
2225 p1 = ptr;
2226 p2 = ptr;
2227 break;
2228 }
2229
2230 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002231 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002232 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002233 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002234 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002235 p1 = ptr;
2236 }
2237
2238 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002239 if (ptr >= buf->data + buf->size)
2240 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002241 }
2242
2243 /* after LF; point to beginning of next line */
2244 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002245 if (p2 >= buf->data + buf->size)
2246 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002247
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002248 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002249 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002250 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002251
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002252 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002253 /* LF/CRLF at beginning of line => end of trailers at p2.
2254 * Everything was scheduled for forwarding, there's nothing
2255 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002256 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002257 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002258 msg->msg_state = HTTP_MSG_DONE;
2259 return 1;
2260 }
2261 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002262 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002263 }
2264}
2265
Willy Tarreauc24715e2014-04-17 15:21:20 +02002266/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2267 * or a possible LF alone at the end of a chunk. It automatically adjusts
2268 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002269 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002270 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2271 * not enough data are available, the function does not change anything and
2272 * returns zero. If a parse error is encountered, the function returns < 0 and
2273 * does not change anything. Note: this function is designed to parse wrapped
2274 * CRLF at the end of the buffer.
2275 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002276static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002277{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002278 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002279 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002280 int bytes;
2281
2282 /* NB: we'll check data availabilty at the end. It's not a
2283 * problem because whatever we match first will be checked
2284 * against the correct length.
2285 */
2286 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002287 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002288 if (*ptr == '\r') {
2289 bytes++;
2290 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002291 if (ptr >= buf->data + buf->size)
2292 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002293 }
2294
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002295 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002296 return 0;
2297
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002298 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002299 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002300 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002301 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002302
2303 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002304 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002305 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002306 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002307 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002308 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2309 return 1;
2310}
Willy Tarreau5b154472009-12-21 20:11:07 +01002311
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002312/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2313 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2314 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2315 * Unparsable qvalues return 1000 as "q=1.000".
2316 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002317int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002318{
2319 int q = 1000;
2320
Willy Tarreau506c69a2014-07-08 00:59:48 +02002321 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002322 goto out;
2323 q = (*qvalue++ - '0') * 1000;
2324
2325 if (*qvalue++ != '.')
2326 goto out;
2327
Willy Tarreau506c69a2014-07-08 00:59:48 +02002328 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002329 goto out;
2330 q += (*qvalue++ - '0') * 100;
2331
Willy Tarreau506c69a2014-07-08 00:59:48 +02002332 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002333 goto out;
2334 q += (*qvalue++ - '0') * 10;
2335
Willy Tarreau506c69a2014-07-08 00:59:48 +02002336 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002337 goto out;
2338 q += (*qvalue++ - '0') * 1;
2339 out:
2340 if (q > 1000)
2341 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002342 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002343 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002344 return q;
2345}
William Lallemand82fe75c2012-10-23 10:25:10 +02002346
2347/*
2348 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002349 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002350int select_compression_request_header(struct stream *s, struct buffer *req)
William Lallemand82fe75c2012-10-23 10:25:10 +02002351{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002352 struct http_txn *txn = s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002353 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002354 struct hdr_ctx ctx;
2355 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002356 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002357
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002358 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2359 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002360 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2361 */
2362 ctx.idx = 0;
2363 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2364 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002365 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2366 (ctx.vlen < 31 ||
2367 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2368 ctx.line[ctx.val + 30] < '6' ||
2369 (ctx.line[ctx.val + 30] == '6' &&
2370 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2371 s->comp_algo = NULL;
2372 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002373 }
2374
William Lallemand82fe75c2012-10-23 10:25:10 +02002375 /* search for the algo in the backend in priority or the frontend */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002376 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 +01002377 int best_q = 0;
2378
William Lallemand82fe75c2012-10-23 10:25:10 +02002379 ctx.idx = 0;
2380 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002381 const char *qval;
2382 int q;
2383 int toklen;
2384
2385 /* try to isolate the token from the optional q-value */
2386 toklen = 0;
2387 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2388 toklen++;
2389
2390 qval = ctx.line + ctx.val + toklen;
2391 while (1) {
2392 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2393 qval++;
2394
2395 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2396 qval = NULL;
2397 break;
2398 }
2399 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002400
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002401 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2402 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002403
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002404 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2405 qval = NULL;
2406 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002407 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002408 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2409 break;
2410
2411 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2412 qval++;
2413 }
2414
2415 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002416 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002417
2418 if (q <= best_q)
2419 continue;
2420
2421 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2422 if (*(ctx.line + ctx.val) == '*' ||
Willy Tarreau615105e2015-03-28 16:40:46 +01002423 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002424 s->comp_algo = comp_algo;
2425 best_q = q;
2426 break;
2427 }
2428 }
2429 }
2430 }
2431
2432 /* remove all occurrences of the header when "compression offload" is set */
2433 if (s->comp_algo) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002434 if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002435 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2436 ctx.idx = 0;
2437 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2438 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002439 }
2440 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002441 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002442 }
2443
2444 /* identity is implicit does not require headers */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002445 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 +02002446 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
Willy Tarreau615105e2015-03-28 16:40:46 +01002447 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002448 s->comp_algo = comp_algo;
2449 return 1;
2450 }
2451 }
2452 }
2453
2454 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002455 return 0;
2456}
2457
2458/*
2459 * Selects a comression algorithm depending of the server response.
2460 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002461int select_compression_response_header(struct stream *s, struct buffer *res)
William Lallemand82fe75c2012-10-23 10:25:10 +02002462{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002463 struct http_txn *txn = s->txn;
William Lallemand82fe75c2012-10-23 10:25:10 +02002464 struct http_msg *msg = &txn->rsp;
2465 struct hdr_ctx ctx;
2466 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002467
2468 /* no common compression algorithm was found in request header */
2469 if (s->comp_algo == NULL)
2470 goto fail;
2471
2472 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002473 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002474 goto fail;
2475
Jesse Hathaway2468d4e2015-03-06 20:16:15 +00002476 /* compress 200,201,202,203 responses only */
2477 if ((txn->status != 200) &&
2478 (txn->status != 201) &&
2479 (txn->status != 202) &&
2480 (txn->status != 203))
William Lallemandd3002612012-11-26 14:34:47 +01002481 goto fail;
2482
William Lallemand82fe75c2012-10-23 10:25:10 +02002483 /* Content-Length is null */
2484 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2485 goto fail;
2486
2487 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002488 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002489 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2490 goto fail;
2491
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002492 /* no compression when Cache-Control: no-transform is present in the message */
2493 ctx.idx = 0;
2494 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2495 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2496 goto fail;
2497 }
2498
William Lallemand82fe75c2012-10-23 10:25:10 +02002499 comp_type = NULL;
2500
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002501 /* we don't want to compress multipart content-types, nor content-types that are
2502 * not listed in the "compression type" directive if any. If no content-type was
2503 * found but configuration requires one, we don't compress either. Backend has
2504 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002505 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002506 ctx.idx = 0;
2507 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2508 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2509 goto fail;
2510
2511 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002512 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002513 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002514 if (ctx.vlen >= comp_type->name_len &&
2515 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002516 /* this Content-Type should be compressed */
2517 break;
2518 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002519 /* this Content-Type should not be compressed */
2520 if (comp_type == NULL)
2521 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002522 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002523 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002524 else { /* no content-type header */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002525 if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->comp->types))
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002526 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002527 }
2528
William Lallemandd85f9172012-11-09 17:05:39 +01002529 /* limit compression rate */
2530 if (global.comp_rate_lim > 0)
2531 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2532 goto fail;
2533
William Lallemand072a2bf2012-11-20 17:01:01 +01002534 /* limit cpu usage */
2535 if (idle_pct < compress_min_idle)
2536 goto fail;
2537
William Lallemand4c49fae2012-11-07 15:00:23 +01002538 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002539 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002540 goto fail;
2541
Willy Tarreaue7dff022015-04-03 01:14:29 +02002542 s->flags |= SF_COMP_READY;
William Lallemandec3e3892012-11-12 17:02:18 +01002543
William Lallemand82fe75c2012-10-23 10:25:10 +02002544 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002545 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002546 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2547 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2548
2549 /* add Transfer-Encoding header */
2550 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2551 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2552
2553 /*
2554 * Add Content-Encoding header when it's not identity encoding.
2555 * RFC 2616 : Identity encoding: This content-coding is used only in the
2556 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2557 * header.
2558 */
Willy Tarreau615105e2015-03-28 16:40:46 +01002559 if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002560 trash.len = 18;
2561 memcpy(trash.str, "Content-Encoding: ", trash.len);
Willy Tarreau615105e2015-03-28 16:40:46 +01002562 memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len);
2563 trash.len += s->comp_algo->ua_name_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002564 trash.str[trash.len] = '\0';
2565 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002566 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002567 return 1;
2568
2569fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002570 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002571 return 0;
2572}
2573
Willy Tarreau87b09662015-04-03 00:22:06 +02002574void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002575{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002576 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002577 int tmp = TX_CON_WANT_KAL;
2578
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002579 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2580 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002581 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2582 tmp = TX_CON_WANT_TUN;
2583
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002584 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002585 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2586 tmp = TX_CON_WANT_TUN;
2587 }
2588
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002589 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002590 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2591 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002592 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002593 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2594 tmp = TX_CON_WANT_CLO;
2595 else
2596 tmp = TX_CON_WANT_SCL;
2597 }
2598
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002599 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002600 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2601 tmp = TX_CON_WANT_CLO;
2602
2603 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2604 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2605
2606 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2607 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2608 /* parse the Connection header and possibly clean it */
2609 int to_del = 0;
2610 if ((msg->flags & HTTP_MSGF_VER_11) ||
2611 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002612 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002613 to_del |= 2; /* remove "keep-alive" */
2614 if (!(msg->flags & HTTP_MSGF_VER_11))
2615 to_del |= 1; /* remove "close" */
2616 http_parse_connection_header(txn, msg, to_del);
2617 }
2618
2619 /* check if client or config asks for explicit close in KAL/SCL */
2620 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2621 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2622 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2623 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2624 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002625 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002626 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2627}
William Lallemand82fe75c2012-10-23 10:25:10 +02002628
Willy Tarreaud787e662009-07-07 10:14:51 +02002629/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2630 * processing can continue on next analysers, or zero if it either needs more
2631 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002632 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002633 * when it has nothing left to do, and may remove any analyser when it wants to
2634 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002635 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002636int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002637{
Willy Tarreau59234e92008-11-30 23:51:27 +01002638 /*
2639 * We will parse the partial (or complete) lines.
2640 * We will check the request syntax, and also join multi-line
2641 * headers. An index of all the lines will be elaborated while
2642 * parsing.
2643 *
2644 * For the parsing, we use a 28 states FSM.
2645 *
2646 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002647 * req->buf->p = beginning of request
2648 * req->buf->p + msg->eoh = end of processed headers / start of current one
2649 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002650 * msg->eol = end of current header or line (LF or CRLF)
2651 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002652 *
2653 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002654 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002655 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2656 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002658
Willy Tarreau59234e92008-11-30 23:51:27 +01002659 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002660 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002661 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002662 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002663 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002664
Willy Tarreau87b09662015-04-03 00:22:06 +02002665 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 +01002666 now_ms, __FUNCTION__,
2667 s,
2668 req,
2669 req->rex, req->wex,
2670 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002671 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002672 req->analysers);
2673
Willy Tarreau52a0c602009-08-16 22:45:38 +02002674 /* we're speaking HTTP here, so let's speak HTTP to the client */
2675 s->srv_error = http_return_srv_error;
2676
Willy Tarreau83e3af02009-12-28 17:39:57 +01002677 /* There's a protected area at the end of the buffer for rewriting
2678 * purposes. We don't want to start to parse the request if the
2679 * protected area is affected, because we may have to move processed
2680 * data later, which is much more complicated.
2681 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002682 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002683 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002684 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002685 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002686 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002687 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002688 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002689 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002690 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002691 return 0;
2692 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002693 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2694 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2695 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002696 }
2697
Willy Tarreau065e8332010-01-08 00:30:20 +01002698 /* Note that we have the same problem with the response ; we
2699 * may want to send a redirect, error or anything which requires
2700 * some spare space. So we'll ensure that we have at least
2701 * maxrewrite bytes available in the response buffer before
2702 * processing that one. This will only affect pipelined
2703 * keep-alive requests.
2704 */
2705 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002706 unlikely(!channel_is_rewritable(&s->res) ||
2707 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2708 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2709 if (s->res.buf->o) {
2710 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002711 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002712 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002713 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002714 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2715 s->res.flags |= CF_WAKE_WRITE;
2716 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002717 return 0;
2718 }
2719 }
2720
Willy Tarreau9b28e032012-10-12 23:49:43 +02002721 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002722 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002723 }
2724
Willy Tarreau59234e92008-11-30 23:51:27 +01002725 /* 1: we might have to print this header in debug mode */
2726 if (unlikely((global.mode & MODE_DEBUG) &&
2727 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002728 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002729 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002730
Willy Tarreau9b28e032012-10-12 23:49:43 +02002731 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002732 /* this is a bit complex : in case of error on the request line,
2733 * we know that rq.l is still zero, so we display only the part
2734 * up to the end of the line (truncated by debug_hdr).
2735 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002736 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002737 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002738
Willy Tarreau59234e92008-11-30 23:51:27 +01002739 sol += hdr_idx_first_pos(&txn->hdr_idx);
2740 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002741
Willy Tarreau59234e92008-11-30 23:51:27 +01002742 while (cur_idx) {
2743 eol = sol + txn->hdr_idx.v[cur_idx].len;
2744 debug_hdr("clihdr", s, sol, eol);
2745 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2746 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002747 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002748 }
2749
Willy Tarreau58f10d72006-12-04 02:26:12 +01002750
Willy Tarreau59234e92008-11-30 23:51:27 +01002751 /*
2752 * Now we quickly check if we have found a full valid request.
2753 * If not so, we check the FD and buffer states before leaving.
2754 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002755 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002756 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002757 * on a keep-alive stream, if we encounter and error, close, t/o,
2758 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002759 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002760 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002761 * Last, we may increase some tracked counters' http request errors on
2762 * the cases that are deliberately the client's fault. For instance,
2763 * a timeout or connection reset is not counted as an error. However
2764 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002765 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002766
Willy Tarreau655dce92009-11-08 13:10:58 +01002767 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002768 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002769 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002770 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002771 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002772 stream_inc_http_req_ctr(s);
2773 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002774 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002775 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002776 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002777
Willy Tarreau59234e92008-11-30 23:51:27 +01002778 /* 1: Since we are in header mode, if there's no space
2779 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002780 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002781 * must terminate it now.
2782 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002783 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002784 /* FIXME: check if URI is set and return Status
2785 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002786 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002787 stream_inc_http_req_ctr(s);
2788 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002789 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002790 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002791 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002792 goto return_bad_req;
2793 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002794
Willy Tarreau59234e92008-11-30 23:51:27 +01002795 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002796 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002797 if (!(s->flags & SF_ERR_MASK))
2798 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002799
Willy Tarreaufcffa692010-01-10 14:21:19 +01002800 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002801 goto failed_keep_alive;
2802
Willy Tarreau0f228a02015-05-01 15:37:53 +02002803 if (sess->fe->options & PR_O_IGNORE_PRB)
2804 goto failed_keep_alive;
2805
Willy Tarreau59234e92008-11-30 23:51:27 +01002806 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002807 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002808 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002809 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002810 }
2811
Willy Tarreaudc979f22012-12-04 10:39:01 +01002812 txn->status = 400;
Willy Tarreau59234e92008-11-30 23:51:27 +01002813 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002814 http_reply_and_close(s, txn->status, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002815 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002816
Willy Tarreau87b09662015-04-03 00:22:06 +02002817 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002818 proxy_inc_fe_req_ctr(sess->fe);
2819 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002820 if (sess->listener->counters)
2821 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002822
Willy Tarreaue7dff022015-04-03 01:14:29 +02002823 if (!(s->flags & SF_FINST_MASK))
2824 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002825 return 0;
2826 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002827
Willy Tarreau59234e92008-11-30 23:51:27 +01002828 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002829 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002830 if (!(s->flags & SF_ERR_MASK))
2831 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002832
Willy Tarreaufcffa692010-01-10 14:21:19 +01002833 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002834 goto failed_keep_alive;
2835
Willy Tarreau0f228a02015-05-01 15:37:53 +02002836 if (sess->fe->options & PR_O_IGNORE_PRB)
2837 goto failed_keep_alive;
2838
Willy Tarreau59234e92008-11-30 23:51:27 +01002839 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002840 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002841 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002842 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002843 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002844 txn->status = 408;
Willy Tarreau59234e92008-11-30 23:51:27 +01002845 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002846 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002847 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002848
Willy Tarreau87b09662015-04-03 00:22:06 +02002849 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002850 proxy_inc_fe_req_ctr(sess->fe);
2851 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002852 if (sess->listener->counters)
2853 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002854
Willy Tarreaue7dff022015-04-03 01:14:29 +02002855 if (!(s->flags & SF_FINST_MASK))
2856 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002857 return 0;
2858 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002859
Willy Tarreau59234e92008-11-30 23:51:27 +01002860 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002861 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002862 if (!(s->flags & SF_ERR_MASK))
2863 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002864
Willy Tarreaufcffa692010-01-10 14:21:19 +01002865 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002866 goto failed_keep_alive;
2867
Willy Tarreau0f228a02015-05-01 15:37:53 +02002868 if (sess->fe->options & PR_O_IGNORE_PRB)
2869 goto failed_keep_alive;
2870
Willy Tarreau4076a152009-04-02 15:18:36 +02002871 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002872 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002873 txn->status = 400;
Willy Tarreau59234e92008-11-30 23:51:27 +01002874 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002875 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002876 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002877
Willy Tarreau87b09662015-04-03 00:22:06 +02002878 stream_inc_http_err_ctr(s);
2879 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002880 proxy_inc_fe_req_ctr(sess->fe);
2881 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002882 if (sess->listener->counters)
2883 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002884
Willy Tarreaue7dff022015-04-03 01:14:29 +02002885 if (!(s->flags & SF_FINST_MASK))
2886 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002887 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002888 }
2889
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002890 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002891 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002892 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002893#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002894 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2895 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002896 /* We need more data, we have to re-enable quick-ack in case we
2897 * previously disabled it, otherwise we might cause the client
2898 * to delay next data.
2899 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002900 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002901 }
2902#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002903
Willy Tarreaufcffa692010-01-10 14:21:19 +01002904 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2905 /* If the client starts to talk, let's fall back to
2906 * request timeout processing.
2907 */
2908 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002909 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002910 }
2911
Willy Tarreau59234e92008-11-30 23:51:27 +01002912 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002913 if (!tick_isset(req->analyse_exp)) {
2914 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2915 (txn->flags & TX_WAIT_NEXT_RQ) &&
2916 tick_isset(s->be->timeout.httpka))
2917 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2918 else
2919 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2920 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002921
Willy Tarreau59234e92008-11-30 23:51:27 +01002922 /* we're not ready yet */
2923 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002924
2925 failed_keep_alive:
2926 /* Here we process low-level errors for keep-alive requests. In
2927 * short, if the request is not the first one and it experiences
2928 * a timeout, read error or shutdown, we just silently close so
2929 * that the client can try again.
2930 */
2931 txn->status = 0;
2932 msg->msg_state = HTTP_MSG_RQBEFORE;
2933 req->analysers = 0;
2934 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002935 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002936 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002937 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002938 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002939 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002940
Willy Tarreaud787e662009-07-07 10:14:51 +02002941 /* OK now we have a complete HTTP request with indexed headers. Let's
2942 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002943 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002944 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002945 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002946 * byte after the last LF. msg->sov points to the first byte of data.
2947 * msg->eol cannot be trusted because it may have been left uninitialized
2948 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002949 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002950
Willy Tarreau87b09662015-04-03 00:22:06 +02002951 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002952 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002953
Willy Tarreaub16a5742010-01-10 14:46:16 +01002954 if (txn->flags & TX_WAIT_NEXT_RQ) {
2955 /* kill the pending keep-alive timeout */
2956 txn->flags &= ~TX_WAIT_NEXT_RQ;
2957 req->analyse_exp = TICK_ETERNITY;
2958 }
2959
2960
Willy Tarreaud787e662009-07-07 10:14:51 +02002961 /* Maybe we found in invalid header name while we were configured not
2962 * to block on that, so we have to capture it now.
2963 */
2964 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002965 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002966
Willy Tarreau59234e92008-11-30 23:51:27 +01002967 /*
2968 * 1: identify the method
2969 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002970 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002971
2972 /* we can make use of server redirect on GET and HEAD */
2973 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002974 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002975
Willy Tarreau59234e92008-11-30 23:51:27 +01002976 /*
2977 * 2: check if the URI matches the monitor_uri.
2978 * We have to do this for every request which gets in, because
2979 * the monitor-uri is defined by the frontend.
2980 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002981 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2982 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002983 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002984 sess->fe->monitor_uri,
2985 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002986 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002987 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002988 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002989 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002990
Willy Tarreaue7dff022015-04-03 01:14:29 +02002991 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002992 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002993
Willy Tarreau59234e92008-11-30 23:51:27 +01002994 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002995 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002996 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002997
Willy Tarreau59234e92008-11-30 23:51:27 +01002998 ret = acl_pass(ret);
2999 if (cond->pol == ACL_COND_UNLESS)
3000 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01003001
Willy Tarreau59234e92008-11-30 23:51:27 +01003002 if (ret) {
3003 /* we fail this request, let's return 503 service unavail */
3004 txn->status = 503;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003005 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02003006 if (!(s->flags & SF_ERR_MASK))
3007 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01003008 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01003009 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003010 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01003011
Willy Tarreau59234e92008-11-30 23:51:27 +01003012 /* nothing to fail, let's reply normaly */
3013 txn->status = 200;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003014 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02003015 if (!(s->flags & SF_ERR_MASK))
3016 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01003017 goto return_prx_cond;
3018 }
3019
3020 /*
3021 * 3: Maybe we have to copy the original REQURI for the logs ?
3022 * Note: we cannot log anymore if the request has been
3023 * classified as invalid.
3024 */
3025 if (unlikely(s->logs.logwait & LW_REQ)) {
3026 /* we have a complete HTTP request that we must log */
3027 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
3028 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003029
Willy Tarreau59234e92008-11-30 23:51:27 +01003030 if (urilen >= REQURI_LEN)
3031 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02003032 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01003033 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003034
Willy Tarreaud79a3b22012-12-28 09:40:16 +01003035 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01003036 s->do_log(s);
3037 } else {
3038 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003039 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003040 }
Willy Tarreau06619262006-12-17 08:37:22 +01003041
Willy Tarreau91852eb2015-05-01 13:26:00 +02003042 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
3043 * exactly one digit "." one digit. This check may be disabled using
3044 * option accept-invalid-http-request.
3045 */
3046 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
3047 if (msg->sl.rq.v_l != 8) {
3048 msg->err_pos = msg->sl.rq.v;
3049 goto return_bad_req;
3050 }
3051
3052 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
3053 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
3054 req->buf->p[msg->sl.rq.v + 6] != '.' ||
3055 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
3056 msg->err_pos = msg->sl.rq.v + 4;
3057 goto return_bad_req;
3058 }
3059 }
Willy Tarreau13317662015-05-01 13:47:08 +02003060 else {
3061 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
3062 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
3063 goto return_bad_req;
3064 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02003065
Willy Tarreau5b154472009-12-21 20:11:07 +01003066 /* ... and check if the request is HTTP/1.1 or above */
3067 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003068 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
3069 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
3070 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003071 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01003072
3073 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01003074 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 +01003075
Willy Tarreau88d349d2010-01-25 12:15:43 +01003076 /* if the frontend has "option http-use-proxy-header", we'll check if
3077 * we have what looks like a proxied connection instead of a connection,
3078 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
3079 * Note that this is *not* RFC-compliant, however browsers and proxies
3080 * happen to do that despite being non-standard :-(
3081 * We consider that a request not beginning with either '/' or '*' is
3082 * a proxied connection, which covers both "scheme://location" and
3083 * CONNECT ip:port.
3084 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003085 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003086 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01003087 txn->flags |= TX_USE_PX_CONN;
3088
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003089 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003090 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003091
Willy Tarreau59234e92008-11-30 23:51:27 +01003092 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003093 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003094 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003095 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003096
Willy Tarreau557f1992015-05-01 10:05:17 +02003097 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3098 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003099 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003100 * The length of a message body is determined by one of the following
3101 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003102 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003103 * 1. Any response to a HEAD request and any response with a 1xx
3104 * (Informational), 204 (No Content), or 304 (Not Modified) status
3105 * code is always terminated by the first empty line after the
3106 * header fields, regardless of the header fields present in the
3107 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003108 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003109 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3110 * the connection will become a tunnel immediately after the empty
3111 * line that concludes the header fields. A client MUST ignore any
3112 * Content-Length or Transfer-Encoding header fields received in
3113 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003114 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003115 * 3. If a Transfer-Encoding header field is present and the chunked
3116 * transfer coding (Section 4.1) is the final encoding, the message
3117 * body length is determined by reading and decoding the chunked
3118 * data until the transfer coding indicates the data is complete.
3119 *
3120 * If a Transfer-Encoding header field is present in a response and
3121 * the chunked transfer coding is not the final encoding, the
3122 * message body length is determined by reading the connection until
3123 * it is closed by the server. If a Transfer-Encoding header field
3124 * is present in a request and the chunked transfer coding is not
3125 * the final encoding, the message body length cannot be determined
3126 * reliably; the server MUST respond with the 400 (Bad Request)
3127 * status code and then close the connection.
3128 *
3129 * If a message is received with both a Transfer-Encoding and a
3130 * Content-Length header field, the Transfer-Encoding overrides the
3131 * Content-Length. Such a message might indicate an attempt to
3132 * perform request smuggling (Section 9.5) or response splitting
3133 * (Section 9.4) and ought to be handled as an error. A sender MUST
3134 * remove the received Content-Length field prior to forwarding such
3135 * a message downstream.
3136 *
3137 * 4. If a message is received without Transfer-Encoding and with
3138 * either multiple Content-Length header fields having differing
3139 * field-values or a single Content-Length header field having an
3140 * invalid value, then the message framing is invalid and the
3141 * recipient MUST treat it as an unrecoverable error. If this is a
3142 * request message, the server MUST respond with a 400 (Bad Request)
3143 * status code and then close the connection. If this is a response
3144 * message received by a proxy, the proxy MUST close the connection
3145 * to the server, discard the received response, and send a 502 (Bad
3146 * Gateway) response to the client. If this is a response message
3147 * received by a user agent, the user agent MUST close the
3148 * connection to the server and discard the received response.
3149 *
3150 * 5. If a valid Content-Length header field is present without
3151 * Transfer-Encoding, its decimal value defines the expected message
3152 * body length in octets. If the sender closes the connection or
3153 * the recipient times out before the indicated number of octets are
3154 * received, the recipient MUST consider the message to be
3155 * incomplete and close the connection.
3156 *
3157 * 6. If this is a request message and none of the above are true, then
3158 * the message body length is zero (no message body is present).
3159 *
3160 * 7. Otherwise, this is a response message without a declared message
3161 * body length, so the message body length is determined by the
3162 * number of octets received prior to the server closing the
3163 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003164 */
3165
Willy Tarreau32b47f42009-10-18 20:55:02 +02003166 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003167 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003168 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003169 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003170 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3171 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003172 /* chunked not last, return badreq */
3173 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003174 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003175 }
3176
Willy Tarreau1c913912015-04-30 10:57:51 +02003177 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003178 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003179 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3180 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3181 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3182 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003183 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003184 signed long long cl;
3185
Willy Tarreauad14f752011-09-02 20:33:27 +02003186 if (!ctx.vlen) {
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;
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 (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
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; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003194 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003195
Willy Tarreauad14f752011-09-02 20:33:27 +02003196 if (cl < 0) {
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;
Willy Tarreauad14f752011-09-02 20:33:27 +02003199 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003200
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003201 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003202 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003203 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003204 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003205
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003206 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003207 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003208 }
3209
Willy Tarreau34dfc602015-05-01 10:09:49 +02003210 /* even bodyless requests have a known length */
3211 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003212
Willy Tarreau179085c2014-04-28 16:48:56 +02003213 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3214 * only change if both the request and the config reference something else.
3215 * Option httpclose by itself sets tunnel mode where headers are mangled.
3216 * However, if another mode is set, it will affect it (eg: server-close/
3217 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3218 * if FE and BE have the same settings (common). The method consists in
3219 * checking if options changed between the two calls (implying that either
3220 * one is non-null, or one of them is non-null and we are there for the first
3221 * time.
3222 */
3223 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003224 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003225 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003226
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003227 /* we may have to wait for the request's body */
3228 if ((s->be->options & PR_O_WREQ_BODY) &&
3229 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3230 req->analysers |= AN_REQ_HTTP_BODY;
3231
Willy Tarreaud787e662009-07-07 10:14:51 +02003232 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003233 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003234 req->analyse_exp = TICK_ETERNITY;
3235 return 1;
3236
3237 return_bad_req:
3238 /* We centralize bad requests processing here */
3239 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3240 /* we detected a parsing error. We want to archive this request
3241 * in the dedicated proxy area for later troubleshooting.
3242 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003243 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003244 }
3245
3246 txn->req.msg_state = HTTP_MSG_ERROR;
3247 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003248 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003249
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003250 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003251 if (sess->listener->counters)
3252 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003253
3254 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003255 if (!(s->flags & SF_ERR_MASK))
3256 s->flags |= SF_ERR_PRXCOND;
3257 if (!(s->flags & SF_FINST_MASK))
3258 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003259
3260 req->analysers = 0;
3261 req->analyse_exp = TICK_ETERNITY;
3262 return 0;
3263}
3264
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003265
Willy Tarreau347a35d2013-11-22 17:51:09 +01003266/* This function prepares an applet to handle the stats. It can deal with the
3267 * "100-continue" expectation, check that admin rules are met for POST requests,
3268 * and program a response message if something was unexpected. It cannot fail
3269 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003270 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003271 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003272 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003273 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003274int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003275{
3276 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003277 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003278 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003279 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003280 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003281 struct uri_auth *uri_auth = s->be->uri_auth;
3282 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003283 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003284
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003285 appctx = si_appctx(si);
3286 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3287 appctx->st1 = appctx->st2 = 0;
3288 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3289 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003290 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003291 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003292
3293 uri = msg->chn->buf->p + msg->sl.rq.u;
3294 lookup = uri + uri_auth->uri_len;
3295
3296 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3297 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003298 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003299 break;
3300 }
3301 }
3302
3303 if (uri_auth->refresh) {
3304 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3305 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003306 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003307 break;
3308 }
3309 }
3310 }
3311
3312 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3313 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003314 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003315 break;
3316 }
3317 }
3318
3319 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3320 if (memcmp(h, ";st=", 4) == 0) {
3321 int i;
3322 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003323 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003324 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3325 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003326 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003327 break;
3328 }
3329 }
3330 break;
3331 }
3332 }
3333
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003334 appctx->ctx.stats.scope_str = 0;
3335 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003336 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3337 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3338 int itx = 0;
3339 const char *h2;
3340 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3341 const char *err;
3342
3343 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3344 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003345 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003346 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3347 itx++;
3348 h++;
3349 }
3350
3351 if (itx > STAT_SCOPE_TXT_MAXLEN)
3352 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003353 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003354
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003355 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003356 memcpy(scope_txt, h2, itx);
3357 scope_txt[itx] = '\0';
3358 err = invalid_char(scope_txt);
3359 if (err) {
3360 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003361 appctx->ctx.stats.scope_str = 0;
3362 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003363 }
3364 break;
3365 }
3366 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003367
3368 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003369 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003370 int ret = 1;
3371
3372 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003373 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 +01003374 ret = acl_pass(ret);
3375 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3376 ret = !ret;
3377 }
3378
3379 if (ret) {
3380 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003381 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003382 break;
3383 }
3384 }
3385
3386 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003387 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003388 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003389 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003390 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3391 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003392 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003393 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003394 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003395 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3396 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003397 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003398 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003399 else {
3400 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003401 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003402 }
3403
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003404 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003405 return 1;
3406}
3407
Lukas Tribus67db8df2013-06-23 17:37:13 +02003408/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3409 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3410 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003411void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003412{
3413#ifdef IP_TOS
3414 if (from.ss_family == AF_INET)
3415 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3416#endif
3417#ifdef IPV6_TCLASS
3418 if (from.ss_family == AF_INET6) {
3419 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3420 /* v4-mapped addresses need IP_TOS */
3421 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3422 else
3423 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3424 }
3425#endif
3426}
3427
Willy Tarreau87b09662015-04-03 00:22:06 +02003428int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003429 const char* name, unsigned int name_len,
3430 const char *str, struct my_regex *re,
3431 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003432{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003433 struct hdr_ctx ctx;
3434 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003435 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003436 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3437 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003438 struct chunk *output = get_trash_chunk();
3439
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003440 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003441
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003442 /* Choose the header browsing function. */
3443 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003444 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003445 http_find_hdr_func = http_find_header2;
3446 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003447 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003448 http_find_hdr_func = http_find_full_header2;
3449 break;
3450 default: /* impossible */
3451 return -1;
3452 }
3453
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003454 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3455 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003456 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003457 char *val = ctx.line + ctx.val;
3458 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003459
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003460 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3461 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003462
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003463 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003464 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003465 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003466
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003467 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003468
3469 hdr->len += delta;
3470 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003471
3472 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003473 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003474 }
3475
3476 return 0;
3477}
3478
Willy Tarreau87b09662015-04-03 00:22:06 +02003479static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003480 const char* name, unsigned int name_len,
3481 struct list *fmt, struct my_regex *re,
3482 int action)
3483{
3484 struct chunk *replace = get_trash_chunk();
3485
3486 replace->len = build_logline(s, replace->str, replace->size, fmt);
3487 if (replace->len >= replace->size - 1)
3488 return -1;
3489
3490 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3491}
3492
Willy Tarreau87b09662015-04-03 00:22:06 +02003493/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003494 * transaction <txn>. Returns the verdict of the first rule that prevents
3495 * further processing of the request (auth, deny, ...), and defaults to
3496 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3497 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3498 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003499 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003500enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003501http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003502{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003503 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003504 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003505 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003506 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003507 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003508 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003509 int act_flags = 0;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003510
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003511 /* If "the current_rule_list" match the executed rule list, we are in
3512 * resume condition. If a resume is needed it is always in the action
3513 * and never in the ACL or converters. In this case, we initialise the
3514 * current rule, and go to the action execution point.
3515 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003516 if (s->current_rule) {
3517 rule = s->current_rule;
3518 s->current_rule = NULL;
3519 if (s->current_rule_list == rules)
3520 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003521 }
3522 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003523
Willy Tarreauff011f22011-01-06 17:51:27 +01003524 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003525
Willy Tarreau96257ec2012-12-27 10:46:37 +01003526 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003527 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003528 int ret;
3529
Willy Tarreau192252e2015-04-04 01:47:55 +02003530 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003531 ret = acl_pass(ret);
3532
Willy Tarreauff011f22011-01-06 17:51:27 +01003533 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003534 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003535
3536 if (!ret) /* condition not matched */
3537 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003538 }
3539
Willy Tarreauacc98002015-09-27 23:34:39 +02003540 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003541resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003542 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003543 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003544 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003545
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003546 case ACT_ACTION_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003547 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003548 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003549
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003550 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003551 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003552 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003553 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003554
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003555 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003556 /* Auth might be performed on regular http-req rules as well as on stats */
3557 auth_realm = rule->arg.auth.realm;
3558 if (!auth_realm) {
3559 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3560 auth_realm = STATS_DEFAULT_REALM;
3561 else
3562 auth_realm = px->id;
3563 }
3564 /* send 401/407 depending on whether we use a proxy or not. We still
3565 * count one error, because normal browsing won't significantly
3566 * increase the counter but brute force attempts will.
3567 */
3568 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3569 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003570 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003571 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003572 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003573
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003574 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003575 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3576 return HTTP_RULE_RES_BADREQ;
3577 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003578
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003579 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003580 s->task->nice = rule->arg.nice;
3581 break;
3582
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003583 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003584 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003585 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003586 break;
3587
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003588 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003589#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003590 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003591 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003592#endif
3593 break;
3594
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003595 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003596 s->logs.level = rule->arg.loglevel;
3597 break;
3598
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003599 case ACT_HTTP_REPLACE_HDR:
3600 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003601 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3602 rule->arg.hdr_add.name_len,
3603 &rule->arg.hdr_add.fmt,
3604 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003605 return HTTP_RULE_RES_BADREQ;
3606 break;
3607
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003608 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003609 ctx.idx = 0;
3610 /* remove all occurrences of the header */
3611 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3612 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3613 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003614 }
Willy Tarreau85603282015-01-21 20:39:27 +01003615 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003616
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003617 case ACT_HTTP_SET_HDR:
3618 case ACT_HTTP_ADD_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003619 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3620 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3621 trash.len = rule->arg.hdr_add.name_len;
3622 trash.str[trash.len++] = ':';
3623 trash.str[trash.len++] = ' ';
3624 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 +01003625
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003626 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003627 /* remove all occurrences of the header */
3628 ctx.idx = 0;
3629 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3630 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3631 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3632 }
3633 }
3634
Willy Tarreau96257ec2012-12-27 10:46:37 +01003635 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3636 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003637
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003638 case ACT_HTTP_DEL_ACL:
3639 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003640 struct pat_ref *ref;
3641 char *key;
3642 int len;
3643
3644 /* collect reference */
3645 ref = pat_ref_lookup(rule->arg.map.ref);
3646 if (!ref)
3647 continue;
3648
3649 /* collect key */
3650 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3651 key = trash.str;
3652 key[len] = '\0';
3653
3654 /* perform update */
3655 /* returned code: 1=ok, 0=ko */
3656 pat_ref_delete(ref, key);
3657
3658 break;
3659 }
3660
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003661 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003662 struct pat_ref *ref;
3663 char *key;
3664 struct chunk *trash_key;
3665 int len;
3666
3667 trash_key = get_trash_chunk();
3668
3669 /* collect reference */
3670 ref = pat_ref_lookup(rule->arg.map.ref);
3671 if (!ref)
3672 continue;
3673
3674 /* collect key */
3675 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3676 key = trash_key->str;
3677 key[len] = '\0';
3678
3679 /* perform update */
3680 /* add entry only if it does not already exist */
3681 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003682 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003683
3684 break;
3685 }
3686
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003687 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003688 struct pat_ref *ref;
3689 char *key, *value;
3690 struct chunk *trash_key, *trash_value;
3691 int len;
3692
3693 trash_key = get_trash_chunk();
3694 trash_value = get_trash_chunk();
3695
3696 /* collect reference */
3697 ref = pat_ref_lookup(rule->arg.map.ref);
3698 if (!ref)
3699 continue;
3700
3701 /* collect key */
3702 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3703 key = trash_key->str;
3704 key[len] = '\0';
3705
3706 /* collect value */
3707 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3708 value = trash_value->str;
3709 value[len] = '\0';
3710
3711 /* perform update */
3712 if (pat_ref_find_elt(ref, key) != NULL)
3713 /* update entry if it exists */
3714 pat_ref_set(ref, key, value, NULL);
3715 else
3716 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003717 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003718
3719 break;
3720 }
William Lallemand73025dd2014-04-24 14:38:37 +02003721
Thierry FOURNIER42148732015-09-02 17:17:33 +02003722 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003723 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3724 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003725
Willy Tarreauacc98002015-09-27 23:34:39 +02003726 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003727 case ACT_RET_ERR:
3728 case ACT_RET_CONT:
3729 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003730 case ACT_RET_STOP:
3731 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003732 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003733 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003734 return HTTP_RULE_RES_YIELD;
3735 }
William Lallemand73025dd2014-04-24 14:38:37 +02003736 break;
3737
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003738 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003739 /* Note: only the first valid tracking parameter of each
3740 * applies.
3741 */
3742
3743 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3744 struct stktable *t;
3745 struct stksess *ts;
3746 struct stktable_key *key;
3747 void *ptr;
3748
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003749 t = rule->arg.trk_ctr.table.t;
3750 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 +02003751
3752 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003753 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003754
3755 /* let's count a new HTTP request as it's the first time we do it */
3756 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3757 if (ptr)
3758 stktable_data_cast(ptr, http_req_cnt)++;
3759
3760 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3761 if (ptr)
3762 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3763 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3764
3765 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003766 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003767 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3768 }
3769 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003770 break;
3771
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003772 case ACT_HTTP_REQ_SET_SRC:
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003773 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
3774 struct sample *smp;
3775
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02003776 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 +02003777
3778 if (smp) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003779 if (smp->data.type == SMP_T_IPV4) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003780 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003781 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003782 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003783 } else if (smp->data.type == SMP_T_IPV6) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003784 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003785 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 +02003786 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0;
3787 }
3788 }
3789 }
3790 break;
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003791
3792 /* other flags exists, but normaly, they never be matched. */
3793 default:
3794 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003795 }
3796 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003797
3798 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003799 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003800}
3801
Willy Tarreau71241ab2012-12-27 11:30:54 +01003802
Willy Tarreau51d861a2015-05-22 17:30:48 +02003803/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3804 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3805 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3806 * is returned, the process can continue the evaluation of next rule list. If
3807 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3808 * is returned, it means the operation could not be processed and a server error
3809 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3810 * deny rule. If *YIELD is returned, the caller must call again the function
3811 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003812 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003813static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003814http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003815{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003816 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003817 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003818 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003819 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003820 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003821 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003822
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003823 /* If "the current_rule_list" match the executed rule list, we are in
3824 * resume condition. If a resume is needed it is always in the action
3825 * and never in the ACL or converters. In this case, we initialise the
3826 * current rule, and go to the action execution point.
3827 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003828 if (s->current_rule) {
3829 rule = s->current_rule;
3830 s->current_rule = NULL;
3831 if (s->current_rule_list == rules)
3832 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003833 }
3834 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003835
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003836 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003837
3838 /* check optional condition */
3839 if (rule->cond) {
3840 int ret;
3841
Willy Tarreau192252e2015-04-04 01:47:55 +02003842 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003843 ret = acl_pass(ret);
3844
3845 if (rule->cond->pol == ACL_COND_UNLESS)
3846 ret = !ret;
3847
3848 if (!ret) /* condition not matched */
3849 continue;
3850 }
3851
Willy Tarreauacc98002015-09-27 23:34:39 +02003852 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003853resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003854 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003855 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003856 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003857
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003858 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003859 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003860 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003861
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003862 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003863 s->task->nice = rule->arg.nice;
3864 break;
3865
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003866 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003867 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003868 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003869 break;
3870
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003871 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003872#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003873 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003874 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003875#endif
3876 break;
3877
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003878 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003879 s->logs.level = rule->arg.loglevel;
3880 break;
3881
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003882 case ACT_HTTP_REPLACE_HDR:
3883 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003884 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3885 rule->arg.hdr_add.name_len,
3886 &rule->arg.hdr_add.fmt,
3887 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003888 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003889 break;
3890
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003891 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003892 ctx.idx = 0;
3893 /* remove all occurrences of the header */
3894 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3895 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3896 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3897 }
Willy Tarreau85603282015-01-21 20:39:27 +01003898 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003899
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003900 case ACT_HTTP_SET_HDR:
3901 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003902 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3903 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3904 trash.len = rule->arg.hdr_add.name_len;
3905 trash.str[trash.len++] = ':';
3906 trash.str[trash.len++] = ' ';
3907 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 +01003908
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003909 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003910 /* remove all occurrences of the header */
3911 ctx.idx = 0;
3912 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3913 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3914 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3915 }
3916 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003917 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3918 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003919
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003920 case ACT_HTTP_DEL_ACL:
3921 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003922 struct pat_ref *ref;
3923 char *key;
3924 int len;
3925
3926 /* collect reference */
3927 ref = pat_ref_lookup(rule->arg.map.ref);
3928 if (!ref)
3929 continue;
3930
3931 /* collect key */
3932 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3933 key = trash.str;
3934 key[len] = '\0';
3935
3936 /* perform update */
3937 /* returned code: 1=ok, 0=ko */
3938 pat_ref_delete(ref, key);
3939
3940 break;
3941 }
3942
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003943 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003944 struct pat_ref *ref;
3945 char *key;
3946 struct chunk *trash_key;
3947 int len;
3948
3949 trash_key = get_trash_chunk();
3950
3951 /* collect reference */
3952 ref = pat_ref_lookup(rule->arg.map.ref);
3953 if (!ref)
3954 continue;
3955
3956 /* collect key */
3957 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3958 key = trash_key->str;
3959 key[len] = '\0';
3960
3961 /* perform update */
3962 /* check if the entry already exists */
3963 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003964 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003965
3966 break;
3967 }
3968
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003969 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003970 struct pat_ref *ref;
3971 char *key, *value;
3972 struct chunk *trash_key, *trash_value;
3973 int len;
3974
3975 trash_key = get_trash_chunk();
3976 trash_value = get_trash_chunk();
3977
3978 /* collect reference */
3979 ref = pat_ref_lookup(rule->arg.map.ref);
3980 if (!ref)
3981 continue;
3982
3983 /* collect key */
3984 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3985 key = trash_key->str;
3986 key[len] = '\0';
3987
3988 /* collect value */
3989 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3990 value = trash_value->str;
3991 value[len] = '\0';
3992
3993 /* perform update */
3994 if (pat_ref_find_elt(ref, key) != NULL)
3995 /* update entry if it exists */
3996 pat_ref_set(ref, key, value, NULL);
3997 else
3998 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003999 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02004000
4001 break;
4002 }
William Lallemand73025dd2014-04-24 14:38:37 +02004003
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004004 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02004005 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
4006 return HTTP_RULE_RES_BADREQ;
4007 return HTTP_RULE_RES_DONE;
4008
Thierry FOURNIER42148732015-09-02 17:17:33 +02004009 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02004010 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
4011 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02004012
Willy Tarreauacc98002015-09-27 23:34:39 +02004013 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004014 case ACT_RET_ERR:
4015 case ACT_RET_CONT:
4016 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02004017 case ACT_RET_STOP:
4018 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004019 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02004020 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004021 return HTTP_RULE_RES_YIELD;
4022 }
William Lallemand73025dd2014-04-24 14:38:37 +02004023 break;
4024
Thierry FOURNIER22e49012015-08-05 19:13:48 +02004025 /* other flags exists, but normaly, they never be matched. */
4026 default:
4027 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004028 }
4029 }
4030
4031 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004032 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004033}
4034
4035
Willy Tarreau71241ab2012-12-27 11:30:54 +01004036/* Perform an HTTP redirect based on the information in <rule>. The function
4037 * returns non-zero on success, or zero in case of a, irrecoverable error such
4038 * as too large a request to build a valid response.
4039 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004040static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004041{
Willy Tarreaub329a312015-05-22 16:27:37 +02004042 struct http_msg *req = &txn->req;
4043 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004044 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004045 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004046
4047 /* build redirect message */
4048 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004049 case 308:
4050 msg_fmt = HTTP_308;
4051 break;
4052 case 307:
4053 msg_fmt = HTTP_307;
4054 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004055 case 303:
4056 msg_fmt = HTTP_303;
4057 break;
4058 case 301:
4059 msg_fmt = HTTP_301;
4060 break;
4061 case 302:
4062 default:
4063 msg_fmt = HTTP_302;
4064 break;
4065 }
4066
4067 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
4068 return 0;
4069
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004070 location = trash.str + trash.len;
4071
Willy Tarreau71241ab2012-12-27 11:30:54 +01004072 switch(rule->type) {
4073 case REDIRECT_TYPE_SCHEME: {
4074 const char *path;
4075 const char *host;
4076 struct hdr_ctx ctx;
4077 int pathlen;
4078 int hostlen;
4079
4080 host = "";
4081 hostlen = 0;
4082 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004083 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004084 host = ctx.line + ctx.val;
4085 hostlen = ctx.vlen;
4086 }
4087
4088 path = http_get_path(txn);
4089 /* build message using path */
4090 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004091 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004092 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4093 int qs = 0;
4094 while (qs < pathlen) {
4095 if (path[qs] == '?') {
4096 pathlen = qs;
4097 break;
4098 }
4099 qs++;
4100 }
4101 }
4102 } else {
4103 path = "/";
4104 pathlen = 1;
4105 }
4106
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004107 if (rule->rdr_str) { /* this is an old "redirect" rule */
4108 /* check if we can add scheme + "://" + host + path */
4109 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
4110 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004111
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004112 /* add scheme */
4113 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4114 trash.len += rule->rdr_len;
4115 }
4116 else {
4117 /* add scheme with executing log format */
4118 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004119
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004120 /* check if we can add scheme + "://" + host + path */
4121 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
4122 return 0;
4123 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004124 /* add "://" */
4125 memcpy(trash.str + trash.len, "://", 3);
4126 trash.len += 3;
4127
4128 /* add host */
4129 memcpy(trash.str + trash.len, host, hostlen);
4130 trash.len += hostlen;
4131
4132 /* add path */
4133 memcpy(trash.str + trash.len, path, pathlen);
4134 trash.len += pathlen;
4135
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004136 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004137 if (trash.len && trash.str[trash.len - 1] != '/' &&
4138 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4139 if (trash.len > trash.size - 5)
4140 return 0;
4141 trash.str[trash.len] = '/';
4142 trash.len++;
4143 }
4144
4145 break;
4146 }
4147 case REDIRECT_TYPE_PREFIX: {
4148 const char *path;
4149 int pathlen;
4150
4151 path = http_get_path(txn);
4152 /* build message using path */
4153 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004154 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004155 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4156 int qs = 0;
4157 while (qs < pathlen) {
4158 if (path[qs] == '?') {
4159 pathlen = qs;
4160 break;
4161 }
4162 qs++;
4163 }
4164 }
4165 } else {
4166 path = "/";
4167 pathlen = 1;
4168 }
4169
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004170 if (rule->rdr_str) { /* this is an old "redirect" rule */
4171 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4172 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004173
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004174 /* add prefix. Note that if prefix == "/", we don't want to
4175 * add anything, otherwise it makes it hard for the user to
4176 * configure a self-redirection.
4177 */
4178 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4179 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4180 trash.len += rule->rdr_len;
4181 }
4182 }
4183 else {
4184 /* add prefix with executing log format */
4185 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4186
4187 /* Check length */
4188 if (trash.len + pathlen > trash.size - 4)
4189 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004190 }
4191
4192 /* add path */
4193 memcpy(trash.str + trash.len, path, pathlen);
4194 trash.len += pathlen;
4195
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004196 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004197 if (trash.len && trash.str[trash.len - 1] != '/' &&
4198 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4199 if (trash.len > trash.size - 5)
4200 return 0;
4201 trash.str[trash.len] = '/';
4202 trash.len++;
4203 }
4204
4205 break;
4206 }
4207 case REDIRECT_TYPE_LOCATION:
4208 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004209 if (rule->rdr_str) { /* this is an old "redirect" rule */
4210 if (trash.len + rule->rdr_len > trash.size - 4)
4211 return 0;
4212
4213 /* add location */
4214 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4215 trash.len += rule->rdr_len;
4216 }
4217 else {
4218 /* add location with executing log format */
4219 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004220
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004221 /* Check left length */
4222 if (trash.len > trash.size - 4)
4223 return 0;
4224 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004225 break;
4226 }
4227
4228 if (rule->cookie_len) {
4229 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4230 trash.len += 14;
4231 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4232 trash.len += rule->cookie_len;
4233 memcpy(trash.str + trash.len, "\r\n", 2);
4234 trash.len += 2;
4235 }
4236
4237 /* add end of headers and the keep-alive/close status.
4238 * We may choose to set keep-alive if the Location begins
4239 * with a slash, because the client will come back to the
4240 * same server.
4241 */
4242 txn->status = rule->code;
4243 /* let's log the request time */
4244 s->logs.tv_request = now;
4245
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004246 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004247 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004248 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004249 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4250 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4251 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004252 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004253 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4254 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4255 trash.len += 30;
4256 } else {
4257 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4258 trash.len += 24;
4259 }
4260 }
4261 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4262 trash.len += 4;
Willy Tarreaub329a312015-05-22 16:27:37 +02004263 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004264 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004265 bi_fast_delete(req->chn->buf, req->sov);
4266 req->next -= req->sov;
4267 req->sov = 0;
4268 s->req.analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004269 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreaub329a312015-05-22 16:27:37 +02004270 req->msg_state = HTTP_MSG_CLOSED;
4271 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004272 /* Trim any possible response */
4273 res->chn->buf->i = 0;
4274 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004275 } else {
4276 /* keep-alive not possible */
4277 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4278 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4279 trash.len += 29;
4280 } else {
4281 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4282 trash.len += 23;
4283 }
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004284 http_reply_and_close(s, txn->status, &trash);
Willy Tarreaub329a312015-05-22 16:27:37 +02004285 req->chn->analysers = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004286 }
4287
Willy Tarreaue7dff022015-04-03 01:14:29 +02004288 if (!(s->flags & SF_ERR_MASK))
4289 s->flags |= SF_ERR_LOCAL;
4290 if (!(s->flags & SF_FINST_MASK))
4291 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004292
4293 return 1;
4294}
4295
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004296/* This stream analyser runs all HTTP request processing which is common to
4297 * frontends and backends, which means blocking ACLs, filters, connection-close,
4298 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004299 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004300 * either needs more data or wants to immediately abort the request (eg: deny,
4301 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004302 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004303int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004304{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004305 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004306 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004307 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004308 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004309 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004310 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004311
Willy Tarreau655dce92009-11-08 13:10:58 +01004312 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004313 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004314 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004315 }
4316
Willy Tarreau87b09662015-04-03 00:22:06 +02004317 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 +02004318 now_ms, __FUNCTION__,
4319 s,
4320 req,
4321 req->rex, req->wex,
4322 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004323 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004324 req->analysers);
4325
Willy Tarreau65410832014-04-28 21:25:43 +02004326 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004327 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004328
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004329 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004330 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004331 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004332
Willy Tarreau0b748332014-04-29 00:13:29 +02004333 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004334 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4335 goto return_prx_yield;
4336
Willy Tarreau0b748332014-04-29 00:13:29 +02004337 case HTTP_RULE_RES_CONT:
4338 case HTTP_RULE_RES_STOP: /* nothing to do */
4339 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004340
Willy Tarreau0b748332014-04-29 00:13:29 +02004341 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4342 if (txn->flags & TX_CLTARPIT)
4343 goto tarpit;
4344 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004345
Willy Tarreau0b748332014-04-29 00:13:29 +02004346 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4347 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004348
Willy Tarreau0b748332014-04-29 00:13:29 +02004349 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004350 goto done;
4351
Willy Tarreau0b748332014-04-29 00:13:29 +02004352 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4353 goto return_bad_req;
4354 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004355 }
4356
Willy Tarreau52542592014-04-28 18:33:26 +02004357 /* OK at this stage, we know that the request was accepted according to
4358 * the http-request rules, we can check for the stats. Note that the
4359 * URI is detected *before* the req* rules in order not to be affected
4360 * by a possible reqrep, while they are processed *after* so that a
4361 * reqdeny can still block them. This clearly needs to change in 1.6!
4362 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004363 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004364 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004365 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004366 txn->status = 500;
4367 s->logs.tv_request = now;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004368 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004369
Willy Tarreaue7dff022015-04-03 01:14:29 +02004370 if (!(s->flags & SF_ERR_MASK))
4371 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004372 goto return_prx_cond;
4373 }
4374
4375 /* parse the whole stats request and extract the relevant information */
4376 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004377 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004378 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004379
Willy Tarreau0b748332014-04-29 00:13:29 +02004380 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4381 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004382
Willy Tarreau0b748332014-04-29 00:13:29 +02004383 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4384 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004385 }
4386
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004387 /* evaluate the req* rules except reqadd */
4388 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004389 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004390 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004391
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004392 if (txn->flags & TX_CLDENY)
4393 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004394
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004395 if (txn->flags & TX_CLTARPIT)
4396 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004397 }
Willy Tarreau06619262006-12-17 08:37:22 +01004398
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004399 /* add request headers from the rule sets in the same order */
4400 list_for_each_entry(wl, &px->req_add, list) {
4401 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004402 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004403 ret = acl_pass(ret);
4404 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4405 ret = !ret;
4406 if (!ret)
4407 continue;
4408 }
4409
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004410 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004411 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004412 }
4413
Willy Tarreau52542592014-04-28 18:33:26 +02004414
4415 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004416 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004417 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004418 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4419 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004420
Willy Tarreaue7dff022015-04-03 01:14:29 +02004421 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4422 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4423 if (!(s->flags & SF_FINST_MASK))
4424 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004425
Willy Tarreau70730dd2014-04-24 18:06:27 +02004426 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004427 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004428 select_compression_request_header(s, req->buf);
4429
4430 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004431 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004432 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004433 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004434
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004435 /* check whether we have some ACLs set to redirect this request */
4436 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004437 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004438 int ret;
4439
Willy Tarreau192252e2015-04-04 01:47:55 +02004440 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004441 ret = acl_pass(ret);
4442 if (rule->cond->pol == ACL_COND_UNLESS)
4443 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004444 if (!ret)
4445 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004446 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004447 if (!http_apply_redirect_rule(rule, s, txn))
4448 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004449 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004450 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004451
Willy Tarreau2be39392010-01-03 17:24:51 +01004452 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4453 * If this happens, then the data will not come immediately, so we must
4454 * send all what we have without waiting. Note that due to the small gain
4455 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004456 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004457 * itself once used.
4458 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004459 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004460
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004461 done: /* done with this analyser, continue with next ones that the calling
4462 * points will have set, if any.
4463 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004464 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004465 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4466 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004467 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004468
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004469 tarpit:
4470 /* When a connection is tarpitted, we use the tarpit timeout,
4471 * which may be the same as the connect timeout if unspecified.
4472 * If unset, then set it to zero because we really want it to
4473 * eventually expire. We build the tarpit as an analyser.
4474 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004475 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004476
4477 /* wipe the request out so that we can drop the connection early
4478 * if the client closes first.
4479 */
4480 channel_dont_connect(req);
4481 req->analysers = 0; /* remove switching rules etc... */
4482 req->analysers |= AN_REQ_HTTP_TARPIT;
4483 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4484 if (!req->analyse_exp)
4485 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004486 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004487 sess->fe->fe_counters.denied_req++;
4488 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004489 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004490 if (sess->listener->counters)
4491 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004492 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004493
4494 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004495 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004496 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004497 s->logs.tv_request = now;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004498 http_reply_and_close(s, txn->status, http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004499 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004500 sess->fe->fe_counters.denied_req++;
4501 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004502 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004503 if (sess->listener->counters)
4504 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004505 goto return_prx_cond;
4506
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004507 return_bad_req:
4508 /* We centralize bad requests processing here */
4509 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4510 /* we detected a parsing error. We want to archive this request
4511 * in the dedicated proxy area for later troubleshooting.
4512 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004513 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004514 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004515
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004516 txn->req.msg_state = HTTP_MSG_ERROR;
4517 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004518 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004519
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004520 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004521 if (sess->listener->counters)
4522 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004523
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004524 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004525 if (!(s->flags & SF_ERR_MASK))
4526 s->flags |= SF_ERR_PRXCOND;
4527 if (!(s->flags & SF_FINST_MASK))
4528 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004529
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004530 req->analysers = 0;
4531 req->analyse_exp = TICK_ETERNITY;
4532 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004533
4534 return_prx_yield:
4535 channel_dont_connect(req);
4536 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004537}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004538
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004539/* This function performs all the processing enabled for the current request.
4540 * It returns 1 if the processing can continue on next analysers, or zero if it
4541 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004542 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004543 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004544int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004545{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004546 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004547 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004548 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004549 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004550
Willy Tarreau655dce92009-11-08 13:10:58 +01004551 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004552 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004553 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004554 return 0;
4555 }
4556
Willy Tarreau87b09662015-04-03 00:22:06 +02004557 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 +02004558 now_ms, __FUNCTION__,
4559 s,
4560 req,
4561 req->rex, req->wex,
4562 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004563 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004564 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004565
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004566 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004567 select_compression_request_header(s, req->buf);
4568
Willy Tarreau59234e92008-11-30 23:51:27 +01004569 /*
4570 * Right now, we know that we have processed the entire headers
4571 * and that unwanted requests have been filtered out. We can do
4572 * whatever we want with the remaining request. Also, now we
4573 * may have separate values for ->fe, ->be.
4574 */
Willy Tarreau06619262006-12-17 08:37:22 +01004575
Willy Tarreau59234e92008-11-30 23:51:27 +01004576 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004577 * If HTTP PROXY is set we simply get remote server address parsing
4578 * incoming request. Note that this requires that a connection is
4579 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004580 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004581 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004582 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004583 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004584
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004585 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004586 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004587 txn->req.msg_state = HTTP_MSG_ERROR;
4588 txn->status = 500;
4589 req->analysers = 0;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004590 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004591
Willy Tarreaue7dff022015-04-03 01:14:29 +02004592 if (!(s->flags & SF_ERR_MASK))
4593 s->flags |= SF_ERR_RESOURCE;
4594 if (!(s->flags & SF_FINST_MASK))
4595 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004596
4597 return 0;
4598 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004599
4600 path = http_get_path(txn);
4601 url2sa(req->buf->p + msg->sl.rq.u,
4602 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004603 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004604 /* if the path was found, we have to remove everything between
4605 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4606 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4607 * u_l characters by a single "/".
4608 */
4609 if (path) {
4610 char *cur_ptr = req->buf->p;
4611 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4612 int delta;
4613
4614 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4615 http_msg_move_end(&txn->req, delta);
4616 cur_end += delta;
4617 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4618 goto return_bad_req;
4619 }
4620 else {
4621 char *cur_ptr = req->buf->p;
4622 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4623 int delta;
4624
4625 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4626 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4627 http_msg_move_end(&txn->req, delta);
4628 cur_end += delta;
4629 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4630 goto return_bad_req;
4631 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004632 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004633
Willy Tarreau59234e92008-11-30 23:51:27 +01004634 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004635 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004636 * Note that doing so might move headers in the request, but
4637 * the fields will stay coherent and the URI will not move.
4638 * This should only be performed in the backend.
4639 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02004640 if ((s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004641 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4642 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004643
William Lallemanda73203e2012-03-12 12:48:57 +01004644 /* add unique-id if "header-unique-id" is specified */
4645
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004646 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004647 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4648 goto return_bad_req;
4649 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004650 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004651 }
William Lallemanda73203e2012-03-12 12:48:57 +01004652
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004653 if (sess->fe->header_unique_id && s->unique_id) {
4654 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004655 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004656 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004657 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004658 goto return_bad_req;
4659 }
4660
Cyril Bontéb21570a2009-11-29 20:04:48 +01004661 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004662 * 9: add X-Forwarded-For if either the frontend or the backend
4663 * asks for it.
4664 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004665 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004666 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004667 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4668 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4669 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004670 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004671 /* The header is set to be added only if none is present
4672 * and we found it, so don't do anything.
4673 */
4674 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004675 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004676 /* Add an X-Forwarded-For header unless the source IP is
4677 * in the 'except' network range.
4678 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004679 if ((!sess->fe->except_mask.s_addr ||
4680 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4681 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004682 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004683 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004684 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004685 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004686 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004687 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004688
4689 /* Note: we rely on the backend to get the header name to be used for
4690 * x-forwarded-for, because the header is really meant for the backends.
4691 * However, if the backend did not specify any option, we have to rely
4692 * on the frontend's header name.
4693 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004694 if (s->be->fwdfor_hdr_len) {
4695 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004696 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004697 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004698 len = sess->fe->fwdfor_hdr_len;
4699 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004700 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004701 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 +01004702
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004703 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004704 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004705 }
4706 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004707 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004708 /* FIXME: for the sake of completeness, we should also support
4709 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004710 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004711 int len;
4712 char pn[INET6_ADDRSTRLEN];
4713 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004714 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004715 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004716
Willy Tarreau59234e92008-11-30 23:51:27 +01004717 /* Note: we rely on the backend to get the header name to be used for
4718 * x-forwarded-for, because the header is really meant for the backends.
4719 * However, if the backend did not specify any option, we have to rely
4720 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004721 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004722 if (s->be->fwdfor_hdr_len) {
4723 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004724 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004725 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004726 len = sess->fe->fwdfor_hdr_len;
4727 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004728 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004729 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004730
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004731 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004732 goto return_bad_req;
4733 }
4734 }
4735
4736 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004737 * 10: add X-Original-To if either the frontend or the backend
4738 * asks for it.
4739 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004740 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004741
4742 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004743 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004744 /* Add an X-Original-To header unless the destination IP is
4745 * in the 'except' network range.
4746 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004747 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004748
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004749 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004750 ((!sess->fe->except_mask_to.s_addr ||
4751 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4752 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004753 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004754 (((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 +02004755 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004756 int len;
4757 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004758 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004759
4760 /* Note: we rely on the backend to get the header name to be used for
4761 * x-original-to, because the header is really meant for the backends.
4762 * However, if the backend did not specify any option, we have to rely
4763 * on the frontend's header name.
4764 */
4765 if (s->be->orgto_hdr_len) {
4766 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004767 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004768 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004769 len = sess->fe->orgto_hdr_len;
4770 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004771 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004772 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 +02004773
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004774 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004775 goto return_bad_req;
4776 }
4777 }
4778 }
4779
Willy Tarreau50fc7772012-11-11 22:19:57 +01004780 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4781 * If an "Upgrade" token is found, the header is left untouched in order not to have
4782 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4783 * "Upgrade" is present in the Connection header.
4784 */
4785 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4786 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
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 Tarreaubbf0b372010-01-18 16:54:40 +01004789 unsigned int want_flags = 0;
4790
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004791 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004792 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
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_CLO_SET;
4797 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004798 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004799 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004800 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004801 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004802 want_flags |= TX_CON_KAL_SET;
4803 }
4804
4805 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004806 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004807 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004808
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004809
Willy Tarreau522d6c02009-12-06 18:49:18 +01004810 /* If we have no server assigned yet and we're balancing on url_param
4811 * with a POST request, we may be interested in checking the body for
4812 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004813 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004814 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004815 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004816 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004817 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004818 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004819 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004820
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004821 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004822 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004823#ifdef TCP_QUICKACK
4824 /* We expect some data from the client. Unless we know for sure
4825 * we already have a full request, we have to re-enable quick-ack
4826 * in case we previously disabled it, otherwise we might cause
4827 * the client to delay further data.
4828 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004829 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004830 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004831 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004832 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004833 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004834#endif
4835 }
Willy Tarreau03945942009-12-22 16:50:27 +01004836
Willy Tarreau59234e92008-11-30 23:51:27 +01004837 /*************************************************************
4838 * OK, that's finished for the headers. We have done what we *
4839 * could. Let's switch to the DATA state. *
4840 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004841 req->analyse_exp = TICK_ETERNITY;
4842 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004843
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004844 /* if the server closes the connection, we want to immediately react
4845 * and close the socket to save packets and syscalls.
4846 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004847 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004848 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004849
Willy Tarreau59234e92008-11-30 23:51:27 +01004850 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004851 /* OK let's go on with the BODY now */
4852 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004853
Willy Tarreau59234e92008-11-30 23:51:27 +01004854 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004855 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004856 /* we detected a parsing error. We want to archive this request
4857 * in the dedicated proxy area for later troubleshooting.
4858 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004859 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004860 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004861
Willy Tarreau59234e92008-11-30 23:51:27 +01004862 txn->req.msg_state = HTTP_MSG_ERROR;
4863 txn->status = 400;
4864 req->analysers = 0;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004865 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004866
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004867 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004868 if (sess->listener->counters)
4869 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004870
Willy Tarreaue7dff022015-04-03 01:14:29 +02004871 if (!(s->flags & SF_ERR_MASK))
4872 s->flags |= SF_ERR_PRXCOND;
4873 if (!(s->flags & SF_FINST_MASK))
4874 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004875 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004876}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004877
Willy Tarreau60b85b02008-11-30 23:28:40 +01004878/* This function is an analyser which processes the HTTP tarpit. It always
4879 * returns zero, at the beginning because it prevents any other processing
4880 * from occurring, and at the end because it terminates the request.
4881 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004882int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004883{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004884 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004885
4886 /* This connection is being tarpitted. The CLIENT side has
4887 * already set the connect expiration date to the right
4888 * timeout. We just have to check that the client is still
4889 * there and that the timeout has not expired.
4890 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004891 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004892 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004893 !tick_is_expired(req->analyse_exp, now_ms))
4894 return 0;
4895
4896 /* We will set the queue timer to the time spent, just for
4897 * logging purposes. We fake a 500 server error, so that the
4898 * attacker will not suspect his connection has been tarpitted.
4899 * It will not cause trouble to the logs because we can exclude
4900 * the tarpitted connections by filtering on the 'PT' status flags.
4901 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004902 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4903
4904 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004905 if (!(req->flags & CF_READ_ERROR))
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004906 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004907
4908 req->analysers = 0;
4909 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004910
Willy Tarreaue7dff022015-04-03 01:14:29 +02004911 if (!(s->flags & SF_ERR_MASK))
4912 s->flags |= SF_ERR_PRXCOND;
4913 if (!(s->flags & SF_FINST_MASK))
4914 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004915 return 0;
4916}
4917
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004918/* This function is an analyser which waits for the HTTP request body. It waits
4919 * for either the buffer to be full, or the full advertised contents to have
4920 * reached the buffer. It must only be called after the standard HTTP request
4921 * processing has occurred, because it expects the request to be parsed and will
4922 * look for the Expect header. It may send a 100-Continue interim response. It
4923 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4924 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4925 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004926 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004927int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004928{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004929 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004930 struct http_txn *txn = s->txn;
4931 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004932
4933 /* We have to parse the HTTP request body to find any required data.
4934 * "balance url_param check_post" should have been the only way to get
4935 * into this. We were brought here after HTTP header analysis, so all
4936 * related structures are ready.
4937 */
4938
Willy Tarreau890988f2014-04-10 11:59:33 +02004939 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4940 /* This is the first call */
4941 if (msg->msg_state < HTTP_MSG_BODY)
4942 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004943
Willy Tarreau890988f2014-04-10 11:59:33 +02004944 if (msg->msg_state < HTTP_MSG_100_SENT) {
4945 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4946 * send an HTTP/1.1 100 Continue intermediate response.
4947 */
4948 if (msg->flags & HTTP_MSGF_VER_11) {
4949 struct hdr_ctx ctx;
4950 ctx.idx = 0;
4951 /* Expect is allowed in 1.1, look for it */
4952 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4953 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004954 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004955 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004956 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004957 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004958 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004959
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004960 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004961 * req->buf->p still points to the beginning of the message. We
4962 * must save the body in msg->next because it survives buffer
4963 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004964 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004965 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004966
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004967 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004968 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4969 else
4970 msg->msg_state = HTTP_MSG_DATA;
4971 }
4972
Willy Tarreau890988f2014-04-10 11:59:33 +02004973 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4974 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004975 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004976 goto missing_data;
4977
4978 /* OK we have everything we need now */
4979 goto http_end;
4980 }
4981
4982 /* OK here we're parsing a chunked-encoded message */
4983
Willy Tarreau522d6c02009-12-06 18:49:18 +01004984 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004985 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004986 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004987 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004988 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004989 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004990
Willy Tarreau115acb92009-12-26 13:56:06 +01004991 if (!ret)
4992 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004993 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004994 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004995 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004996 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004997 }
4998
Willy Tarreaud98cf932009-12-27 22:54:55 +01004999 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02005000 * We have the first data byte is in msg->sov + msg->sol. We're waiting
5001 * for at least a whole chunk or the whole content length bytes after
5002 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01005003 */
Willy Tarreau890988f2014-04-10 11:59:33 +02005004 if (msg->msg_state == HTTP_MSG_TRAILERS)
5005 goto http_end;
5006
Willy Tarreaue115b492015-05-01 23:05:14 +02005007 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01005008 goto http_end;
5009
5010 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02005011 /* we get here if we need to wait for more data. If the buffer is full,
5012 * we have the maximum we can expect.
5013 */
5014 if (buffer_full(req->buf, global.tune.maxrewrite))
5015 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01005016
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005017 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01005018 txn->status = 408;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005019 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02005020
Willy Tarreaue7dff022015-04-03 01:14:29 +02005021 if (!(s->flags & SF_ERR_MASK))
5022 s->flags |= SF_ERR_CLITO;
5023 if (!(s->flags & SF_FINST_MASK))
5024 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005025 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005026 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005027
5028 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005029 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005030 /* Not enough data. We'll re-use the http-request
5031 * timeout here. Ideally, we should set the timeout
5032 * relative to the accept() date. We just set the
5033 * request timeout once at the beginning of the
5034 * request.
5035 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005036 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005037 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005038 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005039 return 0;
5040 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005041
5042 http_end:
5043 /* The situation will not evolve, so let's give up on the analysis. */
5044 s->logs.tv_request = now; /* update the request timer to reflect full request */
5045 req->analysers &= ~an_bit;
5046 req->analyse_exp = TICK_ETERNITY;
5047 return 1;
5048
5049 return_bad_req: /* let's centralize all bad requests */
5050 txn->req.msg_state = HTTP_MSG_ERROR;
5051 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005052 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005053
Willy Tarreaue7dff022015-04-03 01:14:29 +02005054 if (!(s->flags & SF_ERR_MASK))
5055 s->flags |= SF_ERR_PRXCOND;
5056 if (!(s->flags & SF_FINST_MASK))
5057 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005058
Willy Tarreau522d6c02009-12-06 18:49:18 +01005059 return_err_msg:
5060 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005061 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005062 if (sess->listener->counters)
5063 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005064 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005065}
5066
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005067/* send a server's name with an outgoing request over an established connection.
5068 * Note: this function is designed to be called once the request has been scheduled
5069 * for being forwarded. This is the reason why it rewinds the buffer before
5070 * proceeding.
5071 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005072int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005073
5074 struct hdr_ctx ctx;
5075
Mark Lamourinec2247f02012-01-04 13:02:01 -05005076 char *hdr_name = be->server_id_hdr_name;
5077 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005078 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005079 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005080 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005081
William Lallemandd9e90662012-01-30 17:27:17 +01005082 ctx.idx = 0;
5083
Willy Tarreau211cdec2014-04-17 20:18:08 +02005084 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005085 if (old_o) {
5086 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005087 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005088 txn->req.next += old_o;
5089 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005090 }
5091
Willy Tarreau9b28e032012-10-12 23:49:43 +02005092 old_i = chn->buf->i;
5093 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 -05005094 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005095 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005096 }
5097
5098 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005099 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005100 memcpy(hdr_val, hdr_name, hdr_name_len);
5101 hdr_val += hdr_name_len;
5102 *hdr_val++ = ':';
5103 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005104 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5105 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005106
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005107 if (old_o) {
5108 /* If this was a forwarded request, we must readjust the amount of
5109 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005110 * variations. Note that the current state is >= HTTP_MSG_BODY,
5111 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005112 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005113 old_o += chn->buf->i - old_i;
5114 b_adv(chn->buf, old_o);
5115 txn->req.next -= old_o;
5116 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005117 }
5118
Mark Lamourinec2247f02012-01-04 13:02:01 -05005119 return 0;
5120}
5121
Willy Tarreau610ecce2010-01-04 21:15:02 +01005122/* Terminate current transaction and prepare a new one. This is very tricky
5123 * right now but it works.
5124 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005125void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005126{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005127 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005128 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01005129 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02005130 struct connection *srv_conn;
5131 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005132 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005133
Willy Tarreau610ecce2010-01-04 21:15:02 +01005134 /* FIXME: We need a more portable way of releasing a backend's and a
5135 * server's connections. We need a safer way to reinitialize buffer
5136 * flags. We also need a more accurate method for computing per-request
5137 * data.
5138 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005139 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005140
Willy Tarreau4213a112013-12-15 10:25:42 +01005141 /* unless we're doing keep-alive, we want to quickly close the connection
5142 * to the server.
5143 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005144 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005145 !si_conn_ready(&s->si[1])) {
5146 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5147 si_shutr(&s->si[1]);
5148 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005149 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005150
Willy Tarreaue7dff022015-04-03 01:14:29 +02005151 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01005152 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005153 if (unlikely(s->srv_conn))
5154 sess_change_server(s, NULL);
5155 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005156
5157 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005158 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005159
Willy Tarreaueee5b512015-04-03 23:46:31 +02005160 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005161 int n;
5162
Willy Tarreaueee5b512015-04-03 23:46:31 +02005163 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005164 if (n < 1 || n > 5)
5165 n = 0;
5166
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005167 if (fe->mode == PR_MODE_HTTP) {
5168 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005169 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005170 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005171 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005172 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01005173 (be->mode == PR_MODE_HTTP)) {
5174 be->be_counters.p.http.rsp[n]++;
5175 be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005176 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau858b1032015-12-07 17:04:59 +01005177 be->be_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005178 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005179 }
5180
5181 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005182 s->logs.bytes_in -= s->req.buf->i;
5183 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005184
5185 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005186 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005187 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005188 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005189 s->do_log(s);
5190 }
5191
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005192 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005193 stream_stop_content_counters(s);
5194 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005195
Willy Tarreau610ecce2010-01-04 21:15:02 +01005196 s->logs.accept_date = date; /* user-visible date for logging */
5197 s->logs.tv_accept = now; /* corrected date for internal use */
5198 tv_zero(&s->logs.tv_request);
5199 s->logs.t_queue = -1;
5200 s->logs.t_connect = -1;
5201 s->logs.t_data = -1;
5202 s->logs.t_close = 0;
5203 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5204 s->logs.srv_queue_size = 0; /* we will get this number soon */
5205
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005206 s->logs.bytes_in = s->req.total = s->req.buf->i;
5207 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005208
5209 if (s->pend_pos)
5210 pendconn_free(s->pend_pos);
5211
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005212 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005213 if (s->flags & SF_CURR_SESS) {
5214 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005215 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005216 }
Willy Tarreau858b1032015-12-07 17:04:59 +01005217 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005218 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005219 }
5220
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005221 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005222
Willy Tarreau4213a112013-12-15 10:25:42 +01005223 /* only release our endpoint if we don't intend to reuse the
5224 * connection.
5225 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005226 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005227 !si_conn_ready(&s->si[1])) {
5228 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005229 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005230 }
5231
Willy Tarreau350f4872014-11-28 14:42:25 +01005232 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5233 s->si[1].err_type = SI_ET_NONE;
5234 s->si[1].conn_retries = 0; /* used for logging too */
5235 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005236 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 +01005237 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);
5238 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 +02005239 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5240 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5241 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005242
Willy Tarreaueee5b512015-04-03 23:46:31 +02005243 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005244 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005245 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005246
5247 if (prev_status == 401 || prev_status == 407) {
5248 /* In HTTP keep-alive mode, if we receive a 401, we still have
5249 * a chance of being able to send the visitor again to the same
5250 * server over the same connection. This is required by some
5251 * broken protocols such as NTLM, and anyway whenever there is
5252 * an opportunity for sending the challenge to the proper place,
5253 * it's better to do it (at least it helps with debugging).
5254 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005255 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005256 if (srv_conn)
5257 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005258 }
5259
Willy Tarreau53f96852016-02-02 18:50:47 +01005260 /* Never ever allow to reuse a connection from a non-reuse backend */
5261 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
5262 srv_conn->flags |= CO_FL_PRIVATE;
5263
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005264 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005265 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005266
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005267 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005268 s->req.flags |= CF_NEVER_WAIT;
5269 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005270 }
5271
Willy Tarreau610ecce2010-01-04 21:15:02 +01005272 /* if the request buffer is not empty, it means we're
5273 * about to process another request, so send pending
5274 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005275 * Just don't do this if the buffer is close to be full,
5276 * because the request will wait for it to flush a little
5277 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005278 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005279 if (s->req.buf->i) {
5280 if (s->res.buf->o &&
5281 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5282 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5283 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005284 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005285
Willy Tarreau714ea782015-11-25 20:11:11 +01005286 /* we're removing the analysers, we MUST re-enable events detection.
5287 * We don't enable close on the response channel since it's either
5288 * already closed, or in keep-alive with an idle connection handler.
5289 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005290 channel_auto_read(&s->req);
5291 channel_auto_close(&s->req);
5292 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005293
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005294 /* we're in keep-alive with an idle connection, monitor it if not already done */
5295 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005296 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005297 if (!srv)
5298 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01005299 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02005300 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005301 else if (prev_flags & TX_NOT_FIRST)
5302 /* note: we check the request, not the connection, but
5303 * this is valid for strategies SAFE and AGGR, and in
5304 * case of ALWS, we don't care anyway.
5305 */
5306 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005307 else
5308 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005309 }
Willy Tarreau27375622013-12-17 00:00:28 +01005310
Willy Tarreauc29d0cd2015-09-23 12:18:14 +02005311 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005312 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005313}
5314
5315
5316/* This function updates the request state machine according to the response
5317 * state machine and buffer flags. It returns 1 if it changes anything (flag
5318 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5319 * it is only used to find when a request/response couple is complete. Both
5320 * this function and its equivalent should loop until both return zero. It
5321 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5322 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005323int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005324{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005325 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005326 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005327 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005328 unsigned int old_state = txn->req.msg_state;
5329
Willy Tarreau610ecce2010-01-04 21:15:02 +01005330 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5331 return 0;
5332
5333 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005334 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005335 * We can shut the read side unless we want to abort_on_close,
5336 * or we have a POST request. The issue with POST requests is
5337 * that some browsers still send a CRLF after the request, and
5338 * this CRLF must be read so that it does not remain in the kernel
5339 * buffers, otherwise a close could cause an RST on some systems
5340 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005341 * Note that if we're using keep-alive on the client side, we'd
5342 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005343 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005344 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005345 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005346 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5347 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5348 !(s->be->options & PR_O_ABRT_CLOSE) &&
5349 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005350 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005351
Willy Tarreau40f151a2012-12-20 12:10:09 +01005352 /* if the server closes the connection, we want to immediately react
5353 * and close the socket to save packets and syscalls.
5354 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005355 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005356
Willy Tarreau7f876a12015-11-18 11:59:55 +01005357 /* In any case we've finished parsing the request so we must
5358 * disable Nagle when sending data because 1) we're not going
5359 * to shut this side, and 2) the server is waiting for us to
5360 * send pending data.
5361 */
5362 chn->flags |= CF_NEVER_WAIT;
5363
Willy Tarreau610ecce2010-01-04 21:15:02 +01005364 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5365 goto wait_other_side;
5366
5367 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5368 /* The server has not finished to respond, so we
5369 * don't want to move in order not to upset it.
5370 */
5371 goto wait_other_side;
5372 }
5373
5374 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5375 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005376 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005377 txn->req.msg_state = HTTP_MSG_TUNNEL;
5378 goto wait_other_side;
5379 }
5380
5381 /* When we get here, it means that both the request and the
5382 * response have finished receiving. Depending on the connection
5383 * mode, we'll have to wait for the last bytes to leave in either
5384 * direction, and sometimes for a close to be effective.
5385 */
5386
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005387 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5388 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005389 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5390 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005391 }
5392 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5393 /* Option forceclose is set, or either side wants to close,
5394 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005395 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005396 * once both states are CLOSED.
5397 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005398 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5399 channel_shutr_now(chn);
5400 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005401 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005402 }
5403 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005404 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5405 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005406 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005407 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5408 channel_auto_read(chn);
5409 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005410 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005411 }
5412
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005413 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005414 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005415 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005416
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005417 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005418 txn->req.msg_state = HTTP_MSG_CLOSING;
5419 goto http_msg_closing;
5420 }
5421 else {
5422 txn->req.msg_state = HTTP_MSG_CLOSED;
5423 goto http_msg_closed;
5424 }
5425 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005426 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005427 }
5428
5429 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5430 http_msg_closing:
5431 /* nothing else to forward, just waiting for the output buffer
5432 * to be empty and for the shutw_now to take effect.
5433 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005434 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005435 txn->req.msg_state = HTTP_MSG_CLOSED;
5436 goto http_msg_closed;
5437 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005438 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005439 txn->req.msg_state = HTTP_MSG_ERROR;
5440 goto wait_other_side;
5441 }
5442 }
5443
5444 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5445 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005446 /* see above in MSG_DONE why we only do this in these states */
5447 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5448 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5449 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005450 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005451 goto wait_other_side;
5452 }
5453
5454 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005455 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005456}
5457
5458
5459/* This function updates the response state machine according to the request
5460 * state machine and buffer flags. It returns 1 if it changes anything (flag
5461 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5462 * it is only used to find when a request/response couple is complete. Both
5463 * this function and its equivalent should loop until both return zero. It
5464 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5465 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005466int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005467{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005468 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005469 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005470 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005471 unsigned int old_state = txn->rsp.msg_state;
5472
Willy Tarreau610ecce2010-01-04 21:15:02 +01005473 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5474 return 0;
5475
5476 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5477 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005478 * still monitor the server connection for a possible close
5479 * while the request is being uploaded, so we don't disable
5480 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005481 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005482 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005483
5484 if (txn->req.msg_state == HTTP_MSG_ERROR)
5485 goto wait_other_side;
5486
5487 if (txn->req.msg_state < HTTP_MSG_DONE) {
5488 /* The client seems to still be sending data, probably
5489 * because we got an error response during an upload.
5490 * We have the choice of either breaking the connection
5491 * or letting it pass through. Let's do the later.
5492 */
5493 goto wait_other_side;
5494 }
5495
5496 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5497 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005498 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005499 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005500 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005501 goto wait_other_side;
5502 }
5503
5504 /* When we get here, it means that both the request and the
5505 * response have finished receiving. Depending on the connection
5506 * mode, we'll have to wait for the last bytes to leave in either
5507 * direction, and sometimes for a close to be effective.
5508 */
5509
5510 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5511 /* Server-close mode : shut read and wait for the request
5512 * side to close its output buffer. The caller will detect
5513 * when we're in DONE and the other is in CLOSED and will
5514 * catch that for the final cleanup.
5515 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005516 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5517 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005518 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005519 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5520 /* Option forceclose is set, or either side wants to close,
5521 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005522 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005523 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005524 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005525 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5526 channel_shutr_now(chn);
5527 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005528 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005529 }
5530 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005531 /* The last possible modes are keep-alive and tunnel. Tunnel will
5532 * need to forward remaining data. Keep-alive will need to monitor
5533 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005534 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005535 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005536 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005537 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5538 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005539 }
5540
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005541 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005542 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005543 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005544 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5545 goto http_msg_closing;
5546 }
5547 else {
5548 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5549 goto http_msg_closed;
5550 }
5551 }
5552 goto wait_other_side;
5553 }
5554
5555 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5556 http_msg_closing:
5557 /* nothing else to forward, just waiting for the output buffer
5558 * to be empty and for the shutw_now to take effect.
5559 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005560 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005561 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5562 goto http_msg_closed;
5563 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005564 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005565 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005566 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005567 if (objt_server(s->target))
5568 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005569 goto wait_other_side;
5570 }
5571 }
5572
5573 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5574 http_msg_closed:
5575 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005576 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005577 channel_auto_close(chn);
5578 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005579 goto wait_other_side;
5580 }
5581
5582 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005583 /* We force the response to leave immediately if we're waiting for the
5584 * other side, since there is no pending shutdown to push it out.
5585 */
5586 if (!channel_is_empty(chn))
5587 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005588 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005589}
5590
5591
5592/* Resync the request and response state machines. Return 1 if either state
5593 * changes.
5594 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005595int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005596{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005597 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005598 int old_req_state = txn->req.msg_state;
5599 int old_res_state = txn->rsp.msg_state;
5600
Willy Tarreau610ecce2010-01-04 21:15:02 +01005601 http_sync_req_state(s);
5602 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005603 if (!http_sync_res_state(s))
5604 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005605 if (!http_sync_req_state(s))
5606 break;
5607 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005608
Willy Tarreau610ecce2010-01-04 21:15:02 +01005609 /* OK, both state machines agree on a compatible state.
5610 * There are a few cases we're interested in :
5611 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5612 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5613 * directions, so let's simply disable both analysers.
5614 * - HTTP_MSG_CLOSED on the response only means we must abort the
5615 * request.
5616 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5617 * with server-close mode means we've completed one request and we
5618 * must re-initialize the server connection.
5619 */
5620
5621 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5622 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5623 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5624 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005625 s->req.analysers = 0;
5626 channel_auto_close(&s->req);
5627 channel_auto_read(&s->req);
5628 s->res.analysers = 0;
5629 channel_auto_close(&s->res);
5630 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005631 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005632 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005633 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005634 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005635 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005636 s->res.analysers = 0;
5637 channel_auto_close(&s->res);
5638 channel_auto_read(&s->res);
5639 s->req.analysers = 0;
5640 channel_abort(&s->req);
5641 channel_auto_close(&s->req);
5642 channel_auto_read(&s->req);
5643 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005644 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005645 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5646 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005647 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005648 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5649 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5650 /* server-close/keep-alive: terminate this transaction,
5651 * possibly killing the server connection and reinitialize
5652 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005653 */
5654 http_end_txn_clean_session(s);
5655 }
5656
Willy Tarreau610ecce2010-01-04 21:15:02 +01005657 return txn->req.msg_state != old_req_state ||
5658 txn->rsp.msg_state != old_res_state;
5659}
5660
Willy Tarreaud98cf932009-12-27 22:54:55 +01005661/* This function is an analyser which forwards request body (including chunk
5662 * sizes if any). It is called as soon as we must forward, even if we forward
5663 * zero byte. The only situation where it must not be called is when we're in
5664 * tunnel mode and we want to forward till the close. It's used both to forward
5665 * remaining data and to resync after end of body. It expects the msg_state to
5666 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005667 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005668 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005669 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005670 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005671int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005672{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005673 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005674 struct http_txn *txn = s->txn;
5675 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005676
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005677 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5678 return 0;
5679
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005680 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005681 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005682 /* Output closed while we were sending data. We must abort and
5683 * wake the other side up.
5684 */
5685 msg->msg_state = HTTP_MSG_ERROR;
5686 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005687 return 1;
5688 }
5689
Willy Tarreaud98cf932009-12-27 22:54:55 +01005690 /* Note that we don't have to send 100-continue back because we don't
5691 * need the data to complete our job, and it's up to the server to
5692 * decide whether to return 100, 417 or anything else in return of
5693 * an "Expect: 100-continue" header.
5694 */
5695
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005696 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005697 /* we have msg->sov which points to the first byte of message
5698 * body, and req->buf.p still points to the beginning of the
5699 * message. We forward the headers now, as we don't need them
5700 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005701 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005702 b_adv(req->buf, msg->sov);
5703 msg->next -= msg->sov;
5704 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005705
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005706 /* The previous analysers guarantee that the state is somewhere
5707 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5708 * msg->next are always correct.
5709 */
5710 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5711 if (msg->flags & HTTP_MSGF_TE_CHNK)
5712 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5713 else
5714 msg->msg_state = HTTP_MSG_DATA;
5715 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005716 }
5717
Willy Tarreau7ba23542014-04-17 21:50:00 +02005718 /* Some post-connect processing might want us to refrain from starting to
5719 * forward data. Currently, the only reason for this is "balance url_param"
5720 * whichs need to parse/process the request after we've enabled forwarding.
5721 */
5722 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005723 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005724 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005725 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005726 goto missing_data;
5727 }
5728 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5729 }
5730
Willy Tarreau80a92c02014-03-12 10:41:13 +01005731 /* in most states, we should abort in case of early close */
5732 channel_auto_close(req);
5733
Willy Tarreauefdf0942014-04-24 20:08:57 +02005734 if (req->to_forward) {
5735 /* We can't process the buffer's contents yet */
5736 req->flags |= CF_WAKE_WRITE;
5737 goto missing_data;
5738 }
5739
Willy Tarreaud98cf932009-12-27 22:54:55 +01005740 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005741 if (msg->msg_state == HTTP_MSG_DATA) {
5742 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005743 /* we may have some pending data starting at req->buf->p */
5744 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005745 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005746 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005747 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005748 msg->next += msg->chunk_len;
5749 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005750
5751 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005752 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005753 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005754 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005755 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005756 }
5757 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005758 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005759 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005760 * TRAILERS state.
5761 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005762 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005763
Willy Tarreau54d23df2012-10-25 19:04:45 +02005764 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005765 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005766 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005767 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005768 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005769 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005770 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005771 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005772 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005773 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005774 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005775 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005776 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005777
5778 if (ret == 0)
5779 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005780 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005781 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005782 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005783 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005784 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005785 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005786 /* we're in MSG_CHUNK_SIZE now */
5787 }
5788 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005789 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005790
5791 if (ret == 0)
5792 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005793 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005794 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005795 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005796 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005797 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005798 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005799 /* we're in HTTP_MSG_DONE now */
5800 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005801 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005802 int old_state = msg->msg_state;
5803
Willy Tarreau610ecce2010-01-04 21:15:02 +01005804 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005805
5806 /* we may have some pending data starting at req->buf->p
5807 * such as last chunk of data or trailers.
5808 */
5809 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005810 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005811 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005812 msg->next = 0;
5813
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005814 /* we don't want to forward closes on DONE except in
5815 * tunnel mode.
5816 */
5817 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005818 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005819 if (http_resync_states(s)) {
5820 /* some state changes occurred, maybe the analyser
5821 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005822 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005823 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005824 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005825 /* request errors are most likely due to
5826 * the server aborting the transfer.
5827 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005828 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005829 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005830 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005831 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005832 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005833 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005834 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005835 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005836
5837 /* If "option abortonclose" is set on the backend, we
5838 * want to monitor the client's connection and forward
5839 * any shutdown notification to the server, which will
5840 * decide whether to close or to go on processing the
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005841 * request. We only do that in tunnel mode, and not in
5842 * other modes since it can be abused to exhaust source
5843 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005844 */
5845 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005846 channel_auto_read(req);
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005847 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5848 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5849 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005850 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005851 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005852 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005853 /* POST requests may require to read extra CRLF
5854 * sent by broken browsers and which could cause
5855 * an RST to be sent upon close on some systems
5856 * (eg: Linux).
5857 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005858 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005859 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005860
Willy Tarreau610ecce2010-01-04 21:15:02 +01005861 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005862 }
5863 }
5864
Willy Tarreaud98cf932009-12-27 22:54:55 +01005865 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005866 /* we may have some pending data starting at req->buf->p */
5867 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005868 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005869 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5870
Willy Tarreaubed410e2014-04-22 08:19:34 +02005871 msg->next = 0;
5872 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5873
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005874 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005875 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005876 if (!(s->flags & SF_ERR_MASK))
5877 s->flags |= SF_ERR_CLICL;
5878 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005879 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005880 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005881 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005882 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005883 }
5884
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005885 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005886 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005887 if (objt_server(s->target))
5888 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005889
5890 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005891 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005892
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005893 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005894 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005895 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005896
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005897 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005898 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005899 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005900 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005901 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005902
Willy Tarreau5c620922011-05-11 19:56:11 +02005903 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005904 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005905 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005906 * modes are already handled by the stream sock layer. We must not do
5907 * this in content-length mode because it could present the MSG_MORE
5908 * flag with the last block of forwarded data, which would cause an
5909 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005910 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005911 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005912 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005913
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005914 return 0;
5915
Willy Tarreaud98cf932009-12-27 22:54:55 +01005916 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005917 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005918 if (sess->listener->counters)
5919 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005920
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005921 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005922 /* we may have some pending data starting at req->buf->p */
5923 b_adv(req->buf, msg->next);
5924 msg->next = 0;
5925
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005926 txn->req.msg_state = HTTP_MSG_ERROR;
5927 if (txn->status) {
5928 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005929 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005930 } else {
5931 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005932 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005933 }
5934 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005935 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005936
Willy Tarreaue7dff022015-04-03 01:14:29 +02005937 if (!(s->flags & SF_ERR_MASK))
5938 s->flags |= SF_ERR_PRXCOND;
5939 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005940 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005941 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005942 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005943 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005944 }
5945 return 0;
5946
5947 aborted_xfer:
5948 txn->req.msg_state = HTTP_MSG_ERROR;
5949 if (txn->status) {
5950 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005951 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005952 } else {
5953 txn->status = 502;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005954 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005955 }
5956 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005957 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005958
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005959 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005960 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005961 if (objt_server(s->target))
5962 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005963
Willy Tarreaue7dff022015-04-03 01:14:29 +02005964 if (!(s->flags & SF_ERR_MASK))
5965 s->flags |= SF_ERR_SRVCL;
5966 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005967 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005968 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005969 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005970 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005971 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005972 return 0;
5973}
5974
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005975/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5976 * processing can continue on next analysers, or zero if it either needs more
5977 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005978 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005979 * when it has nothing left to do, and may remove any analyser when it wants to
5980 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005981 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005982int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005983{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005984 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005985 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005986 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005987 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005988 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005989 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005990 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005991
Willy Tarreau87b09662015-04-03 00:22:06 +02005992 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 +02005993 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005994 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005995 rep,
5996 rep->rex, rep->wex,
5997 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005998 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005999 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02006000
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006001 /*
6002 * Now parse the partial (or complete) lines.
6003 * We will check the response syntax, and also join multi-line
6004 * headers. An index of all the lines will be elaborated while
6005 * parsing.
6006 *
6007 * For the parsing, we use a 28 states FSM.
6008 *
6009 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02006010 * rep->buf->p = beginning of response
6011 * rep->buf->p + msg->eoh = end of processed headers / start of current one
6012 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02006013 * msg->eol = end of current header or line (LF or CRLF)
6014 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006015 */
6016
Willy Tarreau628c40c2014-04-24 19:11:26 +02006017 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01006018 /* There's a protected area at the end of the buffer for rewriting
6019 * purposes. We don't want to start to parse the request if the
6020 * protected area is affected, because we may have to move processed
6021 * data later, which is much more complicated.
6022 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006023 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01006024 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02006025 /* some data has still not left the buffer, wake us once that's done */
6026 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
6027 goto abort_response;
6028 channel_dont_close(rep);
6029 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01006030 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02006031 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01006032 }
6033
Willy Tarreau379357a2013-06-08 12:55:46 +02006034 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
6035 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
6036 buffer_slow_realign(rep->buf);
6037
Willy Tarreau9b28e032012-10-12 23:49:43 +02006038 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01006039 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01006040 }
6041
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006042 /* 1: we might have to print this header in debug mode */
6043 if (unlikely((global.mode & MODE_DEBUG) &&
6044 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02006045 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006046 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006047
Willy Tarreau9b28e032012-10-12 23:49:43 +02006048 sol = rep->buf->p;
6049 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006050 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006051
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006052 sol += hdr_idx_first_pos(&txn->hdr_idx);
6053 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006054
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006055 while (cur_idx) {
6056 eol = sol + txn->hdr_idx.v[cur_idx].len;
6057 debug_hdr("srvhdr", s, sol, eol);
6058 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
6059 cur_idx = txn->hdr_idx.v[cur_idx].next;
6060 }
6061 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006062
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006063 /*
6064 * Now we quickly check if we have found a full valid response.
6065 * If not so, we check the FD and buffer states before leaving.
6066 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01006067 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006068 * responses are checked first.
6069 *
6070 * Depending on whether the client is still there or not, we
6071 * may send an error response back or not. Note that normally
6072 * we should only check for HTTP status there, and check I/O
6073 * errors somewhere else.
6074 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006075
Willy Tarreau655dce92009-11-08 13:10:58 +01006076 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006077 /* Invalid response */
6078 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6079 /* we detected a parsing error. We want to archive this response
6080 * in the dedicated proxy area for later troubleshooting.
6081 */
6082 hdr_response_bad:
6083 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006084 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006085
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006086 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006087 if (objt_server(s->target)) {
6088 objt_server(s->target)->counters.failed_resp++;
6089 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006090 }
Willy Tarreau64648412010-03-05 10:41:54 +01006091 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006092 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006093 rep->analysers = 0;
6094 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006095 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006096 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006097 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006098
Willy Tarreaue7dff022015-04-03 01:14:29 +02006099 if (!(s->flags & SF_ERR_MASK))
6100 s->flags |= SF_ERR_PRXCOND;
6101 if (!(s->flags & SF_FINST_MASK))
6102 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006103
6104 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006105 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006106
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006107 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006108 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006109 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006110 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006111 goto hdr_response_bad;
6112 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006113
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006114 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006115 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006116 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006117 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006118 else if (txn->flags & TX_NOT_FIRST)
6119 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006120
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006121 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006122 if (objt_server(s->target)) {
6123 objt_server(s->target)->counters.failed_resp++;
6124 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006125 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006126
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006127 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006128 rep->analysers = 0;
6129 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006130 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006131 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006132 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006133
Willy Tarreaue7dff022015-04-03 01:14:29 +02006134 if (!(s->flags & SF_ERR_MASK))
6135 s->flags |= SF_ERR_SRVCL;
6136 if (!(s->flags & SF_FINST_MASK))
6137 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006138 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006139 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006140
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006141 /* read timeout : return a 504 to the client. */
6142 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006143 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006144 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006145
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006146 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006147 if (objt_server(s->target)) {
6148 objt_server(s->target)->counters.failed_resp++;
6149 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006150 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006151
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006152 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006153 rep->analysers = 0;
6154 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006155 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006156 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006157 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006158
Willy Tarreaue7dff022015-04-03 01:14:29 +02006159 if (!(s->flags & SF_ERR_MASK))
6160 s->flags |= SF_ERR_SRVTO;
6161 if (!(s->flags & SF_FINST_MASK))
6162 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006163 return 0;
6164 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006165
Willy Tarreauf003d372012-11-26 13:35:37 +01006166 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006167 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006168 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006169 s->be->be_counters.cli_aborts++;
6170 if (objt_server(s->target))
6171 objt_server(s->target)->counters.cli_aborts++;
6172
6173 rep->analysers = 0;
6174 channel_auto_close(rep);
6175
6176 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006177 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006178 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006179
Willy Tarreaue7dff022015-04-03 01:14:29 +02006180 if (!(s->flags & SF_ERR_MASK))
6181 s->flags |= SF_ERR_CLICL;
6182 if (!(s->flags & SF_FINST_MASK))
6183 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006184
Willy Tarreau87b09662015-04-03 00:22:06 +02006185 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006186 return 0;
6187 }
6188
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006189 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006190 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006191 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006192 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006193 else if (txn->flags & TX_NOT_FIRST)
6194 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006195
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006196 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006197 if (objt_server(s->target)) {
6198 objt_server(s->target)->counters.failed_resp++;
6199 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006200 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006201
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006202 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006203 rep->analysers = 0;
6204 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006205 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006206 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006207 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006208
Willy Tarreaue7dff022015-04-03 01:14:29 +02006209 if (!(s->flags & SF_ERR_MASK))
6210 s->flags |= SF_ERR_SRVCL;
6211 if (!(s->flags & SF_FINST_MASK))
6212 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006213 return 0;
6214 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006215
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006216 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006217 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006218 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006219 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006220 else if (txn->flags & TX_NOT_FIRST)
6221 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006222
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006223 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006224 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006225 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006226
Willy Tarreaue7dff022015-04-03 01:14:29 +02006227 if (!(s->flags & SF_ERR_MASK))
6228 s->flags |= SF_ERR_CLICL;
6229 if (!(s->flags & SF_FINST_MASK))
6230 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006231
Willy Tarreau87b09662015-04-03 00:22:06 +02006232 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006233 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006234 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006235
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006236 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006237 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006238 return 0;
6239 }
6240
6241 /* More interesting part now : we know that we have a complete
6242 * response which at least looks like HTTP. We have an indicator
6243 * of each header's length, so we can parse them quickly.
6244 */
6245
6246 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006247 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006248
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006249 /*
6250 * 1: get the status code
6251 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006252 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006253 if (n < 1 || n > 5)
6254 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006255 /* when the client triggers a 4xx from the server, it's most often due
6256 * to a missing object or permission. These events should be tracked
6257 * because if they happen often, it may indicate a brute force or a
6258 * vulnerability scan.
6259 */
6260 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006261 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006262
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006263 if (objt_server(s->target))
6264 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006265
Willy Tarreau91852eb2015-05-01 13:26:00 +02006266 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6267 * exactly one digit "." one digit. This check may be disabled using
6268 * option accept-invalid-http-response.
6269 */
6270 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6271 if (msg->sl.st.v_l != 8) {
6272 msg->err_pos = 0;
6273 goto hdr_response_bad;
6274 }
6275
6276 if (rep->buf->p[4] != '/' ||
6277 !isdigit((unsigned char)rep->buf->p[5]) ||
6278 rep->buf->p[6] != '.' ||
6279 !isdigit((unsigned char)rep->buf->p[7])) {
6280 msg->err_pos = 4;
6281 goto hdr_response_bad;
6282 }
6283 }
6284
Willy Tarreau5b154472009-12-21 20:11:07 +01006285 /* check if the response is HTTP/1.1 or above */
6286 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006287 ((rep->buf->p[5] > '1') ||
6288 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006289 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006290
6291 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006292 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 +01006293
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006294 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006295 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006296
Willy Tarreau9b28e032012-10-12 23:49:43 +02006297 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006298
Willy Tarreau39650402010-03-15 19:44:39 +01006299 /* Adjust server's health based on status code. Note: status codes 501
6300 * and 505 are triggered on demand by client request, so we must not
6301 * count them as server failures.
6302 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006303 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006304 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006305 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006306 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006307 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006308 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006309
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006310 /*
6311 * 2: check for cacheability.
6312 */
6313
6314 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006315 case 100:
6316 /*
6317 * We may be facing a 100-continue response, in which case this
6318 * is not the right response, and we're waiting for the next one.
6319 * Let's allow this response to go to the client and wait for the
6320 * next one.
6321 */
6322 hdr_idx_init(&txn->hdr_idx);
6323 msg->next -= channel_forward(rep, msg->next);
6324 msg->msg_state = HTTP_MSG_RPBEFORE;
6325 txn->status = 0;
6326 s->logs.t_data = -1; /* was not a response yet */
6327 goto next_one;
6328
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006329 case 200:
6330 case 203:
6331 case 206:
6332 case 300:
6333 case 301:
6334 case 410:
6335 /* RFC2616 @13.4:
6336 * "A response received with a status code of
6337 * 200, 203, 206, 300, 301 or 410 MAY be stored
6338 * by a cache (...) unless a cache-control
6339 * directive prohibits caching."
6340 *
6341 * RFC2616 @9.5: POST method :
6342 * "Responses to this method are not cacheable,
6343 * unless the response includes appropriate
6344 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006345 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006346 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006347 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006348 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6349 break;
6350 default:
6351 break;
6352 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006353
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006354 /*
6355 * 3: we may need to capture headers
6356 */
6357 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006358 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006359 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006360 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006361
Willy Tarreau557f1992015-05-01 10:05:17 +02006362 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6363 * by RFC7230#3.3.3 :
6364 *
6365 * The length of a message body is determined by one of the following
6366 * (in order of precedence):
6367 *
6368 * 1. Any response to a HEAD request and any response with a 1xx
6369 * (Informational), 204 (No Content), or 304 (Not Modified) status
6370 * code is always terminated by the first empty line after the
6371 * header fields, regardless of the header fields present in the
6372 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006373 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006374 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6375 * the connection will become a tunnel immediately after the empty
6376 * line that concludes the header fields. A client MUST ignore any
6377 * Content-Length or Transfer-Encoding header fields received in
6378 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006379 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006380 * 3. If a Transfer-Encoding header field is present and the chunked
6381 * transfer coding (Section 4.1) is the final encoding, the message
6382 * body length is determined by reading and decoding the chunked
6383 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006384 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006385 * If a Transfer-Encoding header field is present in a response and
6386 * the chunked transfer coding is not the final encoding, the
6387 * message body length is determined by reading the connection until
6388 * it is closed by the server. If a Transfer-Encoding header field
6389 * is present in a request and the chunked transfer coding is not
6390 * the final encoding, the message body length cannot be determined
6391 * reliably; the server MUST respond with the 400 (Bad Request)
6392 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006393 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006394 * If a message is received with both a Transfer-Encoding and a
6395 * Content-Length header field, the Transfer-Encoding overrides the
6396 * Content-Length. Such a message might indicate an attempt to
6397 * perform request smuggling (Section 9.5) or response splitting
6398 * (Section 9.4) and ought to be handled as an error. A sender MUST
6399 * remove the received Content-Length field prior to forwarding such
6400 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006401 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006402 * 4. If a message is received without Transfer-Encoding and with
6403 * either multiple Content-Length header fields having differing
6404 * field-values or a single Content-Length header field having an
6405 * invalid value, then the message framing is invalid and the
6406 * recipient MUST treat it as an unrecoverable error. If this is a
6407 * request message, the server MUST respond with a 400 (Bad Request)
6408 * status code and then close the connection. If this is a response
6409 * message received by a proxy, the proxy MUST close the connection
6410 * to the server, discard the received response, and send a 502 (Bad
6411 * Gateway) response to the client. If this is a response message
6412 * received by a user agent, the user agent MUST close the
6413 * connection to the server and discard the received response.
6414 *
6415 * 5. If a valid Content-Length header field is present without
6416 * Transfer-Encoding, its decimal value defines the expected message
6417 * body length in octets. If the sender closes the connection or
6418 * the recipient times out before the indicated number of octets are
6419 * received, the recipient MUST consider the message to be
6420 * incomplete and close the connection.
6421 *
6422 * 6. If this is a request message and none of the above are true, then
6423 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006424 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006425 * 7. Otherwise, this is a response message without a declared message
6426 * body length, so the message body length is determined by the
6427 * number of octets received prior to the server closing the
6428 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006429 */
6430
6431 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006432 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006433 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006434 * FIXME: should we parse anyway and return an error on chunked encoding ?
6435 */
6436 if (txn->meth == HTTP_METH_HEAD ||
6437 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006438 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006439 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006440 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006441 goto skip_content_length;
6442 }
6443
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006444 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006445 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006446 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006447 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006448 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6449 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006450 /* bad transfer-encoding (chunked followed by something else) */
6451 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006452 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006453 break;
6454 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006455 }
6456
Willy Tarreau1c913912015-04-30 10:57:51 +02006457 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006458 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006459 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006460 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6461 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6462 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006463 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006464 signed long long cl;
6465
Willy Tarreauad14f752011-09-02 20:33:27 +02006466 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006467 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006468 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006469 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006470
Willy Tarreauad14f752011-09-02 20:33:27 +02006471 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006472 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006473 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006474 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006475
Willy Tarreauad14f752011-09-02 20:33:27 +02006476 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006477 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006478 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006479 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006480
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006481 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006482 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006483 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006484 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006485
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006486 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006487 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006488 }
6489
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006490 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006491 select_compression_response_header(s, rep->buf);
6492
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006493skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006494 /* Now we have to check if we need to modify the Connection header.
6495 * This is more difficult on the response than it is on the request,
6496 * because we can have two different HTTP versions and we don't know
6497 * how the client will interprete a response. For instance, let's say
6498 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6499 * HTTP/1.1 response without any header. Maybe it will bound itself to
6500 * HTTP/1.0 because it only knows about it, and will consider the lack
6501 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6502 * the lack of header as a keep-alive. Thus we will use two flags
6503 * indicating how a request MAY be understood by the client. In case
6504 * of multiple possibilities, we'll fix the header to be explicit. If
6505 * ambiguous cases such as both close and keepalive are seen, then we
6506 * will fall back to explicit close. Note that we won't take risks with
6507 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006508 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006509 */
6510
Willy Tarreaudc008c52010-02-01 16:20:08 +01006511 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6512 txn->status == 101)) {
6513 /* Either we've established an explicit tunnel, or we're
6514 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006515 * to understand the next protocols. We have to switch to tunnel
6516 * mode, so that we transfer the request and responses then let
6517 * this protocol pass unmodified. When we later implement specific
6518 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006519 * header which contains information about that protocol for
6520 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006521 */
6522 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6523 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006524 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6525 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006526 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006527 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006528 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006529
Willy Tarreau70dffda2014-01-30 03:07:23 +01006530 /* this situation happens when combining pretend-keepalive with httpclose. */
6531 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006532 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006533 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006534 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6535
Willy Tarreau60466522010-01-18 19:08:45 +01006536 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006537 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006538 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6539 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006540
Willy Tarreau60466522010-01-18 19:08:45 +01006541 /* now adjust header transformations depending on current state */
6542 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6543 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6544 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006545 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006546 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006547 }
Willy Tarreau60466522010-01-18 19:08:45 +01006548 else { /* SCL / KAL */
6549 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006550 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006551 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006552 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006553
Willy Tarreau60466522010-01-18 19:08:45 +01006554 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006555 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006556
Willy Tarreau60466522010-01-18 19:08:45 +01006557 /* Some keep-alive responses are converted to Server-close if
6558 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006559 */
Willy Tarreau60466522010-01-18 19:08:45 +01006560 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6561 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006562 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006563 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006564 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006565 }
6566
Willy Tarreau7959a552013-09-23 16:44:27 +02006567 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006568 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006569
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006570 /* end of job, return OK */
6571 rep->analysers &= ~an_bit;
6572 rep->analyse_exp = TICK_ETERNITY;
6573 channel_auto_close(rep);
6574 return 1;
6575
6576 abort_keep_alive:
6577 /* A keep-alive request to the server failed on a network error.
6578 * The client is required to retry. We need to close without returning
6579 * any other information so that the client retries.
6580 */
6581 txn->status = 0;
6582 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006583 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006584 channel_auto_close(rep);
6585 s->logs.logwait = 0;
6586 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006587 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006588 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006589 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006590 return 0;
6591}
6592
6593/* This function performs all the processing enabled for the current response.
6594 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006595 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006596 * other functions. It works like process_request (see indications above).
6597 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006598int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006599{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006600 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006601 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006602 struct http_msg *msg = &txn->rsp;
6603 struct proxy *cur_proxy;
6604 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006605 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006606
Willy Tarreau87b09662015-04-03 00:22:06 +02006607 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 +02006608 now_ms, __FUNCTION__,
6609 s,
6610 rep,
6611 rep->rex, rep->wex,
6612 rep->flags,
6613 rep->buf->i,
6614 rep->analysers);
6615
6616 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6617 return 0;
6618
Willy Tarreau70730dd2014-04-24 18:06:27 +02006619 /* The stats applet needs to adjust the Connection header but we don't
6620 * apply any filter there.
6621 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006622 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6623 rep->analysers &= ~an_bit;
6624 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006625 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006626 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006627
Willy Tarreau58975672014-04-24 21:13:57 +02006628 /*
6629 * We will have to evaluate the filters.
6630 * As opposed to version 1.2, now they will be evaluated in the
6631 * filters order and not in the header order. This means that
6632 * each filter has to be validated among all headers.
6633 *
6634 * Filters are tried with ->be first, then with ->fe if it is
6635 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006636 *
6637 * Maybe we are in resume condiion. In this case I choose the
6638 * "struct proxy" which contains the rule list matching the resume
6639 * pointer. If none of theses "struct proxy" match, I initialise
6640 * the process with the first one.
6641 *
6642 * In fact, I check only correspondance betwwen the current list
6643 * pointer and the ->fe rule list. If it doesn't match, I initialize
6644 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006645 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006646 if (s->current_rule_list == &sess->fe->http_res_rules)
6647 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006648 else
6649 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006650 while (1) {
6651 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006652
Willy Tarreau58975672014-04-24 21:13:57 +02006653 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006654 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006655 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006656
Willy Tarreau51d861a2015-05-22 17:30:48 +02006657 if (ret == HTTP_RULE_RES_BADREQ)
6658 goto return_srv_prx_502;
6659
6660 if (ret == HTTP_RULE_RES_DONE) {
6661 rep->analysers &= ~an_bit;
6662 rep->analyse_exp = TICK_ETERNITY;
6663 return 1;
6664 }
6665 }
6666
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006667 /* we need to be called again. */
6668 if (ret == HTTP_RULE_RES_YIELD) {
6669 channel_dont_close(rep);
6670 return 0;
6671 }
6672
Willy Tarreau58975672014-04-24 21:13:57 +02006673 /* try headers filters */
6674 if (rule_set->rsp_exp != NULL) {
6675 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6676 return_bad_resp:
6677 if (objt_server(s->target)) {
6678 objt_server(s->target)->counters.failed_resp++;
6679 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006680 }
Willy Tarreau58975672014-04-24 21:13:57 +02006681 s->be->be_counters.failed_resp++;
6682 return_srv_prx_502:
6683 rep->analysers = 0;
6684 txn->status = 502;
6685 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006686 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006687 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006688 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006689 if (!(s->flags & SF_ERR_MASK))
6690 s->flags |= SF_ERR_PRXCOND;
6691 if (!(s->flags & SF_FINST_MASK))
6692 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006693 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006694 }
Willy Tarreau58975672014-04-24 21:13:57 +02006695 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006696
Willy Tarreau58975672014-04-24 21:13:57 +02006697 /* has the response been denied ? */
6698 if (txn->flags & TX_SVDENY) {
6699 if (objt_server(s->target))
6700 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006701
Willy Tarreau58975672014-04-24 21:13:57 +02006702 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006703 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006704 if (sess->listener->counters)
6705 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006706
Willy Tarreau58975672014-04-24 21:13:57 +02006707 goto return_srv_prx_502;
6708 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006709
Willy Tarreau58975672014-04-24 21:13:57 +02006710 /* add response headers from the rule sets in the same order */
6711 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006712 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006713 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006714 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006715 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006716 ret = acl_pass(ret);
6717 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6718 ret = !ret;
6719 if (!ret)
6720 continue;
6721 }
6722 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6723 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006724 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006725
Willy Tarreau58975672014-04-24 21:13:57 +02006726 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006727 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006728 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006729 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006730 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006731
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006732 /* After this point, this anayzer can't return yield, so we can
6733 * remove the bit corresponding to this analyzer from the list.
6734 *
6735 * Note that the intermediate returns and goto found previously
6736 * reset the analyzers.
6737 */
6738 rep->analysers &= ~an_bit;
6739 rep->analyse_exp = TICK_ETERNITY;
6740
Willy Tarreau58975672014-04-24 21:13:57 +02006741 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006742 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006743 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006744
Willy Tarreau58975672014-04-24 21:13:57 +02006745 /*
6746 * Now check for a server cookie.
6747 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006748 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006749 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006750
Willy Tarreau58975672014-04-24 21:13:57 +02006751 /*
6752 * Check for cache-control or pragma headers if required.
6753 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006754 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 +02006755 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006756
Willy Tarreau58975672014-04-24 21:13:57 +02006757 /*
6758 * Add server cookie in the response if needed
6759 */
6760 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6761 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006762 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006763 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6764 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6765 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6766 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6767 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006768 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006769 /* the server is known, it's not the one the client requested, or the
6770 * cookie's last seen date needs to be refreshed. We have to
6771 * insert a set-cookie here, except if we want to insert only on POST
6772 * requests and this one isn't. Note that servers which don't have cookies
6773 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006774 */
Willy Tarreau58975672014-04-24 21:13:57 +02006775 if (!objt_server(s->target)->cookie) {
6776 chunk_printf(&trash,
6777 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6778 s->be->cookie_name);
6779 }
6780 else {
6781 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006782
Willy Tarreau58975672014-04-24 21:13:57 +02006783 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6784 /* emit last_date, which is mandatory */
6785 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6786 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6787 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006788
Willy Tarreau58975672014-04-24 21:13:57 +02006789 if (s->be->cookie_maxlife) {
6790 /* emit first_date, which is either the original one or
6791 * the current date.
6792 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006793 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006794 s30tob64(txn->cookie_first_date ?
6795 txn->cookie_first_date >> 2 :
6796 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006797 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006798 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006799 }
Willy Tarreau58975672014-04-24 21:13:57 +02006800 chunk_appendf(&trash, "; path=/");
6801 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006802
Willy Tarreau58975672014-04-24 21:13:57 +02006803 if (s->be->cookie_domain)
6804 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006805
Willy Tarreau58975672014-04-24 21:13:57 +02006806 if (s->be->ck_opts & PR_CK_HTTPONLY)
6807 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006808
Willy Tarreau58975672014-04-24 21:13:57 +02006809 if (s->be->ck_opts & PR_CK_SECURE)
6810 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006811
Willy Tarreau58975672014-04-24 21:13:57 +02006812 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6813 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006814
Willy Tarreau58975672014-04-24 21:13:57 +02006815 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006816 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006817 /* the server did not change, only the date was updated */
6818 txn->flags |= TX_SCK_UPDATED;
6819 else
6820 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006821
Willy Tarreau58975672014-04-24 21:13:57 +02006822 /* Here, we will tell an eventual cache on the client side that we don't
6823 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6824 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6825 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006826 */
Willy Tarreau58975672014-04-24 21:13:57 +02006827 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006828
Willy Tarreau58975672014-04-24 21:13:57 +02006829 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006830
Willy Tarreau58975672014-04-24 21:13:57 +02006831 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6832 "Cache-control: private", 22) < 0))
6833 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006834 }
Willy Tarreau58975672014-04-24 21:13:57 +02006835 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006836
Willy Tarreau58975672014-04-24 21:13:57 +02006837 /*
6838 * Check if result will be cacheable with a cookie.
6839 * We'll block the response if security checks have caught
6840 * nasty things such as a cacheable cookie.
6841 */
6842 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6843 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6844 (s->be->options & PR_O_CHK_CACHE)) {
6845 /* we're in presence of a cacheable response containing
6846 * a set-cookie header. We'll block it as requested by
6847 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006848 */
Willy Tarreau58975672014-04-24 21:13:57 +02006849 if (objt_server(s->target))
6850 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006851
Willy Tarreau58975672014-04-24 21:13:57 +02006852 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006853 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006854 if (sess->listener->counters)
6855 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006856
Willy Tarreau58975672014-04-24 21:13:57 +02006857 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6858 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6859 send_log(s->be, LOG_ALERT,
6860 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6861 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6862 goto return_srv_prx_502;
6863 }
Willy Tarreau03945942009-12-22 16:50:27 +01006864
Willy Tarreau70730dd2014-04-24 18:06:27 +02006865 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006866 /*
6867 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6868 * If an "Upgrade" token is found, the header is left untouched in order
6869 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006870 * if anything but "Upgrade" is present in the Connection header. We don't
6871 * want to touch any 101 response either since it's switching to another
6872 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006873 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006874 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006875 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006876 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006877 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6878 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006879
Willy Tarreau58975672014-04-24 21:13:57 +02006880 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6881 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6882 /* we want a keep-alive response here. Keep-alive header
6883 * required if either side is not 1.1.
6884 */
6885 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6886 want_flags |= TX_CON_KAL_SET;
6887 }
6888 else {
6889 /* we want a close response here. Close header required if
6890 * the server is 1.1, regardless of the client.
6891 */
6892 if (msg->flags & HTTP_MSGF_VER_11)
6893 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006894 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006895
Willy Tarreau58975672014-04-24 21:13:57 +02006896 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6897 http_change_connection_header(txn, msg, want_flags);
6898 }
6899
6900 skip_header_mangling:
6901 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6902 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6903 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006904
Willy Tarreau58975672014-04-24 21:13:57 +02006905 /* if the user wants to log as soon as possible, without counting
6906 * bytes from the server, then this is the right moment. We have
6907 * to temporarily assign bytes_out to log what we currently have.
6908 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006909 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006910 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6911 s->logs.bytes_out = txn->rsp.eoh;
6912 s->do_log(s);
6913 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006914 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006915 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006916}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006917
Willy Tarreaud98cf932009-12-27 22:54:55 +01006918/* This function is an analyser which forwards response body (including chunk
6919 * sizes if any). It is called as soon as we must forward, even if we forward
6920 * zero byte. The only situation where it must not be called is when we're in
6921 * tunnel mode and we want to forward till the close. It's used both to forward
6922 * remaining data and to resync after end of body. It expects the msg_state to
6923 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006924 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006925 *
6926 * It is capable of compressing response data both in content-length mode and
6927 * in chunked mode. The state machines follows different flows depending on
6928 * whether content-length and chunked modes are used, since there are no
6929 * trailers in content-length :
6930 *
6931 * chk-mode cl-mode
6932 * ,----- BODY -----.
6933 * / \
6934 * V size > 0 V chk-mode
6935 * .--> SIZE -------------> DATA -------------> CRLF
6936 * | | size == 0 | last byte |
6937 * | v final crlf v inspected |
6938 * | TRAILERS -----------> DONE |
6939 * | |
6940 * `----------------------------------------------'
6941 *
6942 * Compression only happens in the DATA state, and must be flushed in final
6943 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6944 * is performed at once on final states for all bytes parsed, or when leaving
6945 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006946 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006947int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006948{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006949 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006950 struct http_txn *txn = s->txn;
6951 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006952 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006953 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006954 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006955
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006956 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6957 return 0;
6958
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006959 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006960 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006961 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006962 /* Output closed while we were sending data. We must abort and
6963 * wake the other side up.
6964 */
6965 msg->msg_state = HTTP_MSG_ERROR;
6966 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006967 return 1;
6968 }
6969
Willy Tarreau4fe41902010-06-07 22:27:41 +02006970 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006971 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006972
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006973 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006974 /* we have msg->sov which points to the first byte of message
6975 * body, and res->buf.p still points to the beginning of the
6976 * message. We forward the headers now, as we don't need them
6977 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006978 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006979 b_adv(res->buf, msg->sov);
6980 msg->next -= msg->sov;
6981 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006982
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006983 /* The previous analysers guarantee that the state is somewhere
6984 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6985 * msg->next are always correct.
6986 */
6987 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6988 if (msg->flags & HTTP_MSGF_TE_CHNK)
6989 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6990 else
6991 msg->msg_state = HTTP_MSG_DATA;
6992 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006993 }
6994
Willy Tarreauefdf0942014-04-24 20:08:57 +02006995 if (res->to_forward) {
6996 /* We can't process the buffer's contents yet */
6997 res->flags |= CF_WAKE_WRITE;
6998 goto missing_data;
6999 }
7000
Willy Tarreau32b5ab22014-04-21 11:27:29 +02007001 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
7002 /* We need a compression buffer in the DATA state to put the
7003 * output of compressed data, and in CRLF state to let the
7004 * TRAILERS state finish the job of removing the trailing CRLF.
7005 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01007006 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02007007 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01007008 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02007009 goto aborted_xfer; /* no memory */
7010 }
7011
7012 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01007013 if (ret < 0) {
7014 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02007015 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01007016 }
William Lallemand82fe75c2012-10-23 10:25:10 +02007017 compressing = 1;
7018 }
7019
Willy Tarreaud98cf932009-12-27 22:54:55 +01007020 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007021 switch (msg->msg_state - HTTP_MSG_DATA) {
7022 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02007023 /* we may have some pending data starting at res->buf->p */
7024 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02007025 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01007026 if (ret < 0)
7027 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02007028
Willy Tarreaud5a67832014-04-21 10:54:27 +02007029 if (msg->chunk_len) {
7030 /* input empty or output full */
7031 if (res->buf->i > msg->next)
7032 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02007033 goto missing_data;
7034 }
William Lallemandbf3ae612012-11-19 12:35:37 +01007035 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007036 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02007037 if (msg->chunk_len > res->buf->i - msg->next) {
7038 /* output full */
7039 res->flags |= CF_WAKE_WRITE;
7040 goto missing_data;
7041 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007042 msg->next += msg->chunk_len;
7043 msg->chunk_len = 0;
7044 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01007045
7046 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01007047 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02007048 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01007049 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01007050 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007051 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01007052 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007053 /* fall through for HTTP_MSG_CHUNK_CRLF */
7054
7055 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
7056 /* we want the CRLF after the data */
7057
7058 ret = http_skip_chunk_crlf(msg);
7059 if (ret == 0)
7060 goto missing_data;
7061 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_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007064 goto return_bad_res;
7065 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007066 /* we're in MSG_CHUNK_SIZE now, fall through */
7067
7068 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01007069 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02007070 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01007071 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01007072 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007073
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007074 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02007075 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01007076 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007077 else if (ret < 0) {
7078 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007079 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007080 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007081 }
Willy Tarreau0161d622013-04-02 01:26:55 +02007082 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007083 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01007084
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007085 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02007086 if (unlikely(compressing)) {
7087 /* we need to flush output contents before syncing FSMs */
7088 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7089 compressing = 0;
7090 }
7091
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007092 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007093 if (ret == 0)
7094 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007095 else if (ret < 0) {
7096 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007097 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007098 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007099 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02007100 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007101
7102 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01007103 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007104 if (unlikely(compressing)) {
7105 /* we need to flush output contents before syncing FSMs */
7106 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7107 compressing = 0;
7108 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007109
Willy Tarreauc623c172014-04-18 09:53:50 +02007110 /* we may have some pending data starting at res->buf->p
7111 * such as a last chunk of data or trailers.
7112 */
7113 b_adv(res->buf, msg->next);
7114 msg->next = 0;
7115
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007116 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02007117 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007118 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7119 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007120 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02007121
Willy Tarreau610ecce2010-01-04 21:15:02 +01007122 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01007123 /* some state changes occurred, maybe the analyser
7124 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01007125 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007126 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007127 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007128 /* response errors are most likely due to
7129 * the client aborting the transfer.
7130 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007131 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007132 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007133 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007134 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01007135 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007136 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007137 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01007138 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007139 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007140 }
7141 }
7142
Willy Tarreaud98cf932009-12-27 22:54:55 +01007143 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02007144 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007145 if (unlikely(compressing)) {
7146 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02007147 compressing = 0;
7148 }
Willy Tarreauf003d372012-11-26 13:35:37 +01007149
Willy Tarreauc623c172014-04-18 09:53:50 +02007150 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
7151 b_adv(res->buf, msg->next);
7152 msg->next = 0;
7153 msg->chunk_len -= channel_forward(res, msg->chunk_len);
7154 }
7155
Willy Tarreauf003d372012-11-26 13:35:37 +01007156 if (res->flags & CF_SHUTW)
7157 goto aborted_xfer;
7158
7159 /* stop waiting for data if the input is closed before the end. If the
7160 * client side was already closed, it means that the client has aborted,
7161 * so we don't want to count this as a server abort. Otherwise it's a
7162 * server abort.
7163 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007164 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007165 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01007166 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02007167 /* If we have some pending data, we continue the processing */
7168 if (!buffer_pending(res->buf)) {
7169 if (!(s->flags & SF_ERR_MASK))
7170 s->flags |= SF_ERR_SRVCL;
7171 s->be->be_counters.srv_aborts++;
7172 if (objt_server(s->target))
7173 objt_server(s->target)->counters.srv_aborts++;
7174 goto return_bad_res_stats_ok;
7175 }
Willy Tarreau40dba092010-03-04 18:14:51 +01007176 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01007177
Willy Tarreau40dba092010-03-04 18:14:51 +01007178 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007179 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01007180 goto return_bad_res;
7181
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007182 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007183 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007184 * Similarly, with keep-alive on the client side, we don't want to forward a
7185 * close.
7186 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007187 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007188 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7189 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007190 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007191
Willy Tarreau5c620922011-05-11 19:56:11 +02007192 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007193 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007194 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007195 * modes are already handled by the stream sock layer. We must not do
7196 * this in content-length mode because it could present the MSG_MORE
7197 * flag with the last block of forwarded data, which would cause an
7198 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007199 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007200 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007201 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007202
Willy Tarreau87b09662015-04-03 00:22:06 +02007203 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007204 return 0;
7205
Willy Tarreau40dba092010-03-04 18:14:51 +01007206 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007207 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007208 if (objt_server(s->target))
7209 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007210
7211 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02007212 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02007213 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02007214 compressing = 0;
7215 }
7216
Willy Tarreauc623c172014-04-18 09:53:50 +02007217 /* we may have some pending data starting at res->buf->p */
7218 if (s->comp_algo == NULL) {
7219 b_adv(res->buf, msg->next);
7220 msg->next = 0;
7221 }
7222
Willy Tarreaud98cf932009-12-27 22:54:55 +01007223 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007224 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007225 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007226 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007227 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007228 if (objt_server(s->target))
7229 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007230
Willy Tarreaue7dff022015-04-03 01:14:29 +02007231 if (!(s->flags & SF_ERR_MASK))
7232 s->flags |= SF_ERR_PRXCOND;
7233 if (!(s->flags & SF_FINST_MASK))
7234 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007235 return 0;
7236
7237 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02007238 if (unlikely(compressing)) {
7239 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
7240 compressing = 0;
7241 }
7242
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007243 txn->rsp.msg_state = HTTP_MSG_ERROR;
7244 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007245 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007246 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007247 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007248
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007249 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007250 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007251 if (objt_server(s->target))
7252 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007253
Willy Tarreaue7dff022015-04-03 01:14:29 +02007254 if (!(s->flags & SF_ERR_MASK))
7255 s->flags |= SF_ERR_CLICL;
7256 if (!(s->flags & SF_FINST_MASK))
7257 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007258 return 0;
7259}
7260
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007261/* Iterate the same filter through all request headers.
7262 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007263 * Since it can manage the switch to another backend, it updates the per-proxy
7264 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007265 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007266int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007267{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007268 char *cur_ptr, *cur_end, *cur_next;
7269 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007270 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007271 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007272 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007273
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007274 last_hdr = 0;
7275
Willy Tarreau9b28e032012-10-12 23:49:43 +02007276 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007277 old_idx = 0;
7278
7279 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007280 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007281 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007282 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007283 (exp->action == ACT_ALLOW ||
7284 exp->action == ACT_DENY ||
7285 exp->action == ACT_TARPIT))
7286 return 0;
7287
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007288 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007289 if (!cur_idx)
7290 break;
7291
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007292 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007293 cur_ptr = cur_next;
7294 cur_end = cur_ptr + cur_hdr->len;
7295 cur_next = cur_end + cur_hdr->cr + 1;
7296
7297 /* Now we have one header between cur_ptr and cur_end,
7298 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007299 */
7300
Willy Tarreau15a53a42015-01-21 13:39:42 +01007301 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007302 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007303 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007304 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007305 last_hdr = 1;
7306 break;
7307
7308 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007309 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007310 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007311 break;
7312
7313 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007314 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007315 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007316 break;
7317
7318 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007319 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7320 if (trash.len < 0)
7321 return -1;
7322
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007323 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007324 /* FIXME: if the user adds a newline in the replacement, the
7325 * index will not be recalculated for now, and the new line
7326 * will not be counted as a new header.
7327 */
7328
7329 cur_end += delta;
7330 cur_next += delta;
7331 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007332 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007333 break;
7334
7335 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007336 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007337 cur_next += delta;
7338
Willy Tarreaufa355d42009-11-29 18:12:29 +01007339 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007340 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7341 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007342 cur_hdr->len = 0;
7343 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007344 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007345 break;
7346
7347 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007348 }
7349
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007350 /* keep the link from this header to next one in case of later
7351 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007352 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007353 old_idx = cur_idx;
7354 }
7355 return 0;
7356}
7357
7358
7359/* Apply the filter to the request line.
7360 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7361 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007362 * Since it can manage the switch to another backend, it updates the per-proxy
7363 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007364 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007365int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007366{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007367 char *cur_ptr, *cur_end;
7368 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007369 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007370 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007371
Willy Tarreau3d300592007-03-18 18:34:41 +01007372 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007373 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007374 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007375 (exp->action == ACT_ALLOW ||
7376 exp->action == ACT_DENY ||
7377 exp->action == ACT_TARPIT))
7378 return 0;
7379 else if (exp->action == ACT_REMOVE)
7380 return 0;
7381
7382 done = 0;
7383
Willy Tarreau9b28e032012-10-12 23:49:43 +02007384 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007385 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007386
7387 /* Now we have the request line between cur_ptr and cur_end */
7388
Willy Tarreau15a53a42015-01-21 13:39:42 +01007389 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007390 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007391 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007392 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007393 done = 1;
7394 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007395
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007396 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007397 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007398 done = 1;
7399 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007400
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007401 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007402 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007403 done = 1;
7404 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007405
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007406 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007407 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7408 if (trash.len < 0)
7409 return -1;
7410
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007411 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007412 /* FIXME: if the user adds a newline in the replacement, the
7413 * index will not be recalculated for now, and the new line
7414 * will not be counted as a new header.
7415 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007416
Willy Tarreaufa355d42009-11-29 18:12:29 +01007417 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007418 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007419 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007420 HTTP_MSG_RQMETH,
7421 cur_ptr, cur_end + 1,
7422 NULL, NULL);
7423 if (unlikely(!cur_end))
7424 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007425
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007426 /* we have a full request and we know that we have either a CR
7427 * or an LF at <ptr>.
7428 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007429 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7430 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007431 /* there is no point trying this regex on headers */
7432 return 1;
7433 }
7434 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007435 return done;
7436}
Willy Tarreau97de6242006-12-27 17:18:38 +01007437
Willy Tarreau58f10d72006-12-04 02:26:12 +01007438
Willy Tarreau58f10d72006-12-04 02:26:12 +01007439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007440/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007441 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007442 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007443 * unparsable request. Since it can manage the switch to another backend, it
7444 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007445 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007446int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007447{
Willy Tarreau192252e2015-04-04 01:47:55 +02007448 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007449 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007450 struct hdr_exp *exp;
7451
7452 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007453 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007454
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007455 /*
7456 * The interleaving of transformations and verdicts
7457 * makes it difficult to decide to continue or stop
7458 * the evaluation.
7459 */
7460
Willy Tarreau6c123b12010-01-28 20:22:06 +01007461 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7462 break;
7463
Willy Tarreau3d300592007-03-18 18:34:41 +01007464 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007465 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007466 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007467 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007468
7469 /* if this filter had a condition, evaluate it now and skip to
7470 * next filter if the condition does not match.
7471 */
7472 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007473 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007474 ret = acl_pass(ret);
7475 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7476 ret = !ret;
7477
7478 if (!ret)
7479 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007480 }
7481
7482 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007483 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007484 if (unlikely(ret < 0))
7485 return -1;
7486
7487 if (likely(ret == 0)) {
7488 /* The filter did not match the request, it can be
7489 * iterated through all headers.
7490 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007491 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7492 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007493 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007494 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007495 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007496}
7497
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007498
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007499/* Find the end of a cookie value contained between <s> and <e>. It works the
7500 * same way as with headers above except that the semi-colon also ends a token.
7501 * See RFC2965 for more information. Note that it requires a valid header to
7502 * return a valid result.
7503 */
7504char *find_cookie_value_end(char *s, const char *e)
7505{
7506 int quoted, qdpair;
7507
7508 quoted = qdpair = 0;
7509 for (; s < e; s++) {
7510 if (qdpair) qdpair = 0;
7511 else if (quoted) {
7512 if (*s == '\\') qdpair = 1;
7513 else if (*s == '"') quoted = 0;
7514 }
7515 else if (*s == '"') quoted = 1;
7516 else if (*s == ',' || *s == ';') return s;
7517 }
7518 return s;
7519}
7520
7521/* Delete a value in a header between delimiters <from> and <next> in buffer
7522 * <buf>. The number of characters displaced is returned, and the pointer to
7523 * the first delimiter is updated if required. The function tries as much as
7524 * possible to respect the following principles :
7525 * - replace <from> delimiter by the <next> one unless <from> points to a
7526 * colon, in which case <next> is simply removed
7527 * - set exactly one space character after the new first delimiter, unless
7528 * there are not enough characters in the block being moved to do so.
7529 * - remove unneeded spaces before the previous delimiter and after the new
7530 * one.
7531 *
7532 * It is the caller's responsibility to ensure that :
7533 * - <from> points to a valid delimiter or the colon ;
7534 * - <next> points to a valid delimiter or the final CR/LF ;
7535 * - there are non-space chars before <from> ;
7536 * - there is a CR/LF at or after <next>.
7537 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007538int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007539{
7540 char *prev = *from;
7541
7542 if (*prev == ':') {
7543 /* We're removing the first value, preserve the colon and add a
7544 * space if possible.
7545 */
7546 if (!http_is_crlf[(unsigned char)*next])
7547 next++;
7548 prev++;
7549 if (prev < next)
7550 *prev++ = ' ';
7551
7552 while (http_is_spht[(unsigned char)*next])
7553 next++;
7554 } else {
7555 /* Remove useless spaces before the old delimiter. */
7556 while (http_is_spht[(unsigned char)*(prev-1)])
7557 prev--;
7558 *from = prev;
7559
7560 /* copy the delimiter and if possible a space if we're
7561 * not at the end of the line.
7562 */
7563 if (!http_is_crlf[(unsigned char)*next]) {
7564 *prev++ = *next++;
7565 if (prev + 1 < next)
7566 *prev++ = ' ';
7567 while (http_is_spht[(unsigned char)*next])
7568 next++;
7569 }
7570 }
7571 return buffer_replace2(buf, prev, next, NULL, 0);
7572}
7573
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007574/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007575 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007576 * desirable to call it only when needed. This code is quite complex because
7577 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7578 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007579 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007580void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007581{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007582 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007583 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007584 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007585 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007586 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7587 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007588
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007589 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007590 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007591 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007592
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007593 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007594 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007595 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007596
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007597 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007598 hdr_beg = hdr_next;
7599 hdr_end = hdr_beg + cur_hdr->len;
7600 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007601
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007602 /* We have one full header between hdr_beg and hdr_end, and the
7603 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007604 * "Cookie:" headers.
7605 */
7606
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007607 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007608 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007609 old_idx = cur_idx;
7610 continue;
7611 }
7612
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007613 del_from = NULL; /* nothing to be deleted */
7614 preserve_hdr = 0; /* assume we may kill the whole header */
7615
Willy Tarreau58f10d72006-12-04 02:26:12 +01007616 /* Now look for cookies. Conforming to RFC2109, we have to support
7617 * attributes whose name begin with a '$', and associate them with
7618 * the right cookie, if we want to delete this cookie.
7619 * So there are 3 cases for each cookie read :
7620 * 1) it's a special attribute, beginning with a '$' : ignore it.
7621 * 2) it's a server id cookie that we *MAY* want to delete : save
7622 * some pointers on it (last semi-colon, beginning of cookie...)
7623 * 3) it's an application cookie : we *MAY* have to delete a previous
7624 * "special" cookie.
7625 * At the end of loop, if a "special" cookie remains, we may have to
7626 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007627 * *MUST* delete it.
7628 *
7629 * Note: RFC2965 is unclear about the processing of spaces around
7630 * the equal sign in the ATTR=VALUE form. A careful inspection of
7631 * the RFC explicitly allows spaces before it, and not within the
7632 * tokens (attrs or values). An inspection of RFC2109 allows that
7633 * too but section 10.1.3 lets one think that spaces may be allowed
7634 * after the equal sign too, resulting in some (rare) buggy
7635 * implementations trying to do that. So let's do what servers do.
7636 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7637 * allowed quoted strings in values, with any possible character
7638 * after a backslash, including control chars and delimitors, which
7639 * causes parsing to become ambiguous. Browsers also allow spaces
7640 * within values even without quotes.
7641 *
7642 * We have to keep multiple pointers in order to support cookie
7643 * removal at the beginning, middle or end of header without
7644 * corrupting the header. All of these headers are valid :
7645 *
7646 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7647 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7648 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7649 * | | | | | | | | |
7650 * | | | | | | | | hdr_end <--+
7651 * | | | | | | | +--> next
7652 * | | | | | | +----> val_end
7653 * | | | | | +-----------> val_beg
7654 * | | | | +--------------> equal
7655 * | | | +----------------> att_end
7656 * | | +---------------------> att_beg
7657 * | +--------------------------> prev
7658 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007659 */
7660
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007661 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7662 /* Iterate through all cookies on this line */
7663
7664 /* find att_beg */
7665 att_beg = prev + 1;
7666 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7667 att_beg++;
7668
7669 /* find att_end : this is the first character after the last non
7670 * space before the equal. It may be equal to hdr_end.
7671 */
7672 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007673
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007674 while (equal < hdr_end) {
7675 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007676 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007677 if (http_is_spht[(unsigned char)*equal++])
7678 continue;
7679 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007680 }
7681
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007682 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7683 * is between <att_beg> and <equal>, both may be identical.
7684 */
7685
7686 /* look for end of cookie if there is an equal sign */
7687 if (equal < hdr_end && *equal == '=') {
7688 /* look for the beginning of the value */
7689 val_beg = equal + 1;
7690 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7691 val_beg++;
7692
7693 /* find the end of the value, respecting quotes */
7694 next = find_cookie_value_end(val_beg, hdr_end);
7695
7696 /* make val_end point to the first white space or delimitor after the value */
7697 val_end = next;
7698 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7699 val_end--;
7700 } else {
7701 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007702 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007703
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007704 /* We have nothing to do with attributes beginning with '$'. However,
7705 * they will automatically be removed if a header before them is removed,
7706 * since they're supposed to be linked together.
7707 */
7708 if (*att_beg == '$')
7709 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007710
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007711 /* Ignore cookies with no equal sign */
7712 if (equal == next) {
7713 /* This is not our cookie, so we must preserve it. But if we already
7714 * scheduled another cookie for removal, we cannot remove the
7715 * complete header, but we can remove the previous block itself.
7716 */
7717 preserve_hdr = 1;
7718 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007719 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007720 val_end += delta;
7721 next += delta;
7722 hdr_end += delta;
7723 hdr_next += delta;
7724 cur_hdr->len += delta;
7725 http_msg_move_end(&txn->req, delta);
7726 prev = del_from;
7727 del_from = NULL;
7728 }
7729 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007730 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007731
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007732 /* if there are spaces around the equal sign, we need to
7733 * strip them otherwise we'll get trouble for cookie captures,
7734 * or even for rewrites. Since this happens extremely rarely,
7735 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007736 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007737 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7738 int stripped_before = 0;
7739 int stripped_after = 0;
7740
7741 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007742 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007743 equal += stripped_before;
7744 val_beg += stripped_before;
7745 }
7746
7747 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007748 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007749 val_beg += stripped_after;
7750 stripped_before += stripped_after;
7751 }
7752
7753 val_end += stripped_before;
7754 next += stripped_before;
7755 hdr_end += stripped_before;
7756 hdr_next += stripped_before;
7757 cur_hdr->len += stripped_before;
7758 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007759 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007760 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007761
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007762 /* First, let's see if we want to capture this cookie. We check
7763 * that we don't already have a client side cookie, because we
7764 * can only capture one. Also as an optimisation, we ignore
7765 * cookies shorter than the declared name.
7766 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007767 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7768 (val_end - att_beg >= sess->fe->capture_namelen) &&
7769 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007770 int log_len = val_end - att_beg;
7771
7772 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7773 Alert("HTTP logging : out of memory.\n");
7774 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007775 if (log_len > sess->fe->capture_len)
7776 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007777 memcpy(txn->cli_cookie, att_beg, log_len);
7778 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007779 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007780 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007781
Willy Tarreaubca99692010-10-06 19:25:55 +02007782 /* Persistence cookies in passive, rewrite or insert mode have the
7783 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007784 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007785 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007786 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007787 * For cookies in prefix mode, the form is :
7788 *
7789 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007790 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007791 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7792 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7793 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007794 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007795
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007796 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7797 * have the server ID between val_beg and delim, and the original cookie between
7798 * delim+1 and val_end. Otherwise, delim==val_end :
7799 *
7800 * Cookie: NAME=SRV; # in all but prefix modes
7801 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7802 * | || || | |+-> next
7803 * | || || | +--> val_end
7804 * | || || +---------> delim
7805 * | || |+------------> val_beg
7806 * | || +-------------> att_end = equal
7807 * | |+-----------------> att_beg
7808 * | +------------------> prev
7809 * +-------------------------> hdr_beg
7810 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007811
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007812 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007813 for (delim = val_beg; delim < val_end; delim++)
7814 if (*delim == COOKIE_DELIM)
7815 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007816 } else {
7817 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007818 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007819 /* Now check if the cookie contains a date field, which would
7820 * appear after a vertical bar ('|') just after the server name
7821 * and before the delimiter.
7822 */
7823 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7824 if (vbar1) {
7825 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007826 * right is the last seen date. It is a base64 encoded
7827 * 30-bit value representing the UNIX date since the
7828 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007829 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007830 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007831 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007832 if (val_end - vbar1 >= 5) {
7833 val = b64tos30(vbar1);
7834 if (val > 0)
7835 txn->cookie_last_date = val << 2;
7836 }
7837 /* look for a second vertical bar */
7838 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7839 if (vbar1 && (val_end - vbar1 > 5)) {
7840 val = b64tos30(vbar1 + 1);
7841 if (val > 0)
7842 txn->cookie_first_date = val << 2;
7843 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007844 }
7845 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007846
Willy Tarreauf64d1412010-10-07 20:06:11 +02007847 /* if the cookie has an expiration date and the proxy wants to check
7848 * it, then we do that now. We first check if the cookie is too old,
7849 * then only if it has expired. We detect strict overflow because the
7850 * time resolution here is not great (4 seconds). Cookies with dates
7851 * in the future are ignored if their offset is beyond one day. This
7852 * allows an admin to fix timezone issues without expiring everyone
7853 * and at the same time avoids keeping unwanted side effects for too
7854 * long.
7855 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007856 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7857 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007858 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007859 txn->flags &= ~TX_CK_MASK;
7860 txn->flags |= TX_CK_OLD;
7861 delim = val_beg; // let's pretend we have not found the cookie
7862 txn->cookie_first_date = 0;
7863 txn->cookie_last_date = 0;
7864 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007865 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7866 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007867 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007868 txn->flags &= ~TX_CK_MASK;
7869 txn->flags |= TX_CK_EXPIRED;
7870 delim = val_beg; // let's pretend we have not found the cookie
7871 txn->cookie_first_date = 0;
7872 txn->cookie_last_date = 0;
7873 }
7874
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007875 /* Here, we'll look for the first running server which supports the cookie.
7876 * This allows to share a same cookie between several servers, for example
7877 * to dedicate backup servers to specific servers only.
7878 * However, to prevent clients from sticking to cookie-less backup server
7879 * when they have incidentely learned an empty cookie, we simply ignore
7880 * empty cookies and mark them as invalid.
7881 * The same behaviour is applied when persistence must be ignored.
7882 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007883 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007884 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007885
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007886 while (srv) {
7887 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7888 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007889 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007890 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007891 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007892 /* we found the server and we can use it */
7893 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007894 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007895 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007896 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007897 break;
7898 } else {
7899 /* we found a server, but it's down,
7900 * mark it as such and go on in case
7901 * another one is available.
7902 */
7903 txn->flags &= ~TX_CK_MASK;
7904 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007905 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007906 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007907 srv = srv->next;
7908 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007909
Willy Tarreauf64d1412010-10-07 20:06:11 +02007910 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007911 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007912 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007913 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007914 txn->flags |= TX_CK_UNUSED;
7915 else
7916 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007917 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007918
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007919 /* depending on the cookie mode, we may have to either :
7920 * - delete the complete cookie if we're in insert+indirect mode, so that
7921 * the server never sees it ;
7922 * - remove the server id from the cookie value, and tag the cookie as an
7923 * application cookie so that it does not get accidentely removed later,
7924 * if we're in cookie prefix mode
7925 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007926 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007927 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007928
Willy Tarreau9b28e032012-10-12 23:49:43 +02007929 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007930 val_end += delta;
7931 next += delta;
7932 hdr_end += delta;
7933 hdr_next += delta;
7934 cur_hdr->len += delta;
7935 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007936
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007937 del_from = NULL;
7938 preserve_hdr = 1; /* we want to keep this cookie */
7939 }
7940 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007941 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007942 del_from = prev;
7943 }
7944 } else {
7945 /* This is not our cookie, so we must preserve it. But if we already
7946 * scheduled another cookie for removal, we cannot remove the
7947 * complete header, but we can remove the previous block itself.
7948 */
7949 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007950
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007951 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007952 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007953 if (att_beg >= del_from)
7954 att_beg += delta;
7955 if (att_end >= del_from)
7956 att_end += delta;
7957 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007958 val_end += delta;
7959 next += delta;
7960 hdr_end += delta;
7961 hdr_next += delta;
7962 cur_hdr->len += delta;
7963 http_msg_move_end(&txn->req, delta);
7964 prev = del_from;
7965 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007966 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007967 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007968
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007969 /* continue with next cookie on this header line */
7970 att_beg = next;
7971 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007972
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007973 /* There are no more cookies on this line.
7974 * We may still have one (or several) marked for deletion at the
7975 * end of the line. We must do this now in two ways :
7976 * - if some cookies must be preserved, we only delete from the
7977 * mark to the end of line ;
7978 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007979 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007980 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007981 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007982 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007983 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007984 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007985 cur_hdr->len += delta;
7986 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007987 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007988
7989 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007990 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7991 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007992 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007993 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007994 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007995 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007996 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007997 }
7998
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007999 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01008000 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02008001 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008002}
8003
8004
Willy Tarreaua15645d2007-03-18 16:22:39 +01008005/* Iterate the same filter through all response headers contained in <rtr>.
8006 * Returns 1 if this filter can be stopped upon return, otherwise 0.
8007 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008008int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008009{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008010 char *cur_ptr, *cur_end, *cur_next;
8011 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008012 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008013 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008014 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008015
8016 last_hdr = 0;
8017
Willy Tarreau9b28e032012-10-12 23:49:43 +02008018 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008019 old_idx = 0;
8020
8021 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008022 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008023 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008024 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008025 (exp->action == ACT_ALLOW ||
8026 exp->action == ACT_DENY))
8027 return 0;
8028
8029 cur_idx = txn->hdr_idx.v[old_idx].next;
8030 if (!cur_idx)
8031 break;
8032
8033 cur_hdr = &txn->hdr_idx.v[cur_idx];
8034 cur_ptr = cur_next;
8035 cur_end = cur_ptr + cur_hdr->len;
8036 cur_next = cur_end + cur_hdr->cr + 1;
8037
8038 /* Now we have one header between cur_ptr and cur_end,
8039 * and the next header starts at cur_next.
8040 */
8041
Willy Tarreau15a53a42015-01-21 13:39:42 +01008042 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008043 switch (exp->action) {
8044 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008045 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008046 last_hdr = 1;
8047 break;
8048
8049 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008050 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008051 last_hdr = 1;
8052 break;
8053
8054 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008055 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8056 if (trash.len < 0)
8057 return -1;
8058
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008059 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008060 /* FIXME: if the user adds a newline in the replacement, the
8061 * index will not be recalculated for now, and the new line
8062 * will not be counted as a new header.
8063 */
8064
8065 cur_end += delta;
8066 cur_next += delta;
8067 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008068 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008069 break;
8070
8071 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008072 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008073 cur_next += delta;
8074
Willy Tarreaufa355d42009-11-29 18:12:29 +01008075 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008076 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8077 txn->hdr_idx.used--;
8078 cur_hdr->len = 0;
8079 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008080 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008081 break;
8082
8083 }
8084 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008085
8086 /* keep the link from this header to next one in case of later
8087 * removal of next header.
8088 */
8089 old_idx = cur_idx;
8090 }
8091 return 0;
8092}
8093
8094
8095/* Apply the filter to the status line in the response buffer <rtr>.
8096 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8097 * or -1 if a replacement resulted in an invalid status line.
8098 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008099int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008100{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008101 char *cur_ptr, *cur_end;
8102 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008103 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008104 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008105
8106
Willy Tarreau3d300592007-03-18 18:34:41 +01008107 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008108 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008109 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008110 (exp->action == ACT_ALLOW ||
8111 exp->action == ACT_DENY))
8112 return 0;
8113 else if (exp->action == ACT_REMOVE)
8114 return 0;
8115
8116 done = 0;
8117
Willy Tarreau9b28e032012-10-12 23:49:43 +02008118 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008119 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008120
8121 /* Now we have the status line between cur_ptr and cur_end */
8122
Willy Tarreau15a53a42015-01-21 13:39:42 +01008123 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008124 switch (exp->action) {
8125 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008126 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008127 done = 1;
8128 break;
8129
8130 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008131 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008132 done = 1;
8133 break;
8134
8135 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008136 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8137 if (trash.len < 0)
8138 return -1;
8139
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008140 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008141 /* FIXME: if the user adds a newline in the replacement, the
8142 * index will not be recalculated for now, and the new line
8143 * will not be counted as a new header.
8144 */
8145
Willy Tarreaufa355d42009-11-29 18:12:29 +01008146 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008147 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008148 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008149 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008150 cur_ptr, cur_end + 1,
8151 NULL, NULL);
8152 if (unlikely(!cur_end))
8153 return -1;
8154
8155 /* we have a full respnse and we know that we have either a CR
8156 * or an LF at <ptr>.
8157 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008158 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008159 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008160 /* there is no point trying this regex on headers */
8161 return 1;
8162 }
8163 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008164 return done;
8165}
8166
8167
8168
8169/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008170 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008171 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8172 * unparsable response.
8173 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008174int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008175{
Willy Tarreau192252e2015-04-04 01:47:55 +02008176 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008177 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008178 struct hdr_exp *exp;
8179
8180 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008181 int ret;
8182
8183 /*
8184 * The interleaving of transformations and verdicts
8185 * makes it difficult to decide to continue or stop
8186 * the evaluation.
8187 */
8188
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008189 if (txn->flags & TX_SVDENY)
8190 break;
8191
Willy Tarreau3d300592007-03-18 18:34:41 +01008192 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008193 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8194 exp->action == ACT_PASS)) {
8195 exp = exp->next;
8196 continue;
8197 }
8198
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008199 /* if this filter had a condition, evaluate it now and skip to
8200 * next filter if the condition does not match.
8201 */
8202 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008203 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008204 ret = acl_pass(ret);
8205 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8206 ret = !ret;
8207 if (!ret)
8208 continue;
8209 }
8210
Willy Tarreaua15645d2007-03-18 16:22:39 +01008211 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008212 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008213 if (unlikely(ret < 0))
8214 return -1;
8215
8216 if (likely(ret == 0)) {
8217 /* The filter did not match the response, it can be
8218 * iterated through all headers.
8219 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008220 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8221 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008222 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008223 }
8224 return 0;
8225}
8226
8227
Willy Tarreaua15645d2007-03-18 16:22:39 +01008228/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008229 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008230 * desirable to call it only when needed. This function is also used when we
8231 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008232 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008233void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008234{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008235 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008236 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008237 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008238 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008239 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008240 char *hdr_beg, *hdr_end, *hdr_next;
8241 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008242
Willy Tarreaua15645d2007-03-18 16:22:39 +01008243 /* Iterate through the headers.
8244 * we start with the start line.
8245 */
8246 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008247 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008248
8249 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8250 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008251 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008252
8253 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008254 hdr_beg = hdr_next;
8255 hdr_end = hdr_beg + cur_hdr->len;
8256 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008257
Willy Tarreau24581ba2010-08-31 22:39:35 +02008258 /* We have one full header between hdr_beg and hdr_end, and the
8259 * next header starts at hdr_next. We're only interested in
8260 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008261 */
8262
Willy Tarreau24581ba2010-08-31 22:39:35 +02008263 is_cookie2 = 0;
8264 prev = hdr_beg + 10;
8265 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008266 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008267 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8268 if (!val) {
8269 old_idx = cur_idx;
8270 continue;
8271 }
8272 is_cookie2 = 1;
8273 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008274 }
8275
Willy Tarreau24581ba2010-08-31 22:39:35 +02008276 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8277 * <prev> points to the colon.
8278 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008279 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008280
Willy Tarreau24581ba2010-08-31 22:39:35 +02008281 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8282 * check-cache is enabled) and we are not interested in checking
8283 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008284 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008285 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008286 return;
8287
Willy Tarreau24581ba2010-08-31 22:39:35 +02008288 /* OK so now we know we have to process this response cookie.
8289 * The format of the Set-Cookie header is slightly different
8290 * from the format of the Cookie header in that it does not
8291 * support the comma as a cookie delimiter (thus the header
8292 * cannot be folded) because the Expires attribute described in
8293 * the original Netscape's spec may contain an unquoted date
8294 * with a comma inside. We have to live with this because
8295 * many browsers don't support Max-Age and some browsers don't
8296 * support quoted strings. However the Set-Cookie2 header is
8297 * clean.
8298 *
8299 * We have to keep multiple pointers in order to support cookie
8300 * removal at the beginning, middle or end of header without
8301 * corrupting the header (in case of set-cookie2). A special
8302 * pointer, <scav> points to the beginning of the set-cookie-av
8303 * fields after the first semi-colon. The <next> pointer points
8304 * either to the end of line (set-cookie) or next unquoted comma
8305 * (set-cookie2). All of these headers are valid :
8306 *
8307 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8308 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8309 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8310 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8311 * | | | | | | | | | |
8312 * | | | | | | | | +-> next hdr_end <--+
8313 * | | | | | | | +------------> scav
8314 * | | | | | | +--------------> val_end
8315 * | | | | | +--------------------> val_beg
8316 * | | | | +----------------------> equal
8317 * | | | +------------------------> att_end
8318 * | | +----------------------------> att_beg
8319 * | +------------------------------> prev
8320 * +-----------------------------------------> hdr_beg
8321 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008322
Willy Tarreau24581ba2010-08-31 22:39:35 +02008323 for (; prev < hdr_end; prev = next) {
8324 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008325
Willy Tarreau24581ba2010-08-31 22:39:35 +02008326 /* find att_beg */
8327 att_beg = prev + 1;
8328 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8329 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008330
Willy Tarreau24581ba2010-08-31 22:39:35 +02008331 /* find att_end : this is the first character after the last non
8332 * space before the equal. It may be equal to hdr_end.
8333 */
8334 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008335
Willy Tarreau24581ba2010-08-31 22:39:35 +02008336 while (equal < hdr_end) {
8337 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8338 break;
8339 if (http_is_spht[(unsigned char)*equal++])
8340 continue;
8341 att_end = equal;
8342 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008343
Willy Tarreau24581ba2010-08-31 22:39:35 +02008344 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8345 * is between <att_beg> and <equal>, both may be identical.
8346 */
8347
8348 /* look for end of cookie if there is an equal sign */
8349 if (equal < hdr_end && *equal == '=') {
8350 /* look for the beginning of the value */
8351 val_beg = equal + 1;
8352 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8353 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008354
Willy Tarreau24581ba2010-08-31 22:39:35 +02008355 /* find the end of the value, respecting quotes */
8356 next = find_cookie_value_end(val_beg, hdr_end);
8357
8358 /* make val_end point to the first white space or delimitor after the value */
8359 val_end = next;
8360 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8361 val_end--;
8362 } else {
8363 /* <equal> points to next comma, semi-colon or EOL */
8364 val_beg = val_end = next = equal;
8365 }
8366
8367 if (next < hdr_end) {
8368 /* Set-Cookie2 supports multiple cookies, and <next> points to
8369 * a colon or semi-colon before the end. So skip all attr-value
8370 * pairs and look for the next comma. For Set-Cookie, since
8371 * commas are permitted in values, skip to the end.
8372 */
8373 if (is_cookie2)
8374 next = find_hdr_value_end(next, hdr_end);
8375 else
8376 next = hdr_end;
8377 }
8378
8379 /* Now everything is as on the diagram above */
8380
8381 /* Ignore cookies with no equal sign */
8382 if (equal == val_end)
8383 continue;
8384
8385 /* If there are spaces around the equal sign, we need to
8386 * strip them otherwise we'll get trouble for cookie captures,
8387 * or even for rewrites. Since this happens extremely rarely,
8388 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008389 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008390 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8391 int stripped_before = 0;
8392 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008393
Willy Tarreau24581ba2010-08-31 22:39:35 +02008394 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008395 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008396 equal += stripped_before;
8397 val_beg += stripped_before;
8398 }
8399
8400 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008401 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008402 val_beg += stripped_after;
8403 stripped_before += stripped_after;
8404 }
8405
8406 val_end += stripped_before;
8407 next += stripped_before;
8408 hdr_end += stripped_before;
8409 hdr_next += stripped_before;
8410 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008411 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008412 }
8413
8414 /* First, let's see if we want to capture this cookie. We check
8415 * that we don't already have a server side cookie, because we
8416 * can only capture one. Also as an optimisation, we ignore
8417 * cookies shorter than the declared name.
8418 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008419 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008420 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008421 (val_end - att_beg >= sess->fe->capture_namelen) &&
8422 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008423 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008424 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008425 Alert("HTTP logging : out of memory.\n");
8426 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008427 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008428 if (log_len > sess->fe->capture_len)
8429 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008430 memcpy(txn->srv_cookie, att_beg, log_len);
8431 txn->srv_cookie[log_len] = 0;
8432 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008433 }
8434
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008435 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008436 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008437 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008438 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8439 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008440 /* assume passive cookie by default */
8441 txn->flags &= ~TX_SCK_MASK;
8442 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008443
8444 /* If the cookie is in insert mode on a known server, we'll delete
8445 * this occurrence because we'll insert another one later.
8446 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008447 * a direct access.
8448 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008449 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008450 /* The "preserve" flag was set, we don't want to touch the
8451 * server's cookie.
8452 */
8453 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008454 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008455 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008456 /* this cookie must be deleted */
8457 if (*prev == ':' && next == hdr_end) {
8458 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008459 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008460 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8461 txn->hdr_idx.used--;
8462 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008463 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008464 hdr_next += delta;
8465 http_msg_move_end(&txn->rsp, delta);
8466 /* note: while both invalid now, <next> and <hdr_end>
8467 * are still equal, so the for() will stop as expected.
8468 */
8469 } else {
8470 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008471 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008472 next = prev;
8473 hdr_end += delta;
8474 hdr_next += delta;
8475 cur_hdr->len += delta;
8476 http_msg_move_end(&txn->rsp, delta);
8477 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008478 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008479 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008480 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008481 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008482 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008483 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008484 * with this server since we know it.
8485 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008486 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008487 next += delta;
8488 hdr_end += delta;
8489 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008490 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008491 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008492
Willy Tarreauf1348312010-10-07 15:54:11 +02008493 txn->flags &= ~TX_SCK_MASK;
8494 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008495 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008496 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008497 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008498 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008499 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008500 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008501 next += delta;
8502 hdr_end += delta;
8503 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008504 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008505 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008506
Willy Tarreau827aee92011-03-10 16:55:02 +01008507 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008508 txn->flags &= ~TX_SCK_MASK;
8509 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008510 }
8511 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008512 /* that's done for this cookie, check the next one on the same
8513 * line when next != hdr_end (only if is_cookie2).
8514 */
8515 }
8516 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008517 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008518 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008519}
8520
8521
Willy Tarreaua15645d2007-03-18 16:22:39 +01008522/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008523 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008524 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008525void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008526{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008527 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008528 char *p1, *p2;
8529
8530 char *cur_ptr, *cur_end, *cur_next;
8531 int cur_idx;
8532
Willy Tarreau5df51872007-11-25 16:20:08 +01008533 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008534 return;
8535
8536 /* Iterate through the headers.
8537 * we start with the start line.
8538 */
8539 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008540 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008541
8542 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8543 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008544 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008545
8546 cur_hdr = &txn->hdr_idx.v[cur_idx];
8547 cur_ptr = cur_next;
8548 cur_end = cur_ptr + cur_hdr->len;
8549 cur_next = cur_end + cur_hdr->cr + 1;
8550
8551 /* We have one full header between cur_ptr and cur_end, and the
8552 * next header starts at cur_next. We're only interested in
8553 * "Cookie:" headers.
8554 */
8555
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008556 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8557 if (val) {
8558 if ((cur_end - (cur_ptr + val) >= 8) &&
8559 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8560 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8561 return;
8562 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008563 }
8564
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008565 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8566 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008567 continue;
8568
8569 /* OK, right now we know we have a cache-control header at cur_ptr */
8570
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008571 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008572
8573 if (p1 >= cur_end) /* no more info */
8574 continue;
8575
8576 /* p1 is at the beginning of the value */
8577 p2 = p1;
8578
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008579 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008580 p2++;
8581
8582 /* we have a complete value between p1 and p2 */
8583 if (p2 < cur_end && *p2 == '=') {
8584 /* we have something of the form no-cache="set-cookie" */
8585 if ((cur_end - p1 >= 21) &&
8586 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8587 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008588 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008589 continue;
8590 }
8591
8592 /* OK, so we know that either p2 points to the end of string or to a comma */
8593 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008594 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008595 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8596 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8597 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008598 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008599 return;
8600 }
8601
8602 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008603 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008604 continue;
8605 }
8606 }
8607}
8608
Willy Tarreau58f10d72006-12-04 02:26:12 +01008609
Willy Tarreaub2513902006-12-17 14:52:38 +01008610/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008611 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008612 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008613 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008614 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008615 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008616 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008617 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008618 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008619int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008620{
8621 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008622 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008623 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008624
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008625 if (!uri_auth)
8626 return 0;
8627
Cyril Bonté70be45d2010-10-12 00:14:35 +02008628 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008629 return 0;
8630
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008631 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008632 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008633 return 0;
8634
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008635 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008636 return 0;
8637
Willy Tarreaub2513902006-12-17 14:52:38 +01008638 return 1;
8639}
8640
Willy Tarreau4076a152009-04-02 15:18:36 +02008641/*
8642 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008643 * By default it tries to report the error position as msg->err_pos. However if
8644 * this one is not set, it will then report msg->next, which is the last known
8645 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008646 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008647 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008648void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008649 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008650 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008651{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008652 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008653 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008654 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008655
Willy Tarreau9b28e032012-10-12 23:49:43 +02008656 es->len = MIN(chn->buf->i, sizeof(es->buf));
8657 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008658 len1 = MIN(len1, es->len);
8659 len2 = es->len - len1; /* remaining data if buffer wraps */
8660
Willy Tarreau9b28e032012-10-12 23:49:43 +02008661 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008662 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008663 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008664
Willy Tarreau4076a152009-04-02 15:18:36 +02008665 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008666 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008667 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008668 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008669
Willy Tarreau4076a152009-04-02 15:18:36 +02008670 es->when = date; // user-visible date
8671 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008672 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008673 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008674 if (objt_conn(sess->origin))
8675 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008676 else
8677 memset(&es->src, 0, sizeof(es->src));
8678
Willy Tarreau078272e2010-12-12 12:46:33 +01008679 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008680 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008681 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008682 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008683 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008684 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008685 es->b_out = chn->buf->o;
8686 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008687 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008688 es->m_clen = msg->chunk_len;
8689 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008690}
Willy Tarreaub2513902006-12-17 14:52:38 +01008691
Willy Tarreau294c4732011-12-16 21:35:50 +01008692/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8693 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8694 * performed over the whole headers. Otherwise it must contain a valid header
8695 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8696 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8697 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8698 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008699 * -1. The value fetch stops at commas, so this function is suited for use with
8700 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008701 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008702 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008703unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008704 struct hdr_idx *idx, int occ,
8705 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008706{
Willy Tarreau294c4732011-12-16 21:35:50 +01008707 struct hdr_ctx local_ctx;
8708 char *ptr_hist[MAX_HDR_HISTORY];
8709 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008710 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008711 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008712
Willy Tarreau294c4732011-12-16 21:35:50 +01008713 if (!ctx) {
8714 local_ctx.idx = 0;
8715 ctx = &local_ctx;
8716 }
8717
Willy Tarreaubce70882009-09-07 11:51:47 +02008718 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008719 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008720 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008721 occ--;
8722 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008723 *vptr = ctx->line + ctx->val;
8724 *vlen = ctx->vlen;
8725 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008726 }
8727 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008728 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008729 }
8730
8731 /* negative occurrence, we scan all the list then walk back */
8732 if (-occ > MAX_HDR_HISTORY)
8733 return 0;
8734
Willy Tarreau294c4732011-12-16 21:35:50 +01008735 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008736 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008737 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8738 len_hist[hist_ptr] = ctx->vlen;
8739 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008740 hist_ptr = 0;
8741 found++;
8742 }
8743 if (-occ > found)
8744 return 0;
8745 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008746 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8747 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8748 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008749 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008750 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008751 if (hist_ptr >= MAX_HDR_HISTORY)
8752 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008753 *vptr = ptr_hist[hist_ptr];
8754 *vlen = len_hist[hist_ptr];
8755 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008756}
8757
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008758/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8759 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8760 * performed over the whole headers. Otherwise it must contain a valid header
8761 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8762 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8763 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8764 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8765 * -1. This function differs from http_get_hdr() in that it only returns full
8766 * line header values and does not stop at commas.
8767 * The return value is 0 if nothing was found, or non-zero otherwise.
8768 */
8769unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8770 struct hdr_idx *idx, int occ,
8771 struct hdr_ctx *ctx, char **vptr, int *vlen)
8772{
8773 struct hdr_ctx local_ctx;
8774 char *ptr_hist[MAX_HDR_HISTORY];
8775 int len_hist[MAX_HDR_HISTORY];
8776 unsigned int hist_ptr;
8777 int found;
8778
8779 if (!ctx) {
8780 local_ctx.idx = 0;
8781 ctx = &local_ctx;
8782 }
8783
8784 if (occ >= 0) {
8785 /* search from the beginning */
8786 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8787 occ--;
8788 if (occ <= 0) {
8789 *vptr = ctx->line + ctx->val;
8790 *vlen = ctx->vlen;
8791 return 1;
8792 }
8793 }
8794 return 0;
8795 }
8796
8797 /* negative occurrence, we scan all the list then walk back */
8798 if (-occ > MAX_HDR_HISTORY)
8799 return 0;
8800
8801 found = hist_ptr = 0;
8802 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8803 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8804 len_hist[hist_ptr] = ctx->vlen;
8805 if (++hist_ptr >= MAX_HDR_HISTORY)
8806 hist_ptr = 0;
8807 found++;
8808 }
8809 if (-occ > found)
8810 return 0;
8811 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8812 * find occurrence -occ, so we have to check [hist_ptr+occ].
8813 */
8814 hist_ptr += occ;
8815 if (hist_ptr >= MAX_HDR_HISTORY)
8816 hist_ptr -= MAX_HDR_HISTORY;
8817 *vptr = ptr_hist[hist_ptr];
8818 *vlen = len_hist[hist_ptr];
8819 return 1;
8820}
8821
Willy Tarreaubaaee002006-06-26 02:48:02 +02008822/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008823 * Print a debug line with a header. Always stop at the first CR or LF char,
8824 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8825 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008826 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008827void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008828{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008829 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008830 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008831
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008832 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008833 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008834 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008835 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 +02008836
8837 for (max = 0; start + max < end; max++)
8838 if (start[max] == '\r' || start[max] == '\n')
8839 break;
8840
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008841 UBOUND(max, trash.size - trash.len - 3);
8842 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8843 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008844 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008845}
8846
Willy Tarreaueee5b512015-04-03 23:46:31 +02008847
8848/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8849 * The hdr_idx is allocated as well. In case of allocation failure, everything
8850 * allocated is freed and NULL is returned. Otherwise the new transaction is
8851 * assigned to the stream and returned.
8852 */
8853struct http_txn *http_alloc_txn(struct stream *s)
8854{
8855 struct http_txn *txn = s->txn;
8856
8857 if (txn)
8858 return txn;
8859
8860 txn = pool_alloc2(pool2_http_txn);
8861 if (!txn)
8862 return txn;
8863
8864 txn->hdr_idx.size = global.tune.max_http_hdr;
8865 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8866 if (!txn->hdr_idx.v) {
8867 pool_free2(pool2_http_txn, txn);
8868 return NULL;
8869 }
8870
8871 s->txn = txn;
8872 return txn;
8873}
8874
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008875void http_txn_reset_req(struct http_txn *txn)
8876{
8877 txn->req.flags = 0;
8878 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8879 txn->req.next = 0;
8880 txn->req.chunk_len = 0LL;
8881 txn->req.body_len = 0LL;
8882 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8883}
8884
8885void http_txn_reset_res(struct http_txn *txn)
8886{
8887 txn->rsp.flags = 0;
8888 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8889 txn->rsp.next = 0;
8890 txn->rsp.chunk_len = 0LL;
8891 txn->rsp.body_len = 0LL;
8892 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8893}
8894
Willy Tarreau0937bc42009-12-22 15:03:09 +01008895/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008896 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008897 * the required fields are properly allocated and that we only need to (re)init
8898 * them. This should be used before processing any new request.
8899 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008900void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008901{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008902 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008903 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008904
8905 txn->flags = 0;
8906 txn->status = -1;
8907
Willy Tarreauf64d1412010-10-07 20:06:11 +02008908 txn->cookie_first_date = 0;
8909 txn->cookie_last_date = 0;
8910
Willy Tarreaueee5b512015-04-03 23:46:31 +02008911 txn->srv_cookie = NULL;
8912 txn->cli_cookie = NULL;
8913 txn->uri = NULL;
8914
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008915 http_txn_reset_req(txn);
8916 http_txn_reset_res(txn);
8917
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008918 txn->req.chn = &s->req;
8919 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008920
8921 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008922
8923 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8924 if (fe->options2 & PR_O2_REQBUG_OK)
8925 txn->req.err_pos = -1; /* let buggy requests pass */
8926
Willy Tarreau0937bc42009-12-22 15:03:09 +01008927 if (txn->hdr_idx.v)
8928 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008929
8930 vars_init(&s->vars_txn, SCOPE_TXN);
8931 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008932}
8933
8934/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008935void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008936{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008937 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008938 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008939
Willy Tarreau75195602014-03-11 15:48:55 +01008940 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008941 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008942 s->comp_algo->end(&s->comp_ctx);
8943 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008944 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008945
Willy Tarreau0937bc42009-12-22 15:03:09 +01008946 /* these ones will have been dynamically allocated */
8947 pool_free2(pool2_requri, txn->uri);
8948 pool_free2(pool2_capture, txn->cli_cookie);
8949 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008950 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008951
William Lallemanda73203e2012-03-12 12:48:57 +01008952 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008953 txn->uri = NULL;
8954 txn->srv_cookie = NULL;
8955 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008956
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008957 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008958 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008959 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008960 pool_free2(h->pool, s->req_cap[h->index]);
8961 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008962 }
8963
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008964 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008965 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008966 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008967 pool_free2(h->pool, s->res_cap[h->index]);
8968 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008969 }
8970
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008971 vars_prune(&s->vars_txn, s);
8972 vars_prune(&s->vars_reqres, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008973}
8974
8975/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008976void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008977{
8978 http_end_txn(s);
8979 http_init_txn(s);
8980
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008981 /* reinitialise the current rule list pointer to NULL. We are sure that
8982 * any rulelist match the NULL pointer.
8983 */
8984 s->current_rule_list = NULL;
8985
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008986 s->be = strm_fe(s);
8987 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008988 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008989 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008990 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008991 /* re-init store persistence */
8992 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008993 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008994
Willy Tarreau0937bc42009-12-22 15:03:09 +01008995 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008996
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008997 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008998
Willy Tarreau739cfba2010-01-25 23:11:14 +01008999 /* We must trim any excess data from the response buffer, because we
9000 * may have blocked an invalid response from a server that we don't
9001 * want to accidentely forward once we disable the analysers, nor do
9002 * we want those data to come along with next response. A typical
9003 * example of such data would be from a buggy server responding to
9004 * a HEAD with some data, or sending more than the advertised
9005 * content-length.
9006 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009007 if (unlikely(s->res.buf->i))
9008 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01009009
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009010 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009011 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009012
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009013 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009014 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009015
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009016 s->req.rex = TICK_ETERNITY;
9017 s->req.wex = TICK_ETERNITY;
9018 s->req.analyse_exp = TICK_ETERNITY;
9019 s->res.rex = TICK_ETERNITY;
9020 s->res.wex = TICK_ETERNITY;
9021 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009022}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009023
Sasha Pachev218f0642014-06-16 12:05:59 -06009024void free_http_res_rules(struct list *r)
9025{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009026 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06009027
9028 list_for_each_entry_safe(pr, tr, r, list) {
9029 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009030 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009031 free(pr);
9032 }
9033}
9034
9035void free_http_req_rules(struct list *r)
9036{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009037 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009038
9039 list_for_each_entry_safe(pr, tr, r, list) {
9040 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009041 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009042 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009043
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009044 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009045 free(pr);
9046 }
9047}
9048
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009049/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009050struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009051{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009052 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009053 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009054 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009055 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009056
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009057 rule = (struct act_rule*)calloc(1, sizeof(struct act_rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009058 if (!rule) {
9059 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009060 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009061 }
9062
CJ Ess108b1dd2015-04-07 12:03:37 -04009063 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009064 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009065 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009066 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009067 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009068 int code;
9069 int hc;
9070
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009071 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01009072 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009073 if (strcmp(args[cur_arg], "deny_status") == 0) {
9074 cur_arg++;
9075 if (!args[cur_arg]) {
9076 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9077 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9078 goto out_err;
9079 }
9080
9081 code = atol(args[cur_arg]);
9082 cur_arg++;
9083 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9084 if (http_err_codes[hc] == code) {
9085 rule->deny_status = hc;
9086 break;
9087 }
9088 }
9089
9090 if (hc >= HTTP_ERR_SIZE) {
9091 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9092 file, linenum, code);
9093 }
9094 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009095 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009096 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009097 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009098 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009099 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009100 cur_arg = 1;
9101
9102 while(*args[cur_arg]) {
9103 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009104 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009105 cur_arg+=2;
9106 continue;
9107 } else
9108 break;
9109 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009110 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009111 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009112 cur_arg = 1;
9113
9114 if (!*args[cur_arg] ||
9115 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9116 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9117 file, linenum, args[0]);
9118 goto out_err;
9119 }
9120 rule->arg.nice = atoi(args[cur_arg]);
9121 if (rule->arg.nice < -1024)
9122 rule->arg.nice = -1024;
9123 else if (rule->arg.nice > 1024)
9124 rule->arg.nice = 1024;
9125 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009126 } else if (!strcmp(args[0], "set-tos")) {
9127#ifdef IP_TOS
9128 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009129 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009130 cur_arg = 1;
9131
9132 if (!*args[cur_arg] ||
9133 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9134 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9135 file, linenum, args[0]);
9136 goto out_err;
9137 }
9138
9139 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9140 if (err && *err != '\0') {
9141 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9142 file, linenum, err, args[0]);
9143 goto out_err;
9144 }
9145 cur_arg++;
9146#else
9147 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9148 goto out_err;
9149#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009150 } else if (!strcmp(args[0], "set-mark")) {
9151#ifdef SO_MARK
9152 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009153 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009154 cur_arg = 1;
9155
9156 if (!*args[cur_arg] ||
9157 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9158 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9159 file, linenum, args[0]);
9160 goto out_err;
9161 }
9162
9163 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9164 if (err && *err != '\0') {
9165 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9166 file, linenum, err, args[0]);
9167 goto out_err;
9168 }
9169 cur_arg++;
9170 global.last_checks |= LSTCHK_NETADM;
9171#else
9172 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9173 goto out_err;
9174#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009175 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009176 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009177 cur_arg = 1;
9178
9179 if (!*args[cur_arg] ||
9180 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9181 bad_log_level:
9182 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9183 file, linenum, args[0]);
9184 goto out_err;
9185 }
9186 if (strcmp(args[cur_arg], "silent") == 0)
9187 rule->arg.loglevel = -1;
9188 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9189 goto bad_log_level;
9190 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009191 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009192 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009193 cur_arg = 1;
9194
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009195 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9196 (*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 +01009197 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9198 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009199 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009200 }
9201
9202 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9203 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9204 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009205
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009206 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009207 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009208 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9209 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009210 free(proxy->conf.lfs_file);
9211 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9212 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009213 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009214 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009215 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009216 cur_arg = 1;
9217
9218 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009219 (*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 -06009220 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9221 file, linenum, args[0]);
9222 goto out_err;
9223 }
9224
9225 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9226 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9227 LIST_INIT(&rule->arg.hdr_add.fmt);
9228
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009229 error = NULL;
9230 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9231 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9232 args[cur_arg + 1], error);
9233 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009234 goto out_err;
9235 }
9236
9237 proxy->conf.args.ctx = ARGC_HRQ;
9238 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9239 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9240 file, linenum);
9241
9242 free(proxy->conf.lfs_file);
9243 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9244 proxy->conf.lfs_line = proxy->conf.args.line;
9245 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009246 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009247 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009248 cur_arg = 1;
9249
9250 if (!*args[cur_arg] ||
9251 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9252 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9253 file, linenum, args[0]);
9254 goto out_err;
9255 }
9256
9257 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9258 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9259
9260 proxy->conf.args.ctx = ARGC_HRQ;
9261 free(proxy->conf.lfs_file);
9262 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9263 proxy->conf.lfs_line = proxy->conf.args.line;
9264 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009265 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9266 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009267 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009268 struct sample_expr *expr;
9269 unsigned int where;
9270 char *err = NULL;
9271
9272 cur_arg = 1;
9273 proxy->conf.args.ctx = ARGC_TRK;
9274
9275 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9276 if (!expr) {
9277 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9278 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9279 free(err);
9280 goto out_err;
9281 }
9282
9283 where = 0;
9284 if (proxy->cap & PR_CAP_FE)
9285 where |= SMP_VAL_FE_HRQ_HDR;
9286 if (proxy->cap & PR_CAP_BE)
9287 where |= SMP_VAL_BE_HRQ_HDR;
9288
9289 if (!(expr->fetch->val & where)) {
9290 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9291 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9292 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9293 args[cur_arg-1], sample_src_names(expr->fetch->use));
9294 free(expr);
9295 goto out_err;
9296 }
9297
9298 if (strcmp(args[cur_arg], "table") == 0) {
9299 cur_arg++;
9300 if (!args[cur_arg]) {
9301 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9302 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9303 free(expr);
9304 goto out_err;
9305 }
9306 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009307 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009308 cur_arg++;
9309 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009310 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009311 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009312 } else if (strcmp(args[0], "redirect") == 0) {
9313 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009314 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009315
Willy Tarreaube4653b2015-05-28 15:26:58 +02009316 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009317 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9318 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9319 goto out_err;
9320 }
9321
9322 /* this redirect rule might already contain a parsed condition which
9323 * we'll pass to the http-request rule.
9324 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009325 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009326 rule->arg.redir = redir;
9327 rule->cond = redir->cond;
9328 redir->cond = NULL;
9329 cur_arg = 2;
9330 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009331 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9332 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009333 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009334 /*
9335 * '+ 8' for 'add-acl('
9336 * '- 9' for 'add-acl(' + trailing ')'
9337 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009338 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009339
9340 cur_arg = 1;
9341
9342 if (!*args[cur_arg] ||
9343 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9344 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9345 file, linenum, args[0]);
9346 goto out_err;
9347 }
9348
9349 LIST_INIT(&rule->arg.map.key);
9350 proxy->conf.args.ctx = ARGC_HRQ;
9351 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9352 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9353 file, linenum);
9354 free(proxy->conf.lfs_file);
9355 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9356 proxy->conf.lfs_line = proxy->conf.args.line;
9357 cur_arg += 1;
9358 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9359 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009360 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009361 /*
9362 * '+ 8' for 'del-acl('
9363 * '- 9' for 'del-acl(' + trailing ')'
9364 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009365 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009366
9367 cur_arg = 1;
9368
9369 if (!*args[cur_arg] ||
9370 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9371 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9372 file, linenum, args[0]);
9373 goto out_err;
9374 }
9375
9376 LIST_INIT(&rule->arg.map.key);
9377 proxy->conf.args.ctx = ARGC_HRQ;
9378 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9379 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9380 file, linenum);
9381 free(proxy->conf.lfs_file);
9382 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9383 proxy->conf.lfs_line = proxy->conf.args.line;
9384 cur_arg += 1;
9385 } else if (strncmp(args[0], "del-map", 7) == 0) {
9386 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009387 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009388 /*
9389 * '+ 8' for 'del-map('
9390 * '- 9' for 'del-map(' + trailing ')'
9391 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009392 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009393
9394 cur_arg = 1;
9395
9396 if (!*args[cur_arg] ||
9397 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9398 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9399 file, linenum, args[0]);
9400 goto out_err;
9401 }
9402
9403 LIST_INIT(&rule->arg.map.key);
9404 proxy->conf.args.ctx = ARGC_HRQ;
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 free(proxy->conf.lfs_file);
9409 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9410 proxy->conf.lfs_line = proxy->conf.args.line;
9411 cur_arg += 1;
9412 } else if (strncmp(args[0], "set-map", 7) == 0) {
9413 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009414 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009415 /*
9416 * '+ 8' for 'set-map('
9417 * '- 9' for 'set-map(' + trailing ')'
9418 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009419 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009420
9421 cur_arg = 1;
9422
9423 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9424 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9425 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9426 file, linenum, args[0]);
9427 goto out_err;
9428 }
9429
9430 LIST_INIT(&rule->arg.map.key);
9431 LIST_INIT(&rule->arg.map.value);
9432 proxy->conf.args.ctx = ARGC_HRQ;
9433
9434 /* key pattern */
9435 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9436 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9437 file, linenum);
9438
9439 /* value pattern */
9440 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9441 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9442 file, linenum);
9443 free(proxy->conf.lfs_file);
9444 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9445 proxy->conf.lfs_line = proxy->conf.args.line;
9446
9447 cur_arg += 2;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009448 } else if (strncmp(args[0], "set-src", 7) == 0) {
9449 struct sample_expr *expr;
9450 unsigned int where;
9451 char *err = NULL;
9452
9453 cur_arg = 1;
9454 proxy->conf.args.ctx = ARGC_HRQ;
9455
9456 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9457 if (!expr) {
9458 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9459 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9460 free(err);
9461 goto out_err;
9462 }
9463
9464 where = 0;
9465 if (proxy->cap & PR_CAP_FE)
9466 where |= SMP_VAL_FE_HRQ_HDR;
9467 if (proxy->cap & PR_CAP_BE)
9468 where |= SMP_VAL_BE_HRQ_HDR;
9469
9470 if (!(expr->fetch->val & where)) {
9471 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9472 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9473 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9474 args[cur_arg-1], sample_src_names(expr->fetch->use));
9475 free(expr);
9476 goto out_err;
9477 }
9478
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02009479 rule->arg.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009480 rule->action = ACT_HTTP_REQ_SET_SRC;
William Lallemand73025dd2014-04-24 14:38:37 +02009481 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9482 char *errmsg = NULL;
9483 cur_arg = 1;
9484 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009485 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009486 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009487 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009488 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9489 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9490 free(errmsg);
9491 goto out_err;
9492 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009493 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009494 action_build_list(&http_req_keywords.list, &trash);
9495 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9496 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9497 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', "
9498 "'set-src'%s%s, but got '%s'%s.\n",
9499 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009500 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009501 }
9502
9503 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9504 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009505 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009506
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009507 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9508 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9509 file, linenum, args[0], errmsg);
9510 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009511 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009512 }
9513 rule->cond = cond;
9514 }
9515 else if (*args[cur_arg]) {
9516 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9517 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9518 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009519 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009520 }
9521
9522 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009523 out_err:
9524 free(rule);
9525 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009526}
9527
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009528/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009529struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009530{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009531 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009532 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009533 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009534 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009535
9536 rule = calloc(1, sizeof(*rule));
9537 if (!rule) {
9538 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9539 goto out_err;
9540 }
9541
9542 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009543 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009544 cur_arg = 1;
9545 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009546 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009547 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009548 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009549 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009550 cur_arg = 1;
9551
9552 if (!*args[cur_arg] ||
9553 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9554 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9555 file, linenum, args[0]);
9556 goto out_err;
9557 }
9558 rule->arg.nice = atoi(args[cur_arg]);
9559 if (rule->arg.nice < -1024)
9560 rule->arg.nice = -1024;
9561 else if (rule->arg.nice > 1024)
9562 rule->arg.nice = 1024;
9563 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009564 } else if (!strcmp(args[0], "set-tos")) {
9565#ifdef IP_TOS
9566 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009567 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009568 cur_arg = 1;
9569
9570 if (!*args[cur_arg] ||
9571 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9572 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9573 file, linenum, args[0]);
9574 goto out_err;
9575 }
9576
9577 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9578 if (err && *err != '\0') {
9579 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9580 file, linenum, err, args[0]);
9581 goto out_err;
9582 }
9583 cur_arg++;
9584#else
9585 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9586 goto out_err;
9587#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009588 } else if (!strcmp(args[0], "set-mark")) {
9589#ifdef SO_MARK
9590 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009591 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009592 cur_arg = 1;
9593
9594 if (!*args[cur_arg] ||
9595 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9596 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9597 file, linenum, args[0]);
9598 goto out_err;
9599 }
9600
9601 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9602 if (err && *err != '\0') {
9603 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9604 file, linenum, err, args[0]);
9605 goto out_err;
9606 }
9607 cur_arg++;
9608 global.last_checks |= LSTCHK_NETADM;
9609#else
9610 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9611 goto out_err;
9612#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009613 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009614 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009615 cur_arg = 1;
9616
9617 if (!*args[cur_arg] ||
9618 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9619 bad_log_level:
9620 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9621 file, linenum, args[0]);
9622 goto out_err;
9623 }
9624 if (strcmp(args[cur_arg], "silent") == 0)
9625 rule->arg.loglevel = -1;
9626 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9627 goto bad_log_level;
9628 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009629 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009630 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009631 cur_arg = 1;
9632
9633 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9634 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9635 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9636 file, linenum, args[0]);
9637 goto out_err;
9638 }
9639
9640 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9641 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9642 LIST_INIT(&rule->arg.hdr_add.fmt);
9643
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009644 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009645 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009646 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9647 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009648 free(proxy->conf.lfs_file);
9649 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9650 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009651 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009652 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009653 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009654 cur_arg = 1;
9655
9656 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009657 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9658 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009659 file, linenum, args[0]);
9660 goto out_err;
9661 }
9662
9663 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9664 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9665 LIST_INIT(&rule->arg.hdr_add.fmt);
9666
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009667 error = NULL;
9668 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9669 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9670 args[cur_arg + 1], error);
9671 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009672 goto out_err;
9673 }
9674
9675 proxy->conf.args.ctx = ARGC_HRQ;
9676 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9677 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9678 file, linenum);
9679
9680 free(proxy->conf.lfs_file);
9681 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9682 proxy->conf.lfs_line = proxy->conf.args.line;
9683 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009684 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009685 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009686 cur_arg = 1;
9687
9688 if (!*args[cur_arg] ||
9689 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9690 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9691 file, linenum, args[0]);
9692 goto out_err;
9693 }
9694
9695 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9696 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9697
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009698 proxy->conf.args.ctx = ARGC_HRS;
9699 free(proxy->conf.lfs_file);
9700 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9701 proxy->conf.lfs_line = proxy->conf.args.line;
9702 cur_arg += 1;
9703 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9704 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009705 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009706 /*
9707 * '+ 8' for 'add-acl('
9708 * '- 9' for 'add-acl(' + trailing ')'
9709 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009710 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009711
9712 cur_arg = 1;
9713
9714 if (!*args[cur_arg] ||
9715 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9716 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9717 file, linenum, args[0]);
9718 goto out_err;
9719 }
9720
9721 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009722 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009723 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9724 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9725 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009726 free(proxy->conf.lfs_file);
9727 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9728 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009729
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009730 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009731 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9732 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009733 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009734 /*
9735 * '+ 8' for 'del-acl('
9736 * '- 9' for 'del-acl(' + trailing ')'
9737 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009738 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009739
9740 cur_arg = 1;
9741
9742 if (!*args[cur_arg] ||
9743 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9744 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9745 file, linenum, args[0]);
9746 goto out_err;
9747 }
9748
9749 LIST_INIT(&rule->arg.map.key);
9750 proxy->conf.args.ctx = ARGC_HRS;
9751 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9752 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9753 file, linenum);
9754 free(proxy->conf.lfs_file);
9755 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9756 proxy->conf.lfs_line = proxy->conf.args.line;
9757 cur_arg += 1;
9758 } else if (strncmp(args[0], "del-map", 7) == 0) {
9759 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009760 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009761 /*
9762 * '+ 8' for 'del-map('
9763 * '- 9' for 'del-map(' + trailing ')'
9764 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009765 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009766
9767 cur_arg = 1;
9768
9769 if (!*args[cur_arg] ||
9770 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9771 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9772 file, linenum, args[0]);
9773 goto out_err;
9774 }
9775
9776 LIST_INIT(&rule->arg.map.key);
9777 proxy->conf.args.ctx = ARGC_HRS;
9778 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9779 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9780 file, linenum);
9781 free(proxy->conf.lfs_file);
9782 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9783 proxy->conf.lfs_line = proxy->conf.args.line;
9784 cur_arg += 1;
9785 } else if (strncmp(args[0], "set-map", 7) == 0) {
9786 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009787 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009788 /*
9789 * '+ 8' for 'set-map('
9790 * '- 9' for 'set-map(' + trailing ')'
9791 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009792 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009793
9794 cur_arg = 1;
9795
9796 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9797 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9798 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9799 file, linenum, args[0]);
9800 goto out_err;
9801 }
9802
9803 LIST_INIT(&rule->arg.map.key);
9804 LIST_INIT(&rule->arg.map.value);
9805
9806 proxy->conf.args.ctx = ARGC_HRS;
9807
9808 /* key pattern */
9809 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9810 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9811 file, linenum);
9812
9813 /* value pattern */
9814 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9815 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9816 file, linenum);
9817
9818 free(proxy->conf.lfs_file);
9819 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9820 proxy->conf.lfs_line = proxy->conf.args.line;
9821
9822 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009823 } else if (strcmp(args[0], "redirect") == 0) {
9824 struct redirect_rule *redir;
9825 char *errmsg = NULL;
9826
9827 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9828 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9829 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9830 goto out_err;
9831 }
9832
9833 /* this redirect rule might already contain a parsed condition which
9834 * we'll pass to the http-request rule.
9835 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009836 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009837 rule->arg.redir = redir;
9838 rule->cond = redir->cond;
9839 redir->cond = NULL;
9840 cur_arg = 2;
9841 return rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009842 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9843 char *errmsg = NULL;
9844 cur_arg = 1;
9845 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009846 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009847 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009848 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009849 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9850 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9851 free(errmsg);
9852 goto out_err;
9853 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009854 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009855 action_build_list(&http_res_keywords.list, &trash);
9856 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9857 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9858 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', "
9859 "'set-src'%s%s, but got '%s'%s.\n",
9860 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009861 goto out_err;
9862 }
9863
9864 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9865 struct acl_cond *cond;
9866 char *errmsg = NULL;
9867
9868 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9869 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9870 file, linenum, args[0], errmsg);
9871 free(errmsg);
9872 goto out_err;
9873 }
9874 rule->cond = cond;
9875 }
9876 else if (*args[cur_arg]) {
9877 Alert("parsing [%s:%d]: 'http-response %s' expects"
9878 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9879 file, linenum, args[0], args[cur_arg]);
9880 goto out_err;
9881 }
9882
9883 return rule;
9884 out_err:
9885 free(rule);
9886 return NULL;
9887}
9888
Willy Tarreau4baae242012-12-27 12:00:31 +01009889/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009890 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009891 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9892 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009893 */
9894struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009895 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009896{
9897 struct redirect_rule *rule;
9898 int cur_arg;
9899 int type = REDIRECT_TYPE_NONE;
9900 int code = 302;
9901 const char *destination = NULL;
9902 const char *cookie = NULL;
9903 int cookie_set = 0;
9904 unsigned int flags = REDIRECT_FLAG_NONE;
9905 struct acl_cond *cond = NULL;
9906
9907 cur_arg = 0;
9908 while (*(args[cur_arg])) {
9909 if (strcmp(args[cur_arg], "location") == 0) {
9910 if (!*args[cur_arg + 1])
9911 goto missing_arg;
9912
9913 type = REDIRECT_TYPE_LOCATION;
9914 cur_arg++;
9915 destination = args[cur_arg];
9916 }
9917 else if (strcmp(args[cur_arg], "prefix") == 0) {
9918 if (!*args[cur_arg + 1])
9919 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009920 type = REDIRECT_TYPE_PREFIX;
9921 cur_arg++;
9922 destination = args[cur_arg];
9923 }
9924 else if (strcmp(args[cur_arg], "scheme") == 0) {
9925 if (!*args[cur_arg + 1])
9926 goto missing_arg;
9927
9928 type = REDIRECT_TYPE_SCHEME;
9929 cur_arg++;
9930 destination = args[cur_arg];
9931 }
9932 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9933 if (!*args[cur_arg + 1])
9934 goto missing_arg;
9935
9936 cur_arg++;
9937 cookie = args[cur_arg];
9938 cookie_set = 1;
9939 }
9940 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9941 if (!*args[cur_arg + 1])
9942 goto missing_arg;
9943
9944 cur_arg++;
9945 cookie = args[cur_arg];
9946 cookie_set = 0;
9947 }
9948 else if (strcmp(args[cur_arg], "code") == 0) {
9949 if (!*args[cur_arg + 1])
9950 goto missing_arg;
9951
9952 cur_arg++;
9953 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009954 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009955 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009956 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009957 args[cur_arg - 1], args[cur_arg]);
9958 return NULL;
9959 }
9960 }
9961 else if (!strcmp(args[cur_arg],"drop-query")) {
9962 flags |= REDIRECT_FLAG_DROP_QS;
9963 }
9964 else if (!strcmp(args[cur_arg],"append-slash")) {
9965 flags |= REDIRECT_FLAG_APPEND_SLASH;
9966 }
9967 else if (strcmp(args[cur_arg], "if") == 0 ||
9968 strcmp(args[cur_arg], "unless") == 0) {
9969 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9970 if (!cond) {
9971 memprintf(errmsg, "error in condition: %s", *errmsg);
9972 return NULL;
9973 }
9974 break;
9975 }
9976 else {
9977 memprintf(errmsg,
9978 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9979 args[cur_arg]);
9980 return NULL;
9981 }
9982 cur_arg++;
9983 }
9984
9985 if (type == REDIRECT_TYPE_NONE) {
9986 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9987 return NULL;
9988 }
9989
Willy Tarreaube4653b2015-05-28 15:26:58 +02009990 if (dir && type != REDIRECT_TYPE_LOCATION) {
9991 memprintf(errmsg, "response only supports redirect type 'location'");
9992 return NULL;
9993 }
9994
Willy Tarreau4baae242012-12-27 12:00:31 +01009995 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9996 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009997 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009998
9999 if (!use_fmt) {
10000 /* old-style static redirect rule */
10001 rule->rdr_str = strdup(destination);
10002 rule->rdr_len = strlen(destination);
10003 }
10004 else {
10005 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010006
10007 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
10008 * if prefix == "/", we don't want to add anything, otherwise it
10009 * makes it hard for the user to configure a self-redirection.
10010 */
Godbachd9722032014-12-18 15:44:58 +080010011 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010012 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +010010013 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +020010014 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
10015 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +010010016 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010017 free(curproxy->conf.lfs_file);
10018 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10019 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010020 }
10021 }
10022
Willy Tarreau4baae242012-12-27 12:00:31 +010010023 if (cookie) {
10024 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10025 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10026 */
10027 rule->cookie_len = strlen(cookie);
10028 if (cookie_set) {
10029 rule->cookie_str = malloc(rule->cookie_len + 10);
10030 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10031 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10032 rule->cookie_len += 9;
10033 } else {
10034 rule->cookie_str = malloc(rule->cookie_len + 21);
10035 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10036 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10037 rule->cookie_len += 20;
10038 }
10039 }
10040 rule->type = type;
10041 rule->code = code;
10042 rule->flags = flags;
10043 LIST_INIT(&rule->list);
10044 return rule;
10045
10046 missing_arg:
10047 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10048 return NULL;
10049}
10050
Willy Tarreau8797c062007-05-07 00:55:35 +020010051/************************************************************************/
10052/* The code below is dedicated to ACL parsing and matching */
10053/************************************************************************/
10054
10055
Willy Tarreau14174bc2012-04-16 14:34:04 +020010056/* This function ensures that the prerequisites for an L7 fetch are ready,
10057 * which means that a request or response is ready. If some data is missing,
10058 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010059 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10060 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010061 *
10062 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010063 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10064 * decide whether or not an HTTP message is present ;
10065 * 0 if the requested data cannot be fetched or if it is certain that
10066 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010067 * 1 if an HTTP message is ready
10068 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010069int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010070 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010071{
Willy Tarreau192252e2015-04-04 01:47:55 +020010072 struct http_txn *txn;
10073 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010074
Willy Tarreaueee5b512015-04-03 23:46:31 +020010075 /* Note: this function may only be used from places where
10076 * http_init_txn() has already been done, and implies that <s>,
10077 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
10078 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010079 */
10080
Willy Tarreau192252e2015-04-04 01:47:55 +020010081 if (!s)
10082 return 0;
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010083 if (!s->txn) {
10084 if (unlikely(!http_alloc_txn(s)))
10085 return 0; /* not enough memory */
10086 http_init_txn(s);
10087 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010088 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010089 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010090
10091 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010092 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010093
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010094 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010095 /* If the buffer does not leave enough free space at the end,
10096 * we must first realign it.
10097 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010098 if (s->req.buf->p > s->req.buf->data &&
10099 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10100 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010101
Willy Tarreau14174bc2012-04-16 14:34:04 +020010102 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010103 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010104 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010105
10106 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010107 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010108 http_msg_analyzer(msg, &txn->hdr_idx);
10109
10110 /* Still no valid request ? */
10111 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010112 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010113 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010114 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010115 }
10116 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010117 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010118 return 0;
10119 }
10120
10121 /* OK we just got a valid HTTP request. We have some minor
10122 * preparation to perform so that further checks can rely
10123 * on HTTP tests.
10124 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010125
10126 /* If the request was parsed but was too large, we must absolutely
10127 * return an error so that it is not processed. At the moment this
10128 * cannot happen, but if the parsers are to change in the future,
10129 * we want this check to be maintained.
10130 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010131 if (unlikely(s->req.buf->i + s->req.buf->p >
10132 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010133 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010134 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010135 return 1;
10136 }
10137
Willy Tarreau9b28e032012-10-12 23:49:43 +020010138 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010139 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010140 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010141
Willy Tarreau506d0502013-07-06 13:29:24 +020010142 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10143 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010144 }
10145
Willy Tarreau506d0502013-07-06 13:29:24 +020010146 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010147 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010148 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010149
10150 /* otherwise everything's ready for the request */
10151 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010152 else {
10153 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010154 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10155 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010156 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010157 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010158 }
10159
10160 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010161 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010162 return 1;
10163}
Willy Tarreau8797c062007-05-07 00:55:35 +020010164
Willy Tarreau8797c062007-05-07 00:55:35 +020010165/* 1. Check on METHOD
10166 * We use the pre-parsed method if it is known, and store its number as an
10167 * integer. If it is unknown, we use the pointer and the length.
10168 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010169static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010170{
10171 int len, meth;
10172
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010173 len = strlen(text);
10174 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010175
10176 pattern->val.i = meth;
10177 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010178 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010179 pattern->len = len;
10180 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010181 else {
10182 pattern->ptr.str = NULL;
10183 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010184 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010185 return 1;
10186}
10187
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010188/* This function fetches the method of current HTTP request and stores
10189 * it in the global pattern struct as a chunk. There are two possibilities :
10190 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10191 * in <len> and <ptr> is NULL ;
10192 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10193 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010194 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010195 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010196static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010197smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010198{
10199 int meth;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010200 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010201
Willy Tarreau24e32d82012-04-23 23:55:44 +020010202 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010203
Willy Tarreau8797c062007-05-07 00:55:35 +020010204 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010205 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010206 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010207 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010208 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10209 /* ensure the indexes are not affected */
10210 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010211 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010212 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10213 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010214 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010215 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010216 return 1;
10217}
10218
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010219/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010220static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010221{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010222 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010223 struct pattern_list *lst;
10224 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010225
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010226 list_for_each_entry(lst, &expr->patterns, list) {
10227 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010228
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010229 /* well-known method */
10230 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010231 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010232 return pattern;
10233 else
10234 continue;
10235 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010236
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010237 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010238 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010239 continue;
10240
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010241 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010242 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10243 (!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 +010010244 return pattern;
10245 }
10246 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010247}
10248
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010249static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010250smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010251{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010252 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010253 char *ptr;
10254 int len;
10255
Willy Tarreauc0239e02012-04-16 14:42:55 +020010256 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010257
Willy Tarreau8797c062007-05-07 00:55:35 +020010258 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010259 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010260
10261 while ((len-- > 0) && (*ptr++ != '/'));
10262 if (len <= 0)
10263 return 0;
10264
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010265 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010266 smp->data.u.str.str = ptr;
10267 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010268
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010269 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010270 return 1;
10271}
10272
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010273static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010274smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010275{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010276 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010277 char *ptr;
10278 int len;
10279
Willy Tarreauc0239e02012-04-16 14:42:55 +020010280 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010281
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010282 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010283 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10284 return 0;
10285
Willy Tarreau8797c062007-05-07 00:55:35 +020010286 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010287 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010288
10289 while ((len-- > 0) && (*ptr++ != '/'));
10290 if (len <= 0)
10291 return 0;
10292
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010293 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010294 smp->data.u.str.str = ptr;
10295 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010296
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010297 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010298 return 1;
10299}
10300
10301/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010302static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010303smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010304{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010305 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010306 char *ptr;
10307 int len;
10308
Willy Tarreauc0239e02012-04-16 14:42:55 +020010309 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010310
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010311 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010312 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10313 return 0;
10314
Willy Tarreau8797c062007-05-07 00:55:35 +020010315 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010316 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010317
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010318 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010319 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010320 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010321 return 1;
10322}
10323
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010324/* returns the longest available part of the body. This requires that the body
10325 * has been waited for using http-buffer-request.
10326 */
10327static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010328smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010329{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010330 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010331 struct http_msg *msg;
10332 unsigned long len;
10333 unsigned long block1;
10334 char *body;
10335 struct chunk *temp;
10336
10337 CHECK_HTTP_MESSAGE_FIRST();
10338
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010339 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010340 msg = &txn->req;
10341 else
10342 msg = &txn->rsp;
10343
10344 len = http_body_bytes(msg);
10345 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10346
10347 block1 = len;
10348 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10349 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10350
10351 if (block1 == len) {
10352 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010353 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010354 smp->data.u.str.str = body;
10355 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010356 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10357 }
10358 else {
10359 /* buffer is wrapped, we need to defragment it */
10360 temp = get_trash_chunk();
10361 memcpy(temp->str, body, block1);
10362 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010363 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010364 smp->data.u.str.str = temp->str;
10365 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010366 smp->flags = SMP_F_VOL_TEST;
10367 }
10368 return 1;
10369}
10370
10371
10372/* returns the available length of the body. This requires that the body
10373 * has been waited for using http-buffer-request.
10374 */
10375static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010376smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010377{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010378 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010379 struct http_msg *msg;
10380
10381 CHECK_HTTP_MESSAGE_FIRST();
10382
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010383 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010384 msg = &txn->req;
10385 else
10386 msg = &txn->rsp;
10387
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010388 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010389 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010390
10391 smp->flags = SMP_F_VOL_TEST;
10392 return 1;
10393}
10394
10395
10396/* returns the advertised length of the body, or the advertised size of the
10397 * chunks available in the buffer. This requires that the body has been waited
10398 * for using http-buffer-request.
10399 */
10400static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010401smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010402{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010403 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010404 struct http_msg *msg;
10405
10406 CHECK_HTTP_MESSAGE_FIRST();
10407
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010408 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010409 msg = &txn->req;
10410 else
10411 msg = &txn->rsp;
10412
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010413 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010414 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010415
10416 smp->flags = SMP_F_VOL_TEST;
10417 return 1;
10418}
10419
10420
Willy Tarreau8797c062007-05-07 00:55:35 +020010421/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010422static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010423smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010424{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010425 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010426
Willy Tarreauc0239e02012-04-16 14:42:55 +020010427 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010428
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010429 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010430 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010431 smp->data.u.str.len = txn->req.sl.rq.u_l;
10432 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010433 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010434 return 1;
10435}
10436
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010437static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010438smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010439{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010440 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010441 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010442
Willy Tarreauc0239e02012-04-16 14:42:55 +020010443 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010444
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010445 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010446 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 +020010447 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010448 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010449
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010450 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010451 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010452 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010453 return 1;
10454}
10455
10456static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010457smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010458{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010459 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010460 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010461
Willy Tarreauc0239e02012-04-16 14:42:55 +020010462 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010463
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010464 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010465 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 +020010466 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10467 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010468
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010469 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010470 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010471 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010472 return 1;
10473}
10474
Willy Tarreau185b5c42012-04-26 15:11:51 +020010475/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10476 * Accepts an optional argument of type string containing the header field name,
10477 * and an optional argument of type signed or unsigned integer to request an
10478 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010479 * headers are considered from the first one. It does not stop on commas and
10480 * returns full lines instead (useful for User-Agent or Date for example).
10481 */
10482static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010483smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010484{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010485 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010486 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010487 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010488 int occ = 0;
10489 const char *name_str = NULL;
10490 int name_len = 0;
10491
10492 if (!ctx) {
10493 /* first call */
10494 ctx = &static_hdr_ctx;
10495 ctx->idx = 0;
10496 smp->ctx.a[0] = ctx;
10497 }
10498
10499 if (args) {
10500 if (args[0].type != ARGT_STR)
10501 return 0;
10502 name_str = args[0].data.str.str;
10503 name_len = args[0].data.str.len;
10504
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010505 if (args[1].type == ARGT_SINT)
10506 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010507 }
10508
10509 CHECK_HTTP_MESSAGE_FIRST();
10510
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010511 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010512 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 +020010513
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010514 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10515 /* search for header from the beginning */
10516 ctx->idx = 0;
10517
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010518 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010519 /* no explicit occurrence and single fetch => last header by default */
10520 occ = -1;
10521
10522 if (!occ)
10523 /* prepare to report multiple occurrences for ACL fetches */
10524 smp->flags |= SMP_F_NOT_LAST;
10525
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010526 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010527 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010528 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 +020010529 return 1;
10530
10531 smp->flags &= ~SMP_F_NOT_LAST;
10532 return 0;
10533}
10534
10535/* 6. Check on HTTP header count. The number of occurrences is returned.
10536 * Accepts exactly 1 argument of type string. It does not stop on commas and
10537 * returns full lines instead (useful for User-Agent or Date for example).
10538 */
10539static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010540smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010541{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010542 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010543 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010544 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010545 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010546 const char *name = NULL;
10547 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010548
Willy Tarreau601a4d12015-04-01 19:16:09 +020010549 if (args && args->type == ARGT_STR) {
10550 name = args->data.str.str;
10551 len = args->data.str.len;
10552 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010553
10554 CHECK_HTTP_MESSAGE_FIRST();
10555
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010556 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010557 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 +020010558
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010559 ctx.idx = 0;
10560 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010561 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010562 cnt++;
10563
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010564 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010565 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010566 smp->flags = SMP_F_VOL_HDR;
10567 return 1;
10568}
10569
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010570static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010571smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010572{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010573 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010574 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010575 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010576 struct chunk *temp;
10577 char del = ',';
10578
10579 if (args && args->type == ARGT_STR)
10580 del = *args[0].data.str.str;
10581
10582 CHECK_HTTP_MESSAGE_FIRST();
10583
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010584 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010585 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 +020010586
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010587 temp = get_trash_chunk();
10588
10589 ctx.idx = 0;
10590 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10591 if (temp->len)
10592 temp->str[temp->len++] = del;
10593 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10594 temp->len += ctx.del;
10595 }
10596
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010597 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010598 smp->data.u.str.str = temp->str;
10599 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010600 smp->flags = SMP_F_VOL_HDR;
10601 return 1;
10602}
10603
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010604/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10605 * Accepts an optional argument of type string containing the header field name,
10606 * and an optional argument of type signed or unsigned integer to request an
10607 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010608 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010609 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010610static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010611smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010612{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010613 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010614 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010615 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010616 int occ = 0;
10617 const char *name_str = NULL;
10618 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010619
Willy Tarreaua890d072013-04-02 12:01:06 +020010620 if (!ctx) {
10621 /* first call */
10622 ctx = &static_hdr_ctx;
10623 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010624 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010625 }
10626
Willy Tarreau185b5c42012-04-26 15:11:51 +020010627 if (args) {
10628 if (args[0].type != ARGT_STR)
10629 return 0;
10630 name_str = args[0].data.str.str;
10631 name_len = args[0].data.str.len;
10632
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010633 if (args[1].type == ARGT_SINT)
10634 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010635 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010636
Willy Tarreaue333ec92012-04-16 16:26:40 +020010637 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010638
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010639 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010640 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 +020010641
Willy Tarreau185b5c42012-04-26 15:11:51 +020010642 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010643 /* search for header from the beginning */
10644 ctx->idx = 0;
10645
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010646 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010647 /* no explicit occurrence and single fetch => last header by default */
10648 occ = -1;
10649
10650 if (!occ)
10651 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010652 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010653
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010654 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010655 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010656 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 +020010657 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010658
Willy Tarreau37406352012-04-23 16:16:37 +020010659 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010660 return 0;
10661}
10662
Willy Tarreauc11416f2007-06-17 16:58:38 +020010663/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010664 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010665 */
10666static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010667smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010668{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010669 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010670 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010671 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010672 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010673 const char *name = NULL;
10674 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010675
Willy Tarreau601a4d12015-04-01 19:16:09 +020010676 if (args && args->type == ARGT_STR) {
10677 name = args->data.str.str;
10678 len = args->data.str.len;
10679 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010680
Willy Tarreaue333ec92012-04-16 16:26:40 +020010681 CHECK_HTTP_MESSAGE_FIRST();
10682
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010683 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010684 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 +020010685
Willy Tarreau33a7e692007-06-10 19:45:56 +020010686 ctx.idx = 0;
10687 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010688 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010689 cnt++;
10690
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010691 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010692 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010693 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010694 return 1;
10695}
10696
Willy Tarreau185b5c42012-04-26 15:11:51 +020010697/* Fetch an HTTP header's integer value. The integer value is returned. It
10698 * takes a mandatory argument of type string and an optional one of type int
10699 * to designate a specific occurrence. It returns an unsigned integer, which
10700 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010701 */
10702static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010703smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010704{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010705 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010706
Willy Tarreauf853c462012-04-23 18:53:56 +020010707 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010708 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010709 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010710 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010711
Willy Tarreaud53e2422012-04-16 17:21:11 +020010712 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010713}
10714
Cyril Bonté69fa9922012-10-25 00:01:06 +020010715/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10716 * and an optional one of type int to designate a specific occurrence.
10717 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010718 */
10719static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010720smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010721{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010722 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010723
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010724 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010725 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010726 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010727 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010728 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010729 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010730 if (smp->data.u.str.len < temp->size - 1) {
10731 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10732 temp->str[smp->data.u.str.len] = '\0';
10733 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010734 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010735 break;
10736 }
10737 }
10738 }
10739
Willy Tarreaud53e2422012-04-16 17:21:11 +020010740 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010741 if (!(smp->flags & SMP_F_NOT_LAST))
10742 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010743 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010744 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010745}
10746
Willy Tarreau737b0c12007-06-10 21:28:46 +020010747/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10748 * the first '/' after the possible hostname, and ends before the possible '?'.
10749 */
10750static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010751smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010752{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010753 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010754 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010755
Willy Tarreauc0239e02012-04-16 14:42:55 +020010756 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010757
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010758 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010759 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010760 ptr = http_get_path(txn);
10761 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010762 return 0;
10763
10764 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010765 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010766 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010767
10768 while (ptr < end && *ptr != '?')
10769 ptr++;
10770
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010771 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010772 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010773 return 1;
10774}
10775
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010776/* This produces a concatenation of the first occurrence of the Host header
10777 * followed by the path component if it begins with a slash ('/'). This means
10778 * that '*' will not be added, resulting in exactly the first Host entry.
10779 * If no Host header is found, then the path is returned as-is. The returned
10780 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010781 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010782 */
10783static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010784smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010785{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010786 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010787 char *ptr, *end, *beg;
10788 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010789 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010790
10791 CHECK_HTTP_MESSAGE_FIRST();
10792
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010793 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010794 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010795 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010796 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010797
10798 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010799 temp = get_trash_chunk();
10800 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010801 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010802 smp->data.u.str.str = temp->str;
10803 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010804
10805 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010806 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010807 beg = http_get_path(txn);
10808 if (!beg)
10809 beg = end;
10810
10811 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10812
10813 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010814 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10815 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010816 }
10817
10818 smp->flags = SMP_F_VOL_1ST;
10819 return 1;
10820}
10821
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010822/* This produces a 32-bit hash of the concatenation of the first occurrence of
10823 * the Host header followed by the path component if it begins with a slash ('/').
10824 * This means that '*' will not be added, resulting in exactly the first Host
10825 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010826 * is hashed using the path hash followed by a full avalanche hash and provides a
10827 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010828 * high-traffic sites without having to store whole paths.
10829 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010830int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010831smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010832{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010833 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010834 struct hdr_ctx ctx;
10835 unsigned int hash = 0;
10836 char *ptr, *beg, *end;
10837 int len;
10838
10839 CHECK_HTTP_MESSAGE_FIRST();
10840
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010841 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010842 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010843 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010844 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10845 ptr = ctx.line + ctx.val;
10846 len = ctx.vlen;
10847 while (len--)
10848 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10849 }
10850
10851 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010852 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010853 beg = http_get_path(txn);
10854 if (!beg)
10855 beg = end;
10856
10857 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10858
10859 if (beg < ptr && *beg == '/') {
10860 while (beg < ptr)
10861 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10862 }
10863 hash = full_hash(hash);
10864
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010865 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010866 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010867 smp->flags = SMP_F_VOL_1ST;
10868 return 1;
10869}
10870
Willy Tarreau4a550602012-12-09 14:53:32 +010010871/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010872 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10873 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10874 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010875 * that in environments where IPv6 is insignificant, truncating the output to
10876 * 8 bytes would still work.
10877 */
10878static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010879smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010880{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010881 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010882 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010883
10884 if (!cli_conn)
10885 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010886
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010887 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010888 return 0;
10889
Willy Tarreau47ca5452012-12-23 20:22:19 +010010890 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010891 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010892 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010893
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010894 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010895 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010896 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010897 temp->len += 4;
10898 break;
10899 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010900 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010901 temp->len += 16;
10902 break;
10903 default:
10904 return 0;
10905 }
10906
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010907 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010908 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010909 return 1;
10910}
10911
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010912/* Extracts the query string, which comes after the question mark '?'. If no
10913 * question mark is found, nothing is returned. Otherwise it returns a sample
10914 * of type string carrying the whole query string.
10915 */
10916static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010917smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010918{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010919 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010920 char *ptr, *end;
10921
10922 CHECK_HTTP_MESSAGE_FIRST();
10923
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010924 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010925 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10926 end = ptr + txn->req.sl.rq.u_l;
10927
10928 /* look up the '?' */
10929 do {
10930 if (ptr == end)
10931 return 0;
10932 } while (*ptr++ != '?');
10933
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010934 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010935 smp->data.u.str.str = ptr;
10936 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010937 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10938 return 1;
10939}
10940
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010941static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010942smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010943{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010944 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10945 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10946 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010947
Willy Tarreau24e32d82012-04-23 23:55:44 +020010948 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010949
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010950 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010951 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010952 return 1;
10953}
10954
Willy Tarreau7f18e522010-10-22 20:04:13 +020010955/* return a valid test if the current request is the first one on the connection */
10956static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010957smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010958{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010959 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010960 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010961 return 1;
10962}
10963
Willy Tarreau34db1082012-04-19 17:16:54 +020010964/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010965static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010966smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010967{
10968
Willy Tarreau24e32d82012-04-23 23:55:44 +020010969 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010970 return 0;
10971
Willy Tarreauc0239e02012-04-16 14:42:55 +020010972 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010973
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010974 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010975 return 0;
10976
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010977 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010978 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010979 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010980 return 1;
10981}
Willy Tarreau8797c062007-05-07 00:55:35 +020010982
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010983/* Accepts exactly 1 argument of type userlist */
10984static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010985smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010986{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010987 if (!args || args->type != ARGT_USR)
10988 return 0;
10989
10990 CHECK_HTTP_MESSAGE_FIRST();
10991
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010992 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010993 return 0;
10994
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010995 /* if the user does not belong to the userlist or has a wrong password,
10996 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010997 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010998 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010999 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
11000 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011001 return 0;
11002
11003 /* pat_match_auth() will need the user list */
11004 smp->ctx.a[0] = args->data.usr;
11005
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011006 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011007 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011008 smp->data.u.str.str = smp->strm->txn->auth.user;
11009 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011010
11011 return 1;
11012}
11013
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011014/* Try to find the next occurrence of a cookie name in a cookie header value.
11015 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11016 * the cookie value is returned into *value and *value_l, and the function
11017 * returns a pointer to the next pointer to search from if the value was found.
11018 * Otherwise if the cookie was not found, NULL is returned and neither value
11019 * nor value_l are touched. The input <hdr> string should first point to the
11020 * header's value, and the <hdr_end> pointer must point to the first character
11021 * not part of the value. <list> must be non-zero if value may represent a list
11022 * of values (cookie headers). This makes it faster to abort parsing when no
11023 * list is expected.
11024 */
David Carlier4686f792015-09-25 14:10:50 +010011025char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011026extract_cookie_value(char *hdr, const char *hdr_end,
11027 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011028 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011029{
11030 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11031 char *next;
11032
11033 /* we search at least a cookie name followed by an equal, and more
11034 * generally something like this :
11035 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11036 */
11037 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11038 /* Iterate through all cookies on this line */
11039
11040 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
11041 att_beg++;
11042
11043 /* find att_end : this is the first character after the last non
11044 * space before the equal. It may be equal to hdr_end.
11045 */
11046 equal = att_end = att_beg;
11047
11048 while (equal < hdr_end) {
11049 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11050 break;
11051 if (http_is_spht[(unsigned char)*equal++])
11052 continue;
11053 att_end = equal;
11054 }
11055
11056 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11057 * is between <att_beg> and <equal>, both may be identical.
11058 */
11059
11060 /* look for end of cookie if there is an equal sign */
11061 if (equal < hdr_end && *equal == '=') {
11062 /* look for the beginning of the value */
11063 val_beg = equal + 1;
11064 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
11065 val_beg++;
11066
11067 /* find the end of the value, respecting quotes */
11068 next = find_cookie_value_end(val_beg, hdr_end);
11069
11070 /* make val_end point to the first white space or delimitor after the value */
11071 val_end = next;
11072 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
11073 val_end--;
11074 } else {
11075 val_beg = val_end = next = equal;
11076 }
11077
11078 /* We have nothing to do with attributes beginning with '$'. However,
11079 * they will automatically be removed if a header before them is removed,
11080 * since they're supposed to be linked together.
11081 */
11082 if (*att_beg == '$')
11083 continue;
11084
11085 /* Ignore cookies with no equal sign */
11086 if (equal == next)
11087 continue;
11088
11089 /* Now we have the cookie name between att_beg and att_end, and
11090 * its value between val_beg and val_end.
11091 */
11092
11093 if (att_end - att_beg == cookie_name_l &&
11094 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11095 /* let's return this value and indicate where to go on from */
11096 *value = val_beg;
11097 *value_l = val_end - val_beg;
11098 return next + 1;
11099 }
11100
11101 /* Set-Cookie headers only have the name in the first attr=value part */
11102 if (!list)
11103 break;
11104 }
11105
11106 return NULL;
11107}
11108
William Lallemanda43ba4e2014-01-28 18:14:25 +010011109/* Fetch a captured HTTP request header. The index is the position of
11110 * the "capture" option in the configuration file
11111 */
11112static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011113smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011114{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011115 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011116 int idx;
11117
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011118 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011119 return 0;
11120
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011121 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011122
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011123 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 +010011124 return 0;
11125
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011126 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011127 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011128 smp->data.u.str.str = smp->strm->req_cap[idx];
11129 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011130
11131 return 1;
11132}
11133
11134/* Fetch a captured HTTP response header. The index is the position of
11135 * the "capture" option in the configuration file
11136 */
11137static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011138smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011139{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011140 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011141 int idx;
11142
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011143 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011144 return 0;
11145
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011146 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011147
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011148 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 +010011149 return 0;
11150
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011151 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011152 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011153 smp->data.u.str.str = smp->strm->res_cap[idx];
11154 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011155
11156 return 1;
11157}
11158
William Lallemand65ad6e12014-01-31 15:08:02 +010011159/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11160static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011161smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011162{
11163 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011164 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011165 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011166
Willy Tarreau15e91e12015-04-04 00:52:09 +020011167 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011168 return 0;
11169
William Lallemand96a77852014-02-05 00:30:02 +010011170 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011171
William Lallemand96a77852014-02-05 00:30:02 +010011172 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11173 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011174
William Lallemand96a77852014-02-05 00:30:02 +010011175 temp = get_trash_chunk();
11176 temp->str = txn->uri;
11177 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011178 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011179 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011180 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011181
11182 return 1;
11183
11184}
11185
11186/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11187static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011188smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011189{
11190 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011191 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011192 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011193
Willy Tarreau15e91e12015-04-04 00:52:09 +020011194 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011195 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011196
William Lallemand65ad6e12014-01-31 15:08:02 +010011197 ptr = txn->uri;
11198
11199 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11200 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011201
William Lallemand65ad6e12014-01-31 15:08:02 +010011202 if (!*ptr)
11203 return 0;
11204
11205 ptr++; /* skip the space */
11206
11207 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011208 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011209 if (!ptr)
11210 return 0;
11211 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11212 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011213
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011214 smp->data.u.str = *temp;
11215 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011216 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011217 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011218
11219 return 1;
11220}
11221
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011222/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11223 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11224 */
11225static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011226smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011227{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011228 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011229
Willy Tarreau15e91e12015-04-04 00:52:09 +020011230 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011231 return 0;
11232
11233 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011234 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011235 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011236 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011237
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011238 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011239 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011240 smp->flags = SMP_F_CONST;
11241 return 1;
11242
11243}
11244
11245/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11246 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11247 */
11248static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011249smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011250{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011251 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011252
Willy Tarreau15e91e12015-04-04 00:52:09 +020011253 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011254 return 0;
11255
11256 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011257 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011258 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011259 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011260
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011261 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011262 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011263 smp->flags = SMP_F_CONST;
11264 return 1;
11265
11266}
11267
William Lallemand65ad6e12014-01-31 15:08:02 +010011268
Willy Tarreaue333ec92012-04-16 16:26:40 +020011269/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011270 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011271 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011272 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011273 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011274 * Accepts exactly 1 argument of type string. If the input options indicate
11275 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011276 * The returned sample is of type CSTR. Can be used to parse cookies in other
11277 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011278 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011279int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011280{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011281 struct http_txn *txn;
11282 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011283 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011284 const struct http_msg *msg;
11285 const char *hdr_name;
11286 int hdr_name_len;
11287 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011288 int occ = 0;
11289 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011290
Willy Tarreau24e32d82012-04-23 23:55:44 +020011291 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011292 return 0;
11293
Willy Tarreaua890d072013-04-02 12:01:06 +020011294 if (!ctx) {
11295 /* first call */
11296 ctx = &static_hdr_ctx;
11297 ctx->idx = 0;
11298 smp->ctx.a[2] = ctx;
11299 }
11300
Willy Tarreaue333ec92012-04-16 16:26:40 +020011301 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011302
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011303 txn = smp->strm->txn;
11304 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011305
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011306 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011307 msg = &txn->req;
11308 hdr_name = "Cookie";
11309 hdr_name_len = 6;
11310 } else {
11311 msg = &txn->rsp;
11312 hdr_name = "Set-Cookie";
11313 hdr_name_len = 10;
11314 }
11315
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011316 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011317 /* no explicit occurrence and single fetch => last cookie by default */
11318 occ = -1;
11319
11320 /* OK so basically here, either we want only one value and it's the
11321 * last one, or we want to iterate over all of them and we fetch the
11322 * next one.
11323 */
11324
Willy Tarreau9b28e032012-10-12 23:49:43 +020011325 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011326 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011327 /* search for the header from the beginning, we must first initialize
11328 * the search parameters.
11329 */
Willy Tarreau37406352012-04-23 16:16:37 +020011330 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011331 ctx->idx = 0;
11332 }
11333
Willy Tarreau28376d62012-04-26 21:26:10 +020011334 smp->flags |= SMP_F_VOL_HDR;
11335
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011336 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011337 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11338 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011339 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11340 goto out;
11341
Willy Tarreau24e32d82012-04-23 23:55:44 +020011342 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011343 continue;
11344
Willy Tarreau37406352012-04-23 16:16:37 +020011345 smp->ctx.a[0] = ctx->line + ctx->val;
11346 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011347 }
11348
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011349 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011350 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011351 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011352 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011353 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011354 &smp->data.u.str.str,
11355 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011356 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011357 found = 1;
11358 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011359 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011360 smp->flags |= SMP_F_NOT_LAST;
11361 return 1;
11362 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011363 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011364 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011365 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011366 /* all cookie headers and values were scanned. If we're looking for the
11367 * last occurrence, we may return it now.
11368 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011369 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011370 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011371 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011372}
11373
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011374/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011375 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011376 * multiple cookies may be parsed on the same line. The returned sample is of
11377 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011378 */
11379static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011380smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011381{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011382 struct http_txn *txn;
11383 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011384 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011385 const struct http_msg *msg;
11386 const char *hdr_name;
11387 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011388 int cnt;
11389 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011390 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011391
Willy Tarreau24e32d82012-04-23 23:55:44 +020011392 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011393 return 0;
11394
Willy Tarreaue333ec92012-04-16 16:26:40 +020011395 CHECK_HTTP_MESSAGE_FIRST();
11396
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011397 txn = smp->strm->txn;
11398 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011399
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011400 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011401 msg = &txn->req;
11402 hdr_name = "Cookie";
11403 hdr_name_len = 6;
11404 } else {
11405 msg = &txn->rsp;
11406 hdr_name = "Set-Cookie";
11407 hdr_name_len = 10;
11408 }
11409
Willy Tarreau9b28e032012-10-12 23:49:43 +020011410 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011411 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011412 ctx.idx = 0;
11413 cnt = 0;
11414
11415 while (1) {
11416 /* Note: val_beg == NULL every time we need to fetch a new header */
11417 if (!val_beg) {
11418 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11419 break;
11420
Willy Tarreau24e32d82012-04-23 23:55:44 +020011421 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011422 continue;
11423
11424 val_beg = ctx.line + ctx.val;
11425 val_end = val_beg + ctx.vlen;
11426 }
11427
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011428 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011429 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011430 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011431 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011432 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011433 &smp->data.u.str.str,
11434 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011435 cnt++;
11436 }
11437 }
11438
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011439 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011440 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011441 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011442 return 1;
11443}
11444
Willy Tarreau51539362012-05-08 12:46:28 +020011445/* Fetch an cookie's integer value. The integer value is returned. It
11446 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11447 */
11448static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011449smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011450{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011451 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011452
11453 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011454 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011455 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011456 }
11457
11458 return ret;
11459}
11460
Willy Tarreau8797c062007-05-07 00:55:35 +020011461/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011462/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011463/************************************************************************/
11464
David Cournapeau16023ee2010-12-23 20:55:41 +090011465/*
11466 * Given a path string and its length, find the position of beginning of the
11467 * query string. Returns NULL if no query string is found in the path.
11468 *
11469 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11470 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011471 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011472 */
bedis4c75cca2012-10-05 08:38:24 +020011473static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011474{
11475 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011476
bedis4c75cca2012-10-05 08:38:24 +020011477 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011478 return p ? p + 1 : NULL;
11479}
11480
bedis4c75cca2012-10-05 08:38:24 +020011481static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011482{
bedis4c75cca2012-10-05 08:38:24 +020011483 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011484}
11485
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011486/* after increasing a pointer value, it can exceed the first buffer
11487 * size. This function transform the value of <ptr> according with
11488 * the expected position. <chunks> is an array of the one or two
11489 * avalaible chunks. The first value is the start of the first chunk,
11490 * the second value if the end+1 of the first chunks. The third value
11491 * is NULL or the start of the second chunk and the fourth value is
11492 * the end+1 of the second chunk. The function returns 1 if does a
11493 * wrap, else returns 0.
11494 */
11495static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11496{
11497 if (*ptr < chunks[1])
11498 return 0;
11499 if (!chunks[2])
11500 return 0;
11501 *ptr = chunks[2] + ( *ptr - chunks[1] );
11502 return 1;
11503}
11504
David Cournapeau16023ee2010-12-23 20:55:41 +090011505/*
11506 * Given a url parameter, find the starting position of the first occurence,
11507 * or NULL if the parameter is not found.
11508 *
11509 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11510 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011511 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011512 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011513 * or the second chunk. The caller must be check the position before using the
11514 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011515 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011516static const char *
11517find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011518 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011519 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011520{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011521 const char *pos, *last, *equal;
11522 const char **bufs = chunks;
11523 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011524
David Cournapeau16023ee2010-12-23 20:55:41 +090011525
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011526 pos = bufs[0];
11527 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011528 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011529 /* Check the equal. */
11530 equal = pos + url_param_name_l;
11531 if (fix_pointer_if_wrap(chunks, &equal)) {
11532 if (equal >= chunks[3])
11533 return NULL;
11534 } else {
11535 if (equal >= chunks[1])
11536 return NULL;
11537 }
11538 if (*equal == '=') {
11539 if (pos + url_param_name_l > last) {
11540 /* process wrap case, we detect a wrap. In this case, the
11541 * comparison is performed in two parts.
11542 */
11543
11544 /* This is the end, we dont have any other chunk. */
11545 if (bufs != chunks || !bufs[2])
11546 return NULL;
11547
11548 /* Compute the length of each part of the comparison. */
11549 l1 = last - pos;
11550 l2 = url_param_name_l - l1;
11551
11552 /* The second buffer is too short to contain the compared string. */
11553 if (bufs[2] + l2 > bufs[3])
11554 return NULL;
11555
11556 if (memcmp(pos, url_param_name, l1) == 0 &&
11557 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11558 return pos;
11559
11560 /* Perform wrapping and jump the string who fail the comparison. */
11561 bufs += 2;
11562 pos = bufs[0] + l2;
11563 last = bufs[1];
11564
11565 } else {
11566 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011567 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11568 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011569 pos += url_param_name_l + 1;
11570 if (fix_pointer_if_wrap(chunks, &pos))
11571 last = bufs[2];
11572 }
11573 }
11574
11575 while (1) {
11576 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011577 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011578 pos++;
11579 if (pos < last)
11580 break;
11581 /* process buffer wrapping. */
11582 if (bufs != chunks || !bufs[2])
11583 return NULL;
11584 bufs += 2;
11585 pos = bufs[0];
11586 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011587 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011588 pos++;
11589 }
11590 return NULL;
11591}
11592
11593/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011594 * Given a url parameter name and a query string, find the next value.
11595 * An empty url_param_name matches the first available parameter.
11596 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11597 * respectively provide a pointer to the value and its end.
11598 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011599 */
11600static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011601find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011602 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011603 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011604{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011605 const char *arg_start, *qs_end;
11606 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011607
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011608 arg_start = chunks[0];
11609 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011610 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011611 /* Looks for an argument name. */
11612 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011613 url_param_name, url_param_name_l,
11614 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011615 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011616 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011617 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011618 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011619 if (!arg_start)
11620 return 0;
11621
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011622 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011623 while (1) {
11624 /* looks for the first argument. */
11625 value_start = memchr(arg_start, '=', qs_end - arg_start);
11626 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011627 /* Check for wrapping. */
11628 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011629 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011630 chunks[2]) {
11631 arg_start = chunks[2];
11632 qs_end = chunks[3];
11633 continue;
11634 }
11635 return 0;
11636 }
11637 break;
11638 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011639 value_start++;
11640 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011641 else {
11642 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011643 value_start = arg_start + url_param_name_l + 1;
11644
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011645 /* Check for pointer wrapping. */
11646 if (fix_pointer_if_wrap(chunks, &value_start)) {
11647 /* Update the end pointer. */
11648 qs_end = chunks[3];
11649
11650 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011651 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011652 return 0;
11653 }
11654 }
11655
David Cournapeau16023ee2010-12-23 20:55:41 +090011656 value_end = value_start;
11657
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011658 while (1) {
11659 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11660 value_end++;
11661 if (value_end < qs_end)
11662 break;
11663 /* process buffer wrapping. */
11664 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011665 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011666 chunks[2]) {
11667 value_end = chunks[2];
11668 qs_end = chunks[3];
11669 continue;
11670 }
11671 break;
11672 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011673
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011674 *vstart = value_start;
11675 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011676 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011677}
11678
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011679/* This scans a URL-encoded query string. It takes an optionally wrapping
11680 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11681 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11682 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011683 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011684static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011685smp_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 +090011686{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011687 const char *vstart, *vend;
11688 struct chunk *temp;
11689 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011690
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011691 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011692 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011693 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011694 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011695 return 0;
11696
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011697 /* Create sample. If the value is contiguous, return the pointer as CONST,
11698 * if the value is wrapped, copy-it in a buffer.
11699 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011700 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011701 if (chunks[2] &&
11702 vstart >= chunks[0] && vstart <= chunks[1] &&
11703 vend >= chunks[2] && vend <= chunks[3]) {
11704 /* Wrapped case. */
11705 temp = get_trash_chunk();
11706 memcpy(temp->str, vstart, chunks[1] - vstart);
11707 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011708 smp->data.u.str.str = temp->str;
11709 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011710 } else {
11711 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011712 smp->data.u.str.str = (char *)vstart;
11713 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011714 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11715 }
11716
11717 /* Update context, check wrapping. */
11718 chunks[0] = vend;
11719 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11720 chunks[1] = chunks[3];
11721 chunks[2] = NULL;
11722 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011723
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011724 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011725 smp->flags |= SMP_F_NOT_LAST;
11726
David Cournapeau16023ee2010-12-23 20:55:41 +090011727 return 1;
11728}
11729
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011730/* This function iterates over each parameter of the query string. It uses
11731 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011732 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11733 * An optional parameter name is passed in args[0], otherwise any parameter is
11734 * considered. It supports an optional delimiter argument for the beginning of
11735 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011736 */
11737static int
11738smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11739{
11740 struct http_msg *msg;
11741 char delim = '?';
11742 const char *name;
11743 int name_len;
11744
Dragan Dosen26f77e52015-05-25 10:02:11 +020011745 if (!args ||
11746 (args[0].type && args[0].type != ARGT_STR) ||
11747 (args[1].type && args[1].type != ARGT_STR))
11748 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011749
Dragan Dosen26f77e52015-05-25 10:02:11 +020011750 name = "";
11751 name_len = 0;
11752 if (args->type == ARGT_STR) {
11753 name = args->data.str.str;
11754 name_len = args->data.str.len;
11755 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011756
Dragan Dosen26f77e52015-05-25 10:02:11 +020011757 if (args[1].type)
11758 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011759
Dragan Dosen26f77e52015-05-25 10:02:11 +020011760 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011761 CHECK_HTTP_MESSAGE_FIRST();
11762
11763 msg = &smp->strm->txn->req;
11764
11765 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11766 msg->sl.rq.u_l, delim);
11767 if (!smp->ctx.a[0])
11768 return 0;
11769
11770 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011771
11772 /* Assume that the context is filled with NULL pointer
11773 * before the first call.
11774 * smp->ctx.a[2] = NULL;
11775 * smp->ctx.a[3] = NULL;
11776 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011777 }
11778
11779 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11780}
11781
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011782/* This function iterates over each parameter of the body. This requires
11783 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011784 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11785 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11786 * optional second part if the body wraps at the end of the buffer. An optional
11787 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011788 */
11789static int
11790smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11791{
11792 struct http_txn *txn = smp->strm->txn;
11793 struct http_msg *msg;
11794 unsigned long len;
11795 unsigned long block1;
11796 char *body;
11797 const char *name;
11798 int name_len;
11799
11800 if (!args || (args[0].type && args[0].type != ARGT_STR))
11801 return 0;
11802
11803 name = "";
11804 name_len = 0;
11805 if (args[0].type == ARGT_STR) {
11806 name = args[0].data.str.str;
11807 name_len = args[0].data.str.len;
11808 }
11809
11810 if (!smp->ctx.a[0]) { // first call, find the query string
11811 CHECK_HTTP_MESSAGE_FIRST();
11812
11813 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
11814 msg = &txn->req;
11815 else
11816 msg = &txn->rsp;
11817
11818 len = http_body_bytes(msg);
11819 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11820
11821 block1 = len;
11822 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11823 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11824
11825 if (block1 == len) {
11826 /* buffer is not wrapped (or empty) */
11827 smp->ctx.a[0] = body;
11828 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011829
11830 /* Assume that the context is filled with NULL pointer
11831 * before the first call.
11832 * smp->ctx.a[2] = NULL;
11833 * smp->ctx.a[3] = NULL;
11834 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011835 }
11836 else {
11837 /* buffer is wrapped, we need to defragment it */
11838 smp->ctx.a[0] = body;
11839 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011840 smp->ctx.a[2] = msg->chn->buf->data;
11841 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011842 }
11843 }
11844 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11845}
11846
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011847/* Return the signed integer value for the specified url parameter (see url_param
11848 * above).
11849 */
11850static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011851smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011852{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011853 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011854
11855 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011856 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011857 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011858 }
11859
11860 return ret;
11861}
11862
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011863/* This produces a 32-bit hash of the concatenation of the first occurrence of
11864 * the Host header followed by the path component if it begins with a slash ('/').
11865 * This means that '*' will not be added, resulting in exactly the first Host
11866 * entry. If no Host header is found, then the path is used. The resulting value
11867 * is hashed using the url hash followed by a full avalanche hash and provides a
11868 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11869 * high-traffic sites without having to store whole paths.
11870 * this differs from the base32 functions in that it includes the url parameters
11871 * as well as the path
11872 */
11873static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011874smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011875{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011876 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011877 struct hdr_ctx ctx;
11878 unsigned int hash = 0;
11879 char *ptr, *beg, *end;
11880 int len;
11881
11882 CHECK_HTTP_MESSAGE_FIRST();
11883
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011884 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011885 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011886 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011887 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11888 ptr = ctx.line + ctx.val;
11889 len = ctx.vlen;
11890 while (len--)
11891 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11892 }
11893
11894 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011895 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 +000011896 beg = http_get_path(txn);
11897 if (!beg)
11898 beg = end;
11899
11900 for (ptr = beg; ptr < end ; ptr++);
11901
11902 if (beg < ptr && *beg == '/') {
11903 while (beg < ptr)
11904 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11905 }
11906 hash = full_hash(hash);
11907
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011908 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011909 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011910 smp->flags = SMP_F_VOL_1ST;
11911 return 1;
11912}
11913
11914/* This concatenates the source address with the 32-bit hash of the Host and
11915 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11916 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11917 * on the source address length. The URL hash is stored before the address so
11918 * that in environments where IPv6 is insignificant, truncating the output to
11919 * 8 bytes would still work.
11920 */
11921static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011922smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011923{
11924 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011925 struct connection *cli_conn = objt_conn(smp->sess->origin);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011926 unsigned int hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011927
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011928 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011929 return 0;
11930
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011931 /* The returned hash is a 32 bytes integer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011932 hash = smp->data.u.sint;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011933
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011934 temp = get_trash_chunk();
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011935 memcpy(temp->str + temp->len, &hash, sizeof(hash));
11936 temp->len += sizeof(hash);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011937
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011938 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011939 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011940 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011941 temp->len += 4;
11942 break;
11943 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011944 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011945 temp->len += 16;
11946 break;
11947 default:
11948 return 0;
11949 }
11950
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011951 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011952 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011953 return 1;
11954}
11955
Willy Tarreau185b5c42012-04-26 15:11:51 +020011956/* This function is used to validate the arguments passed to any "hdr" fetch
11957 * keyword. These keywords support an optional positive or negative occurrence
11958 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11959 * is assumed that the types are already the correct ones. Returns 0 on error,
11960 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11961 * error message in case of error, that the caller is responsible for freeing.
11962 * The initial location must either be freeable or NULL.
11963 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011964int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011965{
11966 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011967 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011968 return 0;
11969 }
11970 return 1;
11971}
11972
Willy Tarreau276fae92013-07-25 14:36:01 +020011973/* takes an UINT value on input supposed to represent the time since EPOCH,
11974 * adds an optional offset found in args[0] and emits a string representing
11975 * the date in RFC-1123/5322 format.
11976 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011977static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011978{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011979 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011980 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11981 struct chunk *temp;
11982 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011983 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011984 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011985
11986 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011987 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011988 curr_date += args[0].data.sint;
11989
11990 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011991 if (!tm)
11992 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011993
11994 temp = get_trash_chunk();
11995 temp->len = snprintf(temp->str, temp->size - temp->len,
11996 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11997 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11998 tm->tm_hour, tm->tm_min, tm->tm_sec);
11999
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012000 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012001 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020012002 return 1;
12003}
12004
Thierry FOURNIERad903512014-04-11 17:51:01 +020012005/* Match language range with language tag. RFC2616 14.4:
12006 *
12007 * A language-range matches a language-tag if it exactly equals
12008 * the tag, or if it exactly equals a prefix of the tag such
12009 * that the first tag character following the prefix is "-".
12010 *
12011 * Return 1 if the strings match, else return 0.
12012 */
12013static inline int language_range_match(const char *range, int range_len,
12014 const char *tag, int tag_len)
12015{
12016 const char *end = range + range_len;
12017 const char *tend = tag + tag_len;
12018 while (range < end) {
12019 if (*range == '-' && tag == tend)
12020 return 1;
12021 if (*range != *tag || tag == tend)
12022 return 0;
12023 range++;
12024 tag++;
12025 }
12026 /* Return true only if the last char of the tag is matched. */
12027 return tag == tend;
12028}
12029
12030/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012031static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012032{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012033 const char *al = smp->data.u.str.str;
12034 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012035 const char *token;
12036 int toklen;
12037 int qvalue;
12038 const char *str;
12039 const char *w;
12040 int best_q = 0;
12041
12042 /* Set the constant to the sample, because the output of the
12043 * function will be peek in the constant configuration string.
12044 */
12045 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012046 smp->data.u.str.size = 0;
12047 smp->data.u.str.str = "";
12048 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012049
12050 /* Parse the accept language */
12051 while (1) {
12052
12053 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012054 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012055 al++;
12056 if (al >= end)
12057 break;
12058
12059 /* Start of the fisrt word. */
12060 token = al;
12061
12062 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012063 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012064 al++;
12065 if (al == token)
12066 goto expect_comma;
12067
12068 /* Length of the token. */
12069 toklen = al - token;
12070 qvalue = 1000;
12071
12072 /* Check if the token exists in the list. If the token not exists,
12073 * jump to the next token.
12074 */
12075 str = args[0].data.str.str;
12076 w = str;
12077 while (1) {
12078 if (*str == ';' || *str == '\0') {
12079 if (language_range_match(token, toklen, w, str-w))
12080 goto look_for_q;
12081 if (*str == '\0')
12082 goto expect_comma;
12083 w = str + 1;
12084 }
12085 str++;
12086 }
12087 goto expect_comma;
12088
12089look_for_q:
12090
12091 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012092 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012093 al++;
12094 if (al >= end)
12095 goto process_value;
12096
12097 /* If ',' is found, process the result */
12098 if (*al == ',')
12099 goto process_value;
12100
12101 /* If the character is different from ';', look
12102 * for the end of the header part in best effort.
12103 */
12104 if (*al != ';')
12105 goto expect_comma;
12106
12107 /* Assumes that the char is ';', now expect "q=". */
12108 al++;
12109
12110 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012111 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012112 al++;
12113 if (al >= end)
12114 goto process_value;
12115
12116 /* Expect 'q'. If no 'q', continue in best effort */
12117 if (*al != 'q')
12118 goto process_value;
12119 al++;
12120
12121 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012122 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012123 al++;
12124 if (al >= end)
12125 goto process_value;
12126
12127 /* Expect '='. If no '=', continue in best effort */
12128 if (*al != '=')
12129 goto process_value;
12130 al++;
12131
12132 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012133 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012134 al++;
12135 if (al >= end)
12136 goto process_value;
12137
12138 /* Parse the q value. */
12139 qvalue = parse_qvalue(al, &al);
12140
12141process_value:
12142
12143 /* If the new q value is the best q value, then store the associated
12144 * language in the response. If qvalue is the biggest value (1000),
12145 * break the process.
12146 */
12147 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012148 smp->data.u.str.str = (char *)w;
12149 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012150 if (qvalue >= 1000)
12151 break;
12152 best_q = qvalue;
12153 }
12154
12155expect_comma:
12156
12157 /* Expect comma or end. If the end is detected, quit the loop. */
12158 while (al < end && *al != ',')
12159 al++;
12160 if (al >= end)
12161 break;
12162
12163 /* Comma is found, jump it and restart the analyzer. */
12164 al++;
12165 }
12166
12167 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012168 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12169 smp->data.u.str.str = args[1].data.str.str;
12170 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012171 }
12172
12173 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012174 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012175}
12176
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012177/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012178static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012179{
12180 /* If the constant flag is set or if not size is avalaible at
12181 * the end of the buffer, copy the string in other buffer
12182 * before decoding.
12183 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012184 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012185 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012186 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12187 smp->data.u.str.str = str->str;
12188 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012189 smp->flags &= ~SMP_F_CONST;
12190 }
12191
12192 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012193 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12194 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012195 return 1;
12196}
12197
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012198static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12199{
12200 struct proxy *fe = strm_fe(smp->strm);
12201 int idx, i;
12202 struct cap_hdr *hdr;
12203 int len;
12204
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012205 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012206 return 0;
12207
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012208 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012209
12210 /* Check the availibity of the capture id. */
12211 if (idx > fe->nb_req_cap - 1)
12212 return 0;
12213
12214 /* Look for the original configuration. */
12215 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12216 hdr != NULL && i != idx ;
12217 i--, hdr = hdr->next);
12218 if (!hdr)
12219 return 0;
12220
12221 /* check for the memory allocation */
12222 if (smp->strm->req_cap[hdr->index] == NULL)
12223 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12224 if (smp->strm->req_cap[hdr->index] == NULL)
12225 return 0;
12226
12227 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012228 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012229 if (len > hdr->len)
12230 len = hdr->len;
12231
12232 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012233 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012234 smp->strm->req_cap[idx][len] = '\0';
12235
12236 return 1;
12237}
12238
12239static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12240{
12241 struct proxy *fe = strm_fe(smp->strm);
12242 int idx, i;
12243 struct cap_hdr *hdr;
12244 int len;
12245
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012246 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012247 return 0;
12248
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012249 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012250
12251 /* Check the availibity of the capture id. */
12252 if (idx > fe->nb_rsp_cap - 1)
12253 return 0;
12254
12255 /* Look for the original configuration. */
12256 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12257 hdr != NULL && i != idx ;
12258 i--, hdr = hdr->next);
12259 if (!hdr)
12260 return 0;
12261
12262 /* check for the memory allocation */
12263 if (smp->strm->res_cap[hdr->index] == NULL)
12264 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12265 if (smp->strm->res_cap[hdr->index] == NULL)
12266 return 0;
12267
12268 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012269 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012270 if (len > hdr->len)
12271 len = hdr->len;
12272
12273 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012274 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012275 smp->strm->res_cap[idx][len] = '\0';
12276
12277 return 1;
12278}
12279
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012280/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012281 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012282 * the relevant part of the request line accordingly. Then it updates various
12283 * pointers to the next elements which were moved, and the total buffer length.
12284 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012285 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12286 * error, though this can be revisited when this code is finally exploited.
12287 *
12288 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12289 * query string and 3 to replace uri.
12290 *
12291 * In query string case, the mark question '?' must be set at the start of the
12292 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012293 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012294int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012295 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012296{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012297 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012298 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012299 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012300 int delta;
12301
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012302 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012303 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012304 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012305 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12306
12307 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012308 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012309 txn->req.sl.rq.m_l += delta;
12310 txn->req.sl.rq.u += delta;
12311 txn->req.sl.rq.v += delta;
12312 break;
12313
12314 case 1: // path
12315 cur_ptr = http_get_path(txn);
12316 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012317 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012318
12319 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012320 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 +010012321 cur_end++;
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 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012330 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012331 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012332 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12333 while (cur_ptr < cur_end && *cur_ptr != '?')
12334 cur_ptr++;
12335
12336 /* skip the question mark or indicate that we must insert it
12337 * (but only if the format string is not empty then).
12338 */
12339 if (cur_ptr < cur_end)
12340 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012341 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012342 offset = 0;
12343
12344 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012345 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012346 txn->req.sl.rq.u_l += delta;
12347 txn->req.sl.rq.v += delta;
12348 break;
12349
12350 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012351 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012352 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12353
12354 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012355 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012356 txn->req.sl.rq.u_l += delta;
12357 txn->req.sl.rq.v += delta;
12358 break;
12359
12360 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012361 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012362 }
12363
12364 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012365 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012366 txn->req.sl.rq.l += delta;
12367 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012368 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012369 return 0;
12370}
12371
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012372/* This function replace the HTTP status code and the associated message. The
12373 * variable <status> contains the new status code. This function never fails.
12374 */
12375void http_set_status(unsigned int status, struct stream *s)
12376{
12377 struct http_txn *txn = s->txn;
12378 char *cur_ptr, *cur_end;
12379 int delta;
12380 char *res;
12381 int c_l;
12382 const char *msg;
12383 int msg_len;
12384
12385 chunk_reset(&trash);
12386
12387 res = ultoa_o(status, trash.str, trash.size);
12388 c_l = res - trash.str;
12389
12390 trash.str[c_l] = ' ';
12391 trash.len = c_l + 1;
12392
12393 msg = get_reason(status);
12394 msg_len = strlen(msg);
12395
12396 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12397 trash.len += msg_len;
12398
12399 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12400 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12401
12402 /* commit changes and adjust message */
12403 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12404
12405 /* adjust res line offsets and lengths */
12406 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12407 txn->rsp.sl.st.c_l = c_l;
12408 txn->rsp.sl.st.r_l = msg_len;
12409
12410 delta = trash.len - (cur_end - cur_ptr);
12411 txn->rsp.sl.st.l += delta;
12412 txn->hdr_idx.v[0].len += delta;
12413 http_msg_move_end(&txn->rsp, delta);
12414}
12415
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012416/* This function executes one of the set-{method,path,query,uri} actions. It
12417 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012418 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012419 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012420 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12421 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012422 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012423enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012424 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012425{
12426 chunk_reset(&trash);
12427
12428 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012429 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012430 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012431 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012432
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012433 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012434 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012435}
12436
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012437/* This function is just a compliant action wrapper for "set-status". */
12438enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012439 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012440{
12441 http_set_status(rule->arg.status.code, s);
12442 return ACT_RET_CONT;
12443}
12444
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012445/* parse an http-request action among :
12446 * set-method
12447 * set-path
12448 * set-query
12449 * set-uri
12450 *
12451 * All of them accept a single argument of type string representing a log-format.
12452 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12453 * 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 +020012454 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012455 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012456enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12457 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012458{
12459 int cur_arg = *orig_arg;
12460
Thierry FOURNIER42148732015-09-02 17:17:33 +020012461 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012462
12463 switch (args[0][4]) {
12464 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012465 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012466 rule->action_ptr = http_action_set_req_line;
12467 break;
12468 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012469 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012470 rule->action_ptr = http_action_set_req_line;
12471 break;
12472 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012473 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012474 rule->action_ptr = http_action_set_req_line;
12475 break;
12476 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012477 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012478 rule->action_ptr = http_action_set_req_line;
12479 break;
12480 default:
12481 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012482 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012483 }
12484
12485 if (!*args[cur_arg] ||
12486 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12487 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012488 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012489 }
12490
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012491 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012492 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012493 parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012494 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12495 proxy->conf.args.file, proxy->conf.args.line);
12496
12497 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012498 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012499}
12500
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012501/* parse set-status action:
12502 * This action accepts a single argument of type int representing
12503 * an http status code. It returns ACT_RET_PRS_OK on success,
12504 * ACT_RET_PRS_ERR on error.
12505 */
12506enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12507 struct act_rule *rule, char **err)
12508{
12509 char *error;
12510
Thierry FOURNIER42148732015-09-02 17:17:33 +020012511 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012512 rule->action_ptr = action_http_set_status;
12513
12514 /* Check if an argument is available */
12515 if (!*args[*orig_arg]) {
12516 memprintf(err, "expects exactly 1 argument <status>");
12517 return ACT_RET_PRS_ERR;
12518 }
12519
12520 /* convert status code as integer */
12521 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12522 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12523 memprintf(err, "expects an integer status code between 100 and 999");
12524 return ACT_RET_PRS_ERR;
12525 }
12526
12527 (*orig_arg)++;
12528 return ACT_RET_PRS_OK;
12529}
12530
Willy Tarreaua9083d02015-05-08 15:27:59 +020012531/* This function executes the "capture" action. It executes a fetch expression,
12532 * turns the result into a string and puts it in a capture slot. It always
12533 * returns 1. If an error occurs the action is cancelled, but the rule
12534 * processing continues.
12535 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012536enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012537 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012538{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012539 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012540 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012541 char **cap = s->req_cap;
12542 int len;
12543
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012544 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 +020012545 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012546 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012547
12548 if (cap[h->index] == NULL)
12549 cap[h->index] = pool_alloc2(h->pool);
12550
12551 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012552 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012553
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012554 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012555 if (len > h->len)
12556 len = h->len;
12557
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012558 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012559 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012560 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012561}
12562
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012563/* This function executes the "capture" action and store the result in a
12564 * capture slot if exists. It executes a fetch expression, turns the result
12565 * into a string and puts it in a capture slot. It always returns 1. If an
12566 * error occurs the action is cancelled, but the rule processing continues.
12567 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012568enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012569 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012570{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012571 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012572 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012573 char **cap = s->req_cap;
12574 struct proxy *fe = strm_fe(s);
12575 int len;
12576 int i;
12577
12578 /* Look for the original configuration. */
12579 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012580 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012581 i--, h = h->next);
12582 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012583 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012584
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012585 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 +020012586 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012587 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012588
12589 if (cap[h->index] == NULL)
12590 cap[h->index] = pool_alloc2(h->pool);
12591
12592 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012593 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012594
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012595 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012596 if (len > h->len)
12597 len = h->len;
12598
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012599 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012600 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012601 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012602}
12603
Willy Tarreaua9083d02015-05-08 15:27:59 +020012604/* parse an "http-request capture" action. It takes a single argument which is
12605 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012606 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012607 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012608 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012609enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12610 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012611{
12612 struct sample_expr *expr;
12613 struct cap_hdr *hdr;
12614 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012615 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012616
12617 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12618 if (strcmp(args[cur_arg], "if") == 0 ||
12619 strcmp(args[cur_arg], "unless") == 0)
12620 break;
12621
12622 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012623 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012624 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012625 }
12626
Willy Tarreaua9083d02015-05-08 15:27:59 +020012627 cur_arg = *orig_arg;
12628 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12629 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012630 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012631
12632 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12633 memprintf(err,
12634 "fetch method '%s' extracts information from '%s', none of which is available here",
12635 args[cur_arg-1], sample_src_names(expr->fetch->use));
12636 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012637 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012638 }
12639
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012640 if (!args[cur_arg] || !*args[cur_arg]) {
12641 memprintf(err, "expects 'len or 'id'");
12642 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012643 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012644 }
12645
Willy Tarreaua9083d02015-05-08 15:27:59 +020012646 if (strcmp(args[cur_arg], "len") == 0) {
12647 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012648
12649 if (!(px->cap & PR_CAP_FE)) {
12650 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012651 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012652 }
12653
12654 proxy->conf.args.ctx = ARGC_CAP;
12655
Willy Tarreaua9083d02015-05-08 15:27:59 +020012656 if (!args[cur_arg]) {
12657 memprintf(err, "missing length value");
12658 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012659 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012660 }
12661 /* we copy the table name for now, it will be resolved later */
12662 len = atoi(args[cur_arg]);
12663 if (len <= 0) {
12664 memprintf(err, "length must be > 0");
12665 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012666 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012667 }
12668 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012669
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012670 if (!len) {
12671 memprintf(err, "a positive 'len' argument is mandatory");
12672 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012673 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012674 }
12675
12676 hdr = calloc(sizeof(struct cap_hdr), 1);
12677 hdr->next = px->req_cap;
12678 hdr->name = NULL; /* not a header capture */
12679 hdr->namelen = 0;
12680 hdr->len = len;
12681 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12682 hdr->index = px->nb_req_cap++;
12683
12684 px->req_cap = hdr;
12685 px->to_log |= LW_REQHDR;
12686
Thierry FOURNIER42148732015-09-02 17:17:33 +020012687 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012688 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012689 rule->arg.cap.expr = expr;
12690 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012691 }
12692
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012693 else if (strcmp(args[cur_arg], "id") == 0) {
12694 int id;
12695 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012696
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012697 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012698
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012699 if (!args[cur_arg]) {
12700 memprintf(err, "missing id value");
12701 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012702 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012703 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012704
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012705 id = strtol(args[cur_arg], &error, 10);
12706 if (*error != '\0') {
12707 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12708 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012709 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012710 }
12711 cur_arg++;
12712
12713 proxy->conf.args.ctx = ARGC_CAP;
12714
Thierry FOURNIER42148732015-09-02 17:17:33 +020012715 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012716 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012717 rule->arg.capid.expr = expr;
12718 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012719 }
12720
12721 else {
12722 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12723 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012724 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012725 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012726
12727 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012728 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012729}
12730
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012731/* This function executes the "capture" action and store the result in a
12732 * capture slot if exists. It executes a fetch expression, turns the result
12733 * into a string and puts it in a capture slot. It always returns 1. If an
12734 * error occurs the action is cancelled, but the rule processing continues.
12735 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012736enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012737 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012738{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012739 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012740 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012741 char **cap = s->res_cap;
12742 struct proxy *fe = strm_fe(s);
12743 int len;
12744 int i;
12745
12746 /* Look for the original configuration. */
12747 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012748 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012749 i--, h = h->next);
12750 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012751 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012752
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012753 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 +020012754 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012755 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012756
12757 if (cap[h->index] == NULL)
12758 cap[h->index] = pool_alloc2(h->pool);
12759
12760 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012761 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012762
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012763 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012764 if (len > h->len)
12765 len = h->len;
12766
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012767 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012768 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012769 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012770}
12771
12772/* parse an "http-response capture" action. It takes a single argument which is
12773 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12774 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012775 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012776 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012777enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12778 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012779{
12780 struct sample_expr *expr;
12781 int cur_arg;
12782 int id;
12783 char *error;
12784
12785 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12786 if (strcmp(args[cur_arg], "if") == 0 ||
12787 strcmp(args[cur_arg], "unless") == 0)
12788 break;
12789
12790 if (cur_arg < *orig_arg + 3) {
12791 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012792 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012793 }
12794
12795 cur_arg = *orig_arg;
12796 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12797 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012798 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012799
12800 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12801 memprintf(err,
12802 "fetch method '%s' extracts information from '%s', none of which is available here",
12803 args[cur_arg-1], sample_src_names(expr->fetch->use));
12804 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012805 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012806 }
12807
12808 if (!args[cur_arg] || !*args[cur_arg]) {
12809 memprintf(err, "expects 'len or 'id'");
12810 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012811 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012812 }
12813
12814 if (strcmp(args[cur_arg], "id") != 0) {
12815 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12816 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012817 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012818 }
12819
12820 cur_arg++;
12821
12822 if (!args[cur_arg]) {
12823 memprintf(err, "missing id value");
12824 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012825 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012826 }
12827
12828 id = strtol(args[cur_arg], &error, 10);
12829 if (*error != '\0') {
12830 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12831 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012832 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012833 }
12834 cur_arg++;
12835
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012836 proxy->conf.args.ctx = ARGC_CAP;
12837
Thierry FOURNIER42148732015-09-02 17:17:33 +020012838 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012839 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012840 rule->arg.capid.expr = expr;
12841 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012842
12843 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012844 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012845}
12846
William Lallemand73025dd2014-04-24 14:38:37 +020012847/*
12848 * Return the struct http_req_action_kw associated to a keyword.
12849 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012850struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012851{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012852 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012853}
12854
12855/*
12856 * Return the struct http_res_action_kw associated to a keyword.
12857 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012858struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012859{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012860 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012861}
12862
Willy Tarreau4a568972010-05-12 08:08:50 +020012863/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012864/* All supported ACL keywords must be declared here. */
12865/************************************************************************/
12866
12867/* Note: must not be declared <const> as its list will be overwritten.
12868 * Please take care of keeping this list alphabetically sorted.
12869 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012870static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012871 { "base", "base", PAT_MATCH_STR },
12872 { "base_beg", "base", PAT_MATCH_BEG },
12873 { "base_dir", "base", PAT_MATCH_DIR },
12874 { "base_dom", "base", PAT_MATCH_DOM },
12875 { "base_end", "base", PAT_MATCH_END },
12876 { "base_len", "base", PAT_MATCH_LEN },
12877 { "base_reg", "base", PAT_MATCH_REG },
12878 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012879
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012880 { "cook", "req.cook", PAT_MATCH_STR },
12881 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12882 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12883 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12884 { "cook_end", "req.cook", PAT_MATCH_END },
12885 { "cook_len", "req.cook", PAT_MATCH_LEN },
12886 { "cook_reg", "req.cook", PAT_MATCH_REG },
12887 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012888
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012889 { "hdr", "req.hdr", PAT_MATCH_STR },
12890 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12891 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12892 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12893 { "hdr_end", "req.hdr", PAT_MATCH_END },
12894 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12895 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12896 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012897
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012898 /* these two declarations uses strings with list storage (in place
12899 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12900 * and delete functions are relative to the list management. The parse
12901 * and match method are related to the corresponding fetch methods. This
12902 * is very particular ACL declaration mode.
12903 */
12904 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12905 { "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 +020012906
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012907 { "path", "path", PAT_MATCH_STR },
12908 { "path_beg", "path", PAT_MATCH_BEG },
12909 { "path_dir", "path", PAT_MATCH_DIR },
12910 { "path_dom", "path", PAT_MATCH_DOM },
12911 { "path_end", "path", PAT_MATCH_END },
12912 { "path_len", "path", PAT_MATCH_LEN },
12913 { "path_reg", "path", PAT_MATCH_REG },
12914 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012915
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012916 { "req_ver", "req.ver", PAT_MATCH_STR },
12917 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012918
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012919 { "scook", "res.cook", PAT_MATCH_STR },
12920 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12921 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12922 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12923 { "scook_end", "res.cook", PAT_MATCH_END },
12924 { "scook_len", "res.cook", PAT_MATCH_LEN },
12925 { "scook_reg", "res.cook", PAT_MATCH_REG },
12926 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012927
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012928 { "shdr", "res.hdr", PAT_MATCH_STR },
12929 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12930 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12931 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12932 { "shdr_end", "res.hdr", PAT_MATCH_END },
12933 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12934 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12935 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012936
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012937 { "url", "url", PAT_MATCH_STR },
12938 { "url_beg", "url", PAT_MATCH_BEG },
12939 { "url_dir", "url", PAT_MATCH_DIR },
12940 { "url_dom", "url", PAT_MATCH_DOM },
12941 { "url_end", "url", PAT_MATCH_END },
12942 { "url_len", "url", PAT_MATCH_LEN },
12943 { "url_reg", "url", PAT_MATCH_REG },
12944 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012945
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012946 { "urlp", "urlp", PAT_MATCH_STR },
12947 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12948 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12949 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12950 { "urlp_end", "urlp", PAT_MATCH_END },
12951 { "urlp_len", "urlp", PAT_MATCH_LEN },
12952 { "urlp_reg", "urlp", PAT_MATCH_REG },
12953 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012954
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012955 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012956}};
12957
12958/************************************************************************/
12959/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012960/************************************************************************/
12961/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012962static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012963 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012964 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012965 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12966
Willy Tarreau87b09662015-04-03 00:22:06 +020012967 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012968 { "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 +020012969
12970 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012971 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12972 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12973 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012974
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012975 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12976 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012977
Willy Tarreau409bcde2013-01-08 00:31:00 +010012978 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12979 * are only here to match the ACL's name, are request-only and are used
12980 * for ACL compatibility only.
12981 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012982 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12983 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012984 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12985 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012986
12987 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12988 * only here to match the ACL's name, are request-only and are used for
12989 * ACL compatibility only.
12990 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012991 { "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 +020012992 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012993 { "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 +020012994 { "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 +010012995
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012996 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012997 { "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 +010012998 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012999 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013000 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010013001 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013002
13003 /* HTTP protocol on the request path */
13004 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013005 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013006
13007 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013008 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
13009 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013010
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013011 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013012 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13013 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013014 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013015
Willy Tarreau18ed2562013-01-14 15:56:36 +010013016 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013017 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13018 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013019
Willy Tarreau18ed2562013-01-14 15:56:36 +010013020 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013021 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013022 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13023 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013024
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013025 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013026 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013027 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013028 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013029 { "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 +010013030 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013031 { "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 +010013032
13033 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013034 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013035 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13036 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013037
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013038 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013039 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013040 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013041 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013042 { "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 +010013043 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013044 { "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 +010013045
Willy Tarreau409bcde2013-01-08 00:31:00 +010013046 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013047 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013048 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13049 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013050 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013051
13052 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013053 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013054 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013055 { "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 +020013056 { "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 +010013057
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013058 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013059 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013060 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013061 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013062 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013063 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013064 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13065 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013066 { "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 +010013067 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013068}};
13069
Willy Tarreau8797c062007-05-07 00:55:35 +020013070
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013071/************************************************************************/
13072/* All supported converter keywords must be declared here. */
13073/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013074/* Note: must not be declared <const> as its list will be overwritten */
13075static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013076 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013077 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013078 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13079 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013080 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013081 { NULL, NULL, 0, 0, 0 },
13082}};
13083
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013084
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013085/************************************************************************/
13086/* All supported http-request action keywords must be declared here. */
13087/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013088struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013089 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013090 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013091 { "set-method", parse_set_req_line },
13092 { "set-path", parse_set_req_line },
13093 { "set-query", parse_set_req_line },
13094 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013095 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013096 }
13097};
13098
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013099struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013100 .kw = {
13101 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013102 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013103 { NULL, NULL }
13104 }
13105};
13106
Willy Tarreau8797c062007-05-07 00:55:35 +020013107__attribute__((constructor))
13108static void __http_protocol_init(void)
13109{
13110 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013111 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013112 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013113 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013114 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020013115}
13116
13117
Willy Tarreau58f10d72006-12-04 02:26:12 +010013118/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013119 * Local variables:
13120 * c-indent-level: 8
13121 * c-basic-offset: 8
13122 * End:
13123 */