blob: 8c476c7c03877764a750623c8274aab5d8e1d7f1 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreau8797c062007-05-07 00:55:35 +020044#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020045#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020064#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020068#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020069
Willy Tarreau522d6c02009-12-06 18:49:18 +010070const char HTTP_100[] =
71 "HTTP/1.1 100 Continue\r\n\r\n";
72
73const struct chunk http_100_chunk = {
74 .str = (char *)&HTTP_100,
75 .len = sizeof(HTTP_100)-1
76};
77
Willy Tarreaua9679ac2010-01-03 17:32:57 +010078/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020079const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010081 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020082 "Location: "; /* not terminated since it will be concatenated with the URL */
83
Willy Tarreau0f772532006-12-23 20:51:41 +010084const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010088 "Location: "; /* not terminated since it will be concatenated with the URL */
89
90/* same as 302 except that the browser MUST retry with the GET method */
91const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010094 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010095 "Location: "; /* not terminated since it will be concatenated with the URL */
96
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040097
98/* same as 302 except that the browser MUST retry with the same method */
99const char *HTTP_307 =
100 "HTTP/1.1 307 Temporary Redirect\r\n"
101 "Cache-Control: no-cache\r\n"
102 "Content-length: 0\r\n"
103 "Location: "; /* not terminated since it will be concatenated with the URL */
104
105/* same as 301 except that the browser MUST retry with the same method */
106const char *HTTP_308 =
107 "HTTP/1.1 308 Permanent Redirect\r\n"
108 "Content-length: 0\r\n"
109 "Location: "; /* not terminated since it will be concatenated with the URL */
110
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
112const char *HTTP_401_fmt =
113 "HTTP/1.0 401 Unauthorized\r\n"
114 "Cache-Control: no-cache\r\n"
115 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200116 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
118 "\r\n"
119 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
120
Willy Tarreau844a7e72010-01-31 21:46:18 +0100121const char *HTTP_407_fmt =
122 "HTTP/1.0 407 Unauthorized\r\n"
123 "Cache-Control: no-cache\r\n"
124 "Connection: close\r\n"
125 "Content-Type: text/html\r\n"
126 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
127 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800128 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100129
Willy Tarreau0f772532006-12-23 20:51:41 +0100130
131const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200132 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100133 [HTTP_ERR_400] = 400,
134 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400135 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100136 [HTTP_ERR_408] = 408,
CJ Ess108b1dd2015-04-07 12:03:37 -0400137 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 [HTTP_ERR_500] = 500,
139 [HTTP_ERR_502] = 502,
140 [HTTP_ERR_503] = 503,
141 [HTTP_ERR_504] = 504,
142};
143
Willy Tarreau80587432006-12-24 17:47:20 +0100144static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200145 [HTTP_ERR_200] =
146 "HTTP/1.0 200 OK\r\n"
147 "Cache-Control: no-cache\r\n"
148 "Connection: close\r\n"
149 "Content-Type: text/html\r\n"
150 "\r\n"
151 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
152
Willy Tarreau0f772532006-12-23 20:51:41 +0100153 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100154 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100155 "Cache-Control: no-cache\r\n"
156 "Connection: close\r\n"
157 "Content-Type: text/html\r\n"
158 "\r\n"
159 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
160
161 [HTTP_ERR_403] =
162 "HTTP/1.0 403 Forbidden\r\n"
163 "Cache-Control: no-cache\r\n"
164 "Connection: close\r\n"
165 "Content-Type: text/html\r\n"
166 "\r\n"
167 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
168
CJ Ess108b1dd2015-04-07 12:03:37 -0400169 [HTTP_ERR_405] =
170 "HTTP/1.0 405 Method Not Allowed\r\n"
171 "Cache-Control: no-cache\r\n"
172 "Connection: close\r\n"
173 "Content-Type: text/html\r\n"
174 "\r\n"
175 "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n",
176
Willy Tarreau0f772532006-12-23 20:51:41 +0100177 [HTTP_ERR_408] =
178 "HTTP/1.0 408 Request Time-out\r\n"
179 "Cache-Control: no-cache\r\n"
180 "Connection: close\r\n"
181 "Content-Type: text/html\r\n"
182 "\r\n"
183 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
184
CJ Ess108b1dd2015-04-07 12:03:37 -0400185 [HTTP_ERR_429] =
186 "HTTP/1.0 429 Too Many Requests\r\n"
187 "Cache-Control: no-cache\r\n"
188 "Connection: close\r\n"
189 "Content-Type: text/html\r\n"
190 "\r\n"
191 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
192
Willy Tarreau0f772532006-12-23 20:51:41 +0100193 [HTTP_ERR_500] =
194 "HTTP/1.0 500 Server Error\r\n"
195 "Cache-Control: no-cache\r\n"
196 "Connection: close\r\n"
197 "Content-Type: text/html\r\n"
198 "\r\n"
199 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
200
201 [HTTP_ERR_502] =
202 "HTTP/1.0 502 Bad Gateway\r\n"
203 "Cache-Control: no-cache\r\n"
204 "Connection: close\r\n"
205 "Content-Type: text/html\r\n"
206 "\r\n"
207 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
208
209 [HTTP_ERR_503] =
210 "HTTP/1.0 503 Service Unavailable\r\n"
211 "Cache-Control: no-cache\r\n"
212 "Connection: close\r\n"
213 "Content-Type: text/html\r\n"
214 "\r\n"
215 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
216
217 [HTTP_ERR_504] =
218 "HTTP/1.0 504 Gateway Time-out\r\n"
219 "Cache-Control: no-cache\r\n"
220 "Connection: close\r\n"
221 "Content-Type: text/html\r\n"
222 "\r\n"
223 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
224
225};
226
Cyril Bonté19979e12012-04-04 12:57:21 +0200227/* status codes available for the stats admin page (strictly 4 chars length) */
228const char *stat_status_codes[STAT_STATUS_SIZE] = {
229 [STAT_STATUS_DENY] = "DENY",
230 [STAT_STATUS_DONE] = "DONE",
231 [STAT_STATUS_ERRP] = "ERRP",
232 [STAT_STATUS_EXCD] = "EXCD",
233 [STAT_STATUS_NONE] = "NONE",
234 [STAT_STATUS_PART] = "PART",
235 [STAT_STATUS_UNKN] = "UNKN",
236};
237
238
William Lallemand73025dd2014-04-24 14:38:37 +0200239/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200240struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200241 .list = LIST_HEAD_INIT(http_req_keywords.list)
242};
243
244/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200245struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200246 .list = LIST_HEAD_INIT(http_res_keywords.list)
247};
248
Willy Tarreau80587432006-12-24 17:47:20 +0100249/* We must put the messages here since GCC cannot initialize consts depending
250 * on strlen().
251 */
252struct chunk http_err_chunks[HTTP_ERR_SIZE];
253
Willy Tarreaua890d072013-04-02 12:01:06 +0200254/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
255static struct hdr_ctx static_hdr_ctx;
256
Willy Tarreau42250582007-04-01 01:30:43 +0200257#define FD_SETS_ARE_BITFIELDS
258#ifdef FD_SETS_ARE_BITFIELDS
259/*
260 * This map is used with all the FD_* macros to check whether a particular bit
261 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
262 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
263 * byte should be encoded. Be careful to always pass bytes from 0 to 255
264 * exclusively to the macros.
265 */
266fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
267fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100268fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200269
270#else
271#error "Check if your OS uses bitfields for fd_sets"
272#endif
273
Willy Tarreau87b09662015-04-03 00:22:06 +0200274static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200275
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200276/* This function returns a reason associated with the HTTP status.
277 * This function never fails, a message is always returned.
278 */
279const char *get_reason(unsigned int status)
280{
281 switch (status) {
282 case 100: return "Continue";
283 case 101: return "Switching Protocols";
284 case 102: return "Processing";
285 case 200: return "OK";
286 case 201: return "Created";
287 case 202: return "Accepted";
288 case 203: return "Non-Authoritative Information";
289 case 204: return "No Content";
290 case 205: return "Reset Content";
291 case 206: return "Partial Content";
292 case 207: return "Multi-Status";
293 case 210: return "Content Different";
294 case 226: return "IM Used";
295 case 300: return "Multiple Choices";
296 case 301: return "Moved Permanently";
297 case 302: return "Moved Temporarily";
298 case 303: return "See Other";
299 case 304: return "Not Modified";
300 case 305: return "Use Proxy";
301 case 307: return "Temporary Redirect";
302 case 308: return "Permanent Redirect";
303 case 310: return "Too many Redirects";
304 case 400: return "Bad Request";
305 case 401: return "Unauthorized";
306 case 402: return "Payment Required";
307 case 403: return "Forbidden";
308 case 404: return "Not Found";
309 case 405: return "Method Not Allowed";
310 case 406: return "Not Acceptable";
311 case 407: return "Proxy Authentication Required";
312 case 408: return "Request Time-out";
313 case 409: return "Conflict";
314 case 410: return "Gone";
315 case 411: return "Length Required";
316 case 412: return "Precondition Failed";
317 case 413: return "Request Entity Too Large";
318 case 414: return "Request-URI Too Long";
319 case 415: return "Unsupported Media Type";
320 case 416: return "Requested range unsatisfiable";
321 case 417: return "Expectation failed";
322 case 418: return "I'm a teapot";
323 case 422: return "Unprocessable entity";
324 case 423: return "Locked";
325 case 424: return "Method failure";
326 case 425: return "Unordered Collection";
327 case 426: return "Upgrade Required";
328 case 428: return "Precondition Required";
329 case 429: return "Too Many Requests";
330 case 431: return "Request Header Fields Too Large";
331 case 449: return "Retry With";
332 case 450: return "Blocked by Windows Parental Controls";
333 case 451: return "Unavailable For Legal Reasons";
334 case 456: return "Unrecoverable Error";
335 case 499: return "client has closed connection";
336 case 500: return "Internal Server Error";
337 case 501: return "Not Implemented";
338 case 502: return "Bad Gateway ou Proxy Error";
339 case 503: return "Service Unavailable";
340 case 504: return "Gateway Time-out";
341 case 505: return "HTTP Version not supported";
342 case 506: return "Variant also negociate";
343 case 507: return "Insufficient storage";
344 case 508: return "Loop detected";
345 case 509: return "Bandwidth Limit Exceeded";
346 case 510: return "Not extended";
347 case 511: return "Network authentication required";
348 case 520: return "Web server is returning an unknown error";
349 default:
350 switch (status) {
351 case 100 ... 199: return "Informational";
352 case 200 ... 299: return "Success";
353 case 300 ... 399: return "Redirection";
354 case 400 ... 499: return "Client Error";
355 case 500 ... 599: return "Server Error";
356 default: return "Other";
357 }
358 }
359}
360
Willy Tarreau80587432006-12-24 17:47:20 +0100361void init_proto_http()
362{
Willy Tarreau42250582007-04-01 01:30:43 +0200363 int i;
364 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100365 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200366
Willy Tarreau80587432006-12-24 17:47:20 +0100367 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
368 if (!http_err_msgs[msg]) {
369 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
370 abort();
371 }
372
373 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
374 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
375 }
Willy Tarreau42250582007-04-01 01:30:43 +0200376
377 /* initialize the log header encoding map : '{|}"#' should be encoded with
378 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
379 * URL encoding only requires '"', '#' to be encoded as well as non-
380 * printable characters above.
381 */
382 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
383 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100384 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200385 for (i = 0; i < 32; i++) {
386 FD_SET(i, hdr_encode_map);
387 FD_SET(i, url_encode_map);
388 }
389 for (i = 127; i < 256; i++) {
390 FD_SET(i, hdr_encode_map);
391 FD_SET(i, url_encode_map);
392 }
393
394 tmp = "\"#{|}";
395 while (*tmp) {
396 FD_SET(*tmp, hdr_encode_map);
397 tmp++;
398 }
399
400 tmp = "\"#";
401 while (*tmp) {
402 FD_SET(*tmp, url_encode_map);
403 tmp++;
404 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200405
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100406 /* initialize the http header encoding map. The draft httpbis define the
407 * header content as:
408 *
409 * HTTP-message = start-line
410 * *( header-field CRLF )
411 * CRLF
412 * [ message-body ]
413 * header-field = field-name ":" OWS field-value OWS
414 * field-value = *( field-content / obs-fold )
415 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
416 * obs-fold = CRLF 1*( SP / HTAB )
417 * field-vchar = VCHAR / obs-text
418 * VCHAR = %x21-7E
419 * obs-text = %x80-FF
420 *
421 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
422 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
423 * "obs-fold" is volontary forgotten because haproxy remove this.
424 */
425 memset(http_encode_map, 0, sizeof(http_encode_map));
426 for (i = 0x00; i <= 0x08; i++)
427 FD_SET(i, http_encode_map);
428 for (i = 0x0a; i <= 0x1f; i++)
429 FD_SET(i, http_encode_map);
430 FD_SET(0x7f, http_encode_map);
431
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200432 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200433 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200434 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100435 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100436}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200437
Willy Tarreau53b6c742006-12-17 13:37:46 +0100438/*
439 * We have 26 list of methods (1 per first letter), each of which can have
440 * up to 3 entries (2 valid, 1 null).
441 */
442struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100443 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100444 int len;
445 const char text[8];
446};
447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100448const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100449 ['C' - 'A'] = {
450 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
451 },
452 ['D' - 'A'] = {
453 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
454 },
455 ['G' - 'A'] = {
456 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
457 },
458 ['H' - 'A'] = {
459 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
460 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200461 ['O' - 'A'] = {
462 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
463 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100464 ['P' - 'A'] = {
465 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
466 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
467 },
468 ['T' - 'A'] = {
469 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
470 },
471 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200472 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100473 */
474};
475
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100476const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100477 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
478 [HTTP_METH_GET] = { "GET", 3 },
479 [HTTP_METH_HEAD] = { "HEAD", 4 },
480 [HTTP_METH_POST] = { "POST", 4 },
481 [HTTP_METH_PUT] = { "PUT", 3 },
482 [HTTP_METH_DELETE] = { "DELETE", 6 },
483 [HTTP_METH_TRACE] = { "TRACE", 5 },
484 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
485};
486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100487/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200488 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100489 * RFC2616 for those chars.
490 */
491
492const char http_is_spht[256] = {
493 [' '] = 1, ['\t'] = 1,
494};
495
496const char http_is_crlf[256] = {
497 ['\r'] = 1, ['\n'] = 1,
498};
499
500const char http_is_lws[256] = {
501 [' '] = 1, ['\t'] = 1,
502 ['\r'] = 1, ['\n'] = 1,
503};
504
505const char http_is_sep[256] = {
506 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
507 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
508 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
509 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
510 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
511};
512
513const char http_is_ctl[256] = {
514 [0 ... 31] = 1,
515 [127] = 1,
516};
517
518/*
519 * A token is any ASCII char that is neither a separator nor a CTL char.
520 * Do not overwrite values in assignment since gcc-2.95 will not handle
521 * them correctly. Instead, define every non-CTL char's status.
522 */
523const char http_is_token[256] = {
524 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
525 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
526 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
527 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
528 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
529 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
530 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
531 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
532 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
533 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
534 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
535 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
536 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
537 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
538 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
539 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
540 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
541 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
542 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
543 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
544 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
545 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
546 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
547 ['|'] = 1, ['}'] = 0, ['~'] = 1,
548};
549
550
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100551/*
552 * An http ver_token is any ASCII which can be found in an HTTP version,
553 * which includes 'H', 'T', 'P', '/', '.' and any digit.
554 */
555const char http_is_ver_token[256] = {
556 ['.'] = 1, ['/'] = 1,
557 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
558 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
Thierry FOURNIER63d692c2015-02-28 19:03:56 +0100559 ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1,
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100560};
561
562
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100563/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100564 * Adds a header and its CRLF at the tail of the message's buffer, just before
565 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100566 * The header is also automatically added to the index <hdr_idx>, and the end
567 * of headers is automatically adjusted. The number of bytes added is returned
568 * on success, otherwise <0 is returned indicating an error.
569 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100570int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100571{
572 int bytes, len;
573
574 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200575 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100576 if (!bytes)
577 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100578 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100579 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
580}
581
582/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100583 * Adds a header and its CRLF at the tail of the message's buffer, just before
584 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100585 * the buffer is only opened and the space reserved, but nothing is copied.
586 * The header is also automatically added to the index <hdr_idx>, and the end
587 * of headers is automatically adjusted. The number of bytes added is returned
588 * on success, otherwise <0 is returned indicating an error.
589 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100590int http_header_add_tail2(struct http_msg *msg,
591 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100592{
593 int bytes;
594
Willy Tarreau9b28e032012-10-12 23:49:43 +0200595 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100596 if (!bytes)
597 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100598 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100599 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
600}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200601
602/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100603 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
604 * If so, returns the position of the first non-space character relative to
605 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
606 * to return a pointer to the place after the first space. Returns 0 if the
607 * header name does not match. Checks are case-insensitive.
608 */
609int http_header_match2(const char *hdr, const char *end,
610 const char *name, int len)
611{
612 const char *val;
613
614 if (hdr + len >= end)
615 return 0;
616 if (hdr[len] != ':')
617 return 0;
618 if (strncasecmp(hdr, name, len) != 0)
619 return 0;
620 val = hdr + len + 1;
621 while (val < end && HTTP_IS_SPHT(*val))
622 val++;
623 if ((val >= end) && (len + 2 <= end - hdr))
624 return len + 2; /* we may replace starting from second space */
625 return val - hdr;
626}
627
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200628/* Find the first or next occurrence of header <name> in message buffer <sol>
629 * using headers index <idx>, and return it in the <ctx> structure. This
630 * structure holds everything necessary to use the header and find next
631 * occurrence. If its <idx> member is 0, the header is searched from the
632 * beginning. Otherwise, the next occurrence is returned. The function returns
633 * 1 when it finds a value, and 0 when there is no more. It is very similar to
634 * http_find_header2() except that it is designed to work with full-line headers
635 * whose comma is not a delimiter but is part of the syntax. As a special case,
636 * if ctx->val is NULL when searching for a new values of a header, the current
637 * header is rescanned. This allows rescanning after a header deletion.
638 */
639int http_find_full_header2(const char *name, int len,
640 char *sol, struct hdr_idx *idx,
641 struct hdr_ctx *ctx)
642{
643 char *eol, *sov;
644 int cur_idx, old_idx;
645
646 cur_idx = ctx->idx;
647 if (cur_idx) {
648 /* We have previously returned a header, let's search another one */
649 sol = ctx->line;
650 eol = sol + idx->v[cur_idx].len;
651 goto next_hdr;
652 }
653
654 /* first request for this header */
655 sol += hdr_idx_first_pos(idx);
656 old_idx = 0;
657 cur_idx = hdr_idx_first_idx(idx);
658 while (cur_idx) {
659 eol = sol + idx->v[cur_idx].len;
660
661 if (len == 0) {
662 /* No argument was passed, we want any header.
663 * To achieve this, we simply build a fake request. */
664 while (sol + len < eol && sol[len] != ':')
665 len++;
666 name = sol;
667 }
668
669 if ((len < eol - sol) &&
670 (sol[len] == ':') &&
671 (strncasecmp(sol, name, len) == 0)) {
672 ctx->del = len;
673 sov = sol + len + 1;
674 while (sov < eol && http_is_lws[(unsigned char)*sov])
675 sov++;
676
677 ctx->line = sol;
678 ctx->prev = old_idx;
679 ctx->idx = cur_idx;
680 ctx->val = sov - sol;
681 ctx->tws = 0;
682 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
683 eol--;
684 ctx->tws++;
685 }
686 ctx->vlen = eol - sov;
687 return 1;
688 }
689 next_hdr:
690 sol = eol + idx->v[cur_idx].cr + 1;
691 old_idx = cur_idx;
692 cur_idx = idx->v[cur_idx].next;
693 }
694 return 0;
695}
696
Willy Tarreauc90dc232015-02-20 13:51:36 +0100697/* Find the first or next header field in message buffer <sol> using headers
698 * index <idx>, and return it in the <ctx> structure. This structure holds
699 * everything necessary to use the header and find next occurrence. If its
700 * <idx> member is 0, the first header is retrieved. Otherwise, the next
701 * occurrence is returned. The function returns 1 when it finds a value, and
702 * 0 when there is no more. It is equivalent to http_find_full_header2() with
703 * no header name.
704 */
705int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
706{
707 char *eol, *sov;
708 int cur_idx, old_idx;
709 int len;
710
711 cur_idx = ctx->idx;
712 if (cur_idx) {
713 /* We have previously returned a header, let's search another one */
714 sol = ctx->line;
715 eol = sol + idx->v[cur_idx].len;
716 goto next_hdr;
717 }
718
719 /* first request for this header */
720 sol += hdr_idx_first_pos(idx);
721 old_idx = 0;
722 cur_idx = hdr_idx_first_idx(idx);
723 while (cur_idx) {
724 eol = sol + idx->v[cur_idx].len;
725
726 len = 0;
727 while (1) {
728 if (len >= eol - sol)
729 goto next_hdr;
730 if (sol[len] == ':')
731 break;
732 len++;
733 }
734
735 ctx->del = len;
736 sov = sol + len + 1;
737 while (sov < eol && http_is_lws[(unsigned char)*sov])
738 sov++;
739
740 ctx->line = sol;
741 ctx->prev = old_idx;
742 ctx->idx = cur_idx;
743 ctx->val = sov - sol;
744 ctx->tws = 0;
745
746 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
747 eol--;
748 ctx->tws++;
749 }
750 ctx->vlen = eol - sov;
751 return 1;
752
753 next_hdr:
754 sol = eol + idx->v[cur_idx].cr + 1;
755 old_idx = cur_idx;
756 cur_idx = idx->v[cur_idx].next;
757 }
758 return 0;
759}
760
Willy Tarreau68085d82010-01-18 14:54:04 +0100761/* Find the end of the header value contained between <s> and <e>. See RFC2616,
762 * par 2.2 for more information. Note that it requires a valid header to return
763 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200764 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100765char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200766{
767 int quoted, qdpair;
768
769 quoted = qdpair = 0;
770 for (; s < e; s++) {
771 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200772 else if (quoted) {
773 if (*s == '\\') qdpair = 1;
774 else if (*s == '"') quoted = 0;
775 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200776 else if (*s == '"') quoted = 1;
777 else if (*s == ',') return s;
778 }
779 return s;
780}
781
782/* Find the first or next occurrence of header <name> in message buffer <sol>
783 * using headers index <idx>, and return it in the <ctx> structure. This
784 * structure holds everything necessary to use the header and find next
785 * occurrence. If its <idx> member is 0, the header is searched from the
786 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100787 * 1 when it finds a value, and 0 when there is no more. It is designed to work
788 * with headers defined as comma-separated lists. As a special case, if ctx->val
789 * is NULL when searching for a new values of a header, the current header is
790 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200791 */
792int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100793 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200794 struct hdr_ctx *ctx)
795{
Willy Tarreau68085d82010-01-18 14:54:04 +0100796 char *eol, *sov;
797 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200798
Willy Tarreau68085d82010-01-18 14:54:04 +0100799 cur_idx = ctx->idx;
800 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200801 /* We have previously returned a value, let's search
802 * another one on the same line.
803 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200804 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200805 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100806 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200807 eol = sol + idx->v[cur_idx].len;
808
809 if (sov >= eol)
810 /* no more values in this header */
811 goto next_hdr;
812
Willy Tarreau68085d82010-01-18 14:54:04 +0100813 /* values remaining for this header, skip the comma but save it
814 * for later use (eg: for header deletion).
815 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200816 sov++;
817 while (sov < eol && http_is_lws[(unsigned char)*sov])
818 sov++;
819
820 goto return_hdr;
821 }
822
823 /* first request for this header */
824 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100825 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200826 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200827 while (cur_idx) {
828 eol = sol + idx->v[cur_idx].len;
829
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200830 if (len == 0) {
831 /* No argument was passed, we want any header.
832 * To achieve this, we simply build a fake request. */
833 while (sol + len < eol && sol[len] != ':')
834 len++;
835 name = sol;
836 }
837
Willy Tarreau33a7e692007-06-10 19:45:56 +0200838 if ((len < eol - sol) &&
839 (sol[len] == ':') &&
840 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100841 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200842 sov = sol + len + 1;
843 while (sov < eol && http_is_lws[(unsigned char)*sov])
844 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100845
Willy Tarreau33a7e692007-06-10 19:45:56 +0200846 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100847 ctx->prev = old_idx;
848 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200849 ctx->idx = cur_idx;
850 ctx->val = sov - sol;
851
852 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200853 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200854 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200855 eol--;
856 ctx->tws++;
857 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200858 ctx->vlen = eol - sov;
859 return 1;
860 }
861 next_hdr:
862 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100863 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200864 cur_idx = idx->v[cur_idx].next;
865 }
866 return 0;
867}
868
869int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100870 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200871 struct hdr_ctx *ctx)
872{
873 return http_find_header2(name, strlen(name), sol, idx, ctx);
874}
875
Willy Tarreau68085d82010-01-18 14:54:04 +0100876/* Remove one value of a header. This only works on a <ctx> returned by one of
877 * the http_find_header functions. The value is removed, as well as surrounding
878 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100879 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100880 * message <msg>. The new index is returned. If it is zero, it means there is
881 * no more header, so any processing may stop. The ctx is always left in a form
882 * that can be handled by http_find_header2() to find next occurrence.
883 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100884int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100885{
886 int cur_idx = ctx->idx;
887 char *sol = ctx->line;
888 struct hdr_idx_elem *hdr;
889 int delta, skip_comma;
890
891 if (!cur_idx)
892 return 0;
893
894 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200895 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100896 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200897 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100898 http_msg_move_end(msg, delta);
899 idx->used--;
900 hdr->len = 0; /* unused entry */
901 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100902 if (idx->tail == ctx->idx)
903 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100904 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100905 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100906 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200907 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100908 return ctx->idx;
909 }
910
911 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200912 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
913 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100914 */
915
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200916 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200917 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200918 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100919 NULL, 0);
920 hdr->len += delta;
921 http_msg_move_end(msg, delta);
922 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200923 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100924 return ctx->idx;
925}
926
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100927/* This function handles a server error at the stream interface level. The
928 * stream interface is assumed to be already in a closed state. An optional
929 * message is copied into the input buffer, and an HTTP status code stored.
930 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100931 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200933static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100934 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200935{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100936 channel_auto_read(si_oc(si));
937 channel_abort(si_oc(si));
938 channel_auto_close(si_oc(si));
939 channel_erase(si_oc(si));
940 channel_auto_close(si_ic(si));
941 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100942 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200943 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100944 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200945 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200946 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200947 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200948 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200949 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200950}
951
Willy Tarreau87b09662015-04-03 00:22:06 +0200952/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100953 * and message.
954 */
955
Willy Tarreau87b09662015-04-03 00:22:06 +0200956struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100957{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200958 if (s->be->errmsg[msgnum].str)
959 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200960 else if (strm_fe(s)->errmsg[msgnum].str)
961 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100962 else
963 return &http_err_chunks[msgnum];
964}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200965
Willy Tarreau53b6c742006-12-17 13:37:46 +0100966/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200967 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
968 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100969 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100970enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100971{
972 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100973 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100974
975 m = ((unsigned)*str - 'A');
976
977 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100978 for (h = http_methods[m]; h->len > 0; h++) {
979 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100980 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100981 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100982 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100983 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100984 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200985 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100986}
987
Willy Tarreau21d2af32008-02-14 20:25:24 +0100988/* Parse the URI from the given transaction (which is assumed to be in request
989 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
990 * It is returned otherwise.
991 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +0200992char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100993{
994 char *ptr, *end;
995
Willy Tarreau9b28e032012-10-12 23:49:43 +0200996 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100997 end = ptr + txn->req.sl.rq.u_l;
998
999 if (ptr >= end)
1000 return NULL;
1001
1002 /* RFC2616, par. 5.1.2 :
1003 * Request-URI = "*" | absuri | abspath | authority
1004 */
1005
1006 if (*ptr == '*')
1007 return NULL;
1008
1009 if (isalpha((unsigned char)*ptr)) {
1010 /* this is a scheme as described by RFC3986, par. 3.1 */
1011 ptr++;
1012 while (ptr < end &&
1013 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1014 ptr++;
1015 /* skip '://' */
1016 if (ptr == end || *ptr++ != ':')
1017 return NULL;
1018 if (ptr == end || *ptr++ != '/')
1019 return NULL;
1020 if (ptr == end || *ptr++ != '/')
1021 return NULL;
1022 }
1023 /* skip [user[:passwd]@]host[:[port]] */
1024
1025 while (ptr < end && *ptr != '/')
1026 ptr++;
1027
1028 if (ptr == end)
1029 return NULL;
1030
1031 /* OK, we got the '/' ! */
1032 return ptr;
1033}
1034
William Lallemand65ad6e12014-01-31 15:08:02 +01001035/* Parse the URI from the given string and look for the "/" beginning the PATH.
1036 * If not found, return NULL. It is returned otherwise.
1037 */
1038static char *
1039http_get_path_from_string(char *str)
1040{
1041 char *ptr = str;
1042
1043 /* RFC2616, par. 5.1.2 :
1044 * Request-URI = "*" | absuri | abspath | authority
1045 */
1046
1047 if (*ptr == '*')
1048 return NULL;
1049
1050 if (isalpha((unsigned char)*ptr)) {
1051 /* this is a scheme as described by RFC3986, par. 3.1 */
1052 ptr++;
1053 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1054 ptr++;
1055 /* skip '://' */
1056 if (*ptr == '\0' || *ptr++ != ':')
1057 return NULL;
1058 if (*ptr == '\0' || *ptr++ != '/')
1059 return NULL;
1060 if (*ptr == '\0' || *ptr++ != '/')
1061 return NULL;
1062 }
1063 /* skip [user[:passwd]@]host[:[port]] */
1064
1065 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1066 ptr++;
1067
1068 if (*ptr == '\0' || *ptr == ' ')
1069 return NULL;
1070
1071 /* OK, we got the '/' ! */
1072 return ptr;
1073}
1074
Willy Tarreau71241ab2012-12-27 11:30:54 +01001075/* Returns a 302 for a redirectable request that reaches a server working in
1076 * in redirect mode. This may only be called just after the stream interface
1077 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1078 * follow normal proxy processing. NOTE: this function is designed to support
1079 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001080 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001081void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001082{
1083 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001084 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001085 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001086 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001087
1088 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001089 trash.len = strlen(HTTP_302);
1090 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001091
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001092 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001093
Willy Tarreauefb453c2008-10-26 20:49:47 +01001094 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001095 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001096 return;
1097
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001098 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001099 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001100 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1101 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001102 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001103
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001104 /* 3: add the request URI. Since it was already forwarded, we need
1105 * to temporarily rewind the buffer.
1106 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001107 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001108 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001109
Willy Tarreauefb453c2008-10-26 20:49:47 +01001110 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001111 len = buffer_count(s->req.buf, path, b_ptr(s->req.buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001112
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001113 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001114
Willy Tarreauefb453c2008-10-26 20:49:47 +01001115 if (!path)
1116 return;
1117
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001118 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001119 return;
1120
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001121 memcpy(trash.str + trash.len, path, len);
1122 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001123
1124 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001125 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1126 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001127 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001128 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1129 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001130 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001131
1132 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001133 si_shutr(si);
1134 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001135 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001136 si->state = SI_ST_CLO;
1137
1138 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001139 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001140
1141 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001142 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001143 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001144}
1145
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001146/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001147 * that the server side is closed. Note that err_type is actually a
1148 * bitmask, where almost only aborts may be cumulated with other
1149 * values. We consider that aborted operations are more important
1150 * than timeouts or errors due to the fact that nobody else in the
1151 * logs might explain incomplete retries. All others should avoid
1152 * being cumulated. It should normally not be possible to have multiple
1153 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001154 * Note that connection errors appearing on the second request of a keep-alive
1155 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001156 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001157void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001158{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001159 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001160
1161 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001162 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001163 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001164 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001165 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001166 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001167 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001168 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001169 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001170 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001171 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001172 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001173 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001174 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001175 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001176 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001177 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001178 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001179 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1180 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001181 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001182 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001183 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001184 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001185 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001186 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001187 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001188 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001189}
1190
Willy Tarreau42250582007-04-01 01:30:43 +02001191extern const char sess_term_cond[8];
1192extern const char sess_fin_state[8];
1193extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001194struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001195struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001196struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001197struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001198
Willy Tarreau117f59e2007-03-04 18:17:17 +01001199/*
1200 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001201 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001202 */
1203void capture_headers(char *som, struct hdr_idx *idx,
1204 char **cap, struct cap_hdr *cap_hdr)
1205{
1206 char *eol, *sol, *col, *sov;
1207 int cur_idx;
1208 struct cap_hdr *h;
1209 int len;
1210
1211 sol = som + hdr_idx_first_pos(idx);
1212 cur_idx = hdr_idx_first_idx(idx);
1213
1214 while (cur_idx) {
1215 eol = sol + idx->v[cur_idx].len;
1216
1217 col = sol;
1218 while (col < eol && *col != ':')
1219 col++;
1220
1221 sov = col + 1;
1222 while (sov < eol && http_is_lws[(unsigned char)*sov])
1223 sov++;
1224
1225 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001226 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001227 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1228 if (cap[h->index] == NULL)
1229 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001230 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001231
1232 if (cap[h->index] == NULL) {
1233 Alert("HTTP capture : out of memory.\n");
1234 continue;
1235 }
1236
1237 len = eol - sov;
1238 if (len > h->len)
1239 len = h->len;
1240
1241 memcpy(cap[h->index], sov, len);
1242 cap[h->index][len]=0;
1243 }
1244 }
1245 sol = eol + idx->v[cur_idx].cr + 1;
1246 cur_idx = idx->v[cur_idx].next;
1247 }
1248}
1249
1250
Willy Tarreau42250582007-04-01 01:30:43 +02001251/* either we find an LF at <ptr> or we jump to <bad>.
1252 */
1253#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1254
1255/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1256 * otherwise to <http_msg_ood> with <state> set to <st>.
1257 */
1258#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1259 ptr++; \
1260 if (likely(ptr < end)) \
1261 goto good; \
1262 else { \
1263 state = (st); \
1264 goto http_msg_ood; \
1265 } \
1266 } while (0)
1267
1268
Willy Tarreaubaaee002006-06-26 02:48:02 +02001269/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001270 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001271 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1272 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1273 * will give undefined results.
1274 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1275 * and that msg->sol points to the beginning of the response.
1276 * If a complete line is found (which implies that at least one CR or LF is
1277 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1278 * returned indicating an incomplete line (which does not mean that parts have
1279 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1280 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1281 * upon next call.
1282 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001283 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001284 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1285 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001286 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001287 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001288const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001289 enum ht_state state, const char *ptr, const char *end,
1290 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001291{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001292 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001293
Willy Tarreau8973c702007-01-21 23:58:29 +01001294 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001295 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001296 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001297 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001298 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1299
1300 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001301 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001302 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1303 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001304 state = HTTP_MSG_ERROR;
1305 break;
1306
Willy Tarreau8973c702007-01-21 23:58:29 +01001307 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001308 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001310 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 goto http_msg_rpcode;
1312 }
1313 if (likely(HTTP_IS_SPHT(*ptr)))
1314 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1315 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001316 state = HTTP_MSG_ERROR;
1317 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001318
Willy Tarreau8973c702007-01-21 23:58:29 +01001319 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001320 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001321 if (likely(!HTTP_IS_LWS(*ptr)))
1322 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1323
1324 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001325 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001326 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1327 }
1328
1329 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001330 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001331 http_msg_rsp_reason:
1332 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001333 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 msg->sl.st.r_l = 0;
1335 goto http_msg_rpline_eol;
1336
Willy Tarreau8973c702007-01-21 23:58:29 +01001337 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001338 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001339 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001340 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001341 goto http_msg_rpreason;
1342 }
1343 if (likely(HTTP_IS_SPHT(*ptr)))
1344 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1345 /* so it's a CR/LF, so there is no reason phrase */
1346 goto http_msg_rsp_reason;
1347
Willy Tarreau8973c702007-01-21 23:58:29 +01001348 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001349 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001350 if (likely(!HTTP_IS_CRLF(*ptr)))
1351 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001352 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001353 http_msg_rpline_eol:
1354 /* We have seen the end of line. Note that we do not
1355 * necessarily have the \n yet, but at least we know that we
1356 * have EITHER \r OR \n, otherwise the response would not be
1357 * complete. We can then record the response length and return
1358 * to the caller which will be able to register it.
1359 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001360 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001361 return ptr;
1362
Willy Tarreau8973c702007-01-21 23:58:29 +01001363 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001364#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001365 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1366 exit(1);
1367#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001368 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001369 }
1370
1371 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001372 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001373 if (ret_state)
1374 *ret_state = state;
1375 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001376 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001377 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001378}
1379
Willy Tarreau8973c702007-01-21 23:58:29 +01001380/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381 * This function parses a request line between <ptr> and <end>, starting with
1382 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1383 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1384 * will give undefined results.
1385 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1386 * and that msg->sol points to the beginning of the request.
1387 * If a complete line is found (which implies that at least one CR or LF is
1388 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1389 * returned indicating an incomplete line (which does not mean that parts have
1390 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1391 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1392 * upon next call.
1393 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001394 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1396 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001397 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001398 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001399const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001400 enum ht_state state, const char *ptr, const char *end,
1401 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001402{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001403 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001404
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001407 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 if (likely(HTTP_IS_TOKEN(*ptr)))
1409 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001410
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001412 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1414 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001415
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 if (likely(HTTP_IS_CRLF(*ptr))) {
1417 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001418 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001420 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001422 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001424 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 msg->sl.rq.v_l = 0;
1426 goto http_msg_rqline_eol;
1427 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001428 state = HTTP_MSG_ERROR;
1429 break;
1430
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001432 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001434 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001435 goto http_msg_rquri;
1436 }
1437 if (likely(HTTP_IS_SPHT(*ptr)))
1438 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1439 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1440 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001441
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001443 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001444 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001445 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001448 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1450 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001451
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001452 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001453 /* non-ASCII chars are forbidden unless option
1454 * accept-invalid-http-request is enabled in the frontend.
1455 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001456 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001457 if (msg->err_pos < -1)
1458 goto invalid_char;
1459 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001460 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001461 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1462 }
1463
1464 if (likely(HTTP_IS_CRLF(*ptr))) {
1465 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1466 goto http_msg_req09_uri_e;
1467 }
1468
1469 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001470 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001471 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001472 state = HTTP_MSG_ERROR;
1473 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001476 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001478 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001479 goto http_msg_rqver;
1480 }
1481 if (likely(HTTP_IS_SPHT(*ptr)))
1482 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1483 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1484 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001487 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001488 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001490
1491 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001492 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001493 http_msg_rqline_eol:
1494 /* We have seen the end of line. Note that we do not
1495 * necessarily have the \n yet, but at least we know that we
1496 * have EITHER \r OR \n, otherwise the request would not be
1497 * complete. We can then record the request length and return
1498 * to the caller which will be able to register it.
1499 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001500 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001501 return ptr;
1502 }
1503
1504 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001505 state = HTTP_MSG_ERROR;
1506 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001507
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001509#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1511 exit(1);
1512#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001513 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001514 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001515
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001517 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 if (ret_state)
1519 *ret_state = state;
1520 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001521 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001524
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001525/*
1526 * Returns the data from Authorization header. Function may be called more
1527 * than once so data is stored in txn->auth_data. When no header is found
1528 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001529 * searching again for something we are unable to find anyway. However, if
1530 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001531 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001532 */
1533
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001534/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1535 * set according to global.tune.bufsize.
1536 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001537char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001538
1539int
Willy Tarreau87b09662015-04-03 00:22:06 +02001540get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001541{
1542
Willy Tarreaueee5b512015-04-03 23:46:31 +02001543 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001544 struct chunk auth_method;
1545 struct hdr_ctx ctx;
1546 char *h, *p;
1547 int len;
1548
1549#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001550 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001551#endif
1552
1553 if (txn->auth.method == HTTP_AUTH_WRONG)
1554 return 0;
1555
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001556 txn->auth.method = HTTP_AUTH_WRONG;
1557
1558 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001559
1560 if (txn->flags & TX_USE_PX_CONN) {
1561 h = "Proxy-Authorization";
1562 len = strlen(h);
1563 } else {
1564 h = "Authorization";
1565 len = strlen(h);
1566 }
1567
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001568 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001569 return 0;
1570
1571 h = ctx.line + ctx.val;
1572
1573 p = memchr(h, ' ', ctx.vlen);
1574 if (!p || p == h)
1575 return 0;
1576
1577 chunk_initlen(&auth_method, h, 0, p-h);
1578 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1579
1580 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1581
1582 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001583 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001584
1585 if (len < 0)
1586 return 0;
1587
1588
1589 get_http_auth_buff[len] = '\0';
1590
1591 p = strchr(get_http_auth_buff, ':');
1592
1593 if (!p)
1594 return 0;
1595
1596 txn->auth.user = get_http_auth_buff;
1597 *p = '\0';
1598 txn->auth.pass = p+1;
1599
1600 txn->auth.method = HTTP_AUTH_BASIC;
1601 return 1;
1602 }
1603
1604 return 0;
1605}
1606
Willy Tarreau58f10d72006-12-04 02:26:12 +01001607
Willy Tarreau8973c702007-01-21 23:58:29 +01001608/*
1609 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001610 * depending on the initial msg->msg_state. The caller is responsible for
1611 * ensuring that the message does not wrap. The function can be preempted
1612 * everywhere when data are missing and recalled at the exact same location
1613 * with no information loss. The message may even be realigned between two
1614 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001615 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001616 * fields. Note that msg->sol will be initialized after completing the first
1617 * state, so that none of the msg pointers has to be initialized prior to the
1618 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001619 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001620void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621{
Willy Tarreau3770f232013-12-07 00:01:53 +01001622 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001624 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001625
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001626 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001627 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001628 ptr = buf->p + msg->next;
1629 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001630
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 if (unlikely(ptr >= end))
1632 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001633
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001634 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001635 /*
1636 * First, states that are specific to the response only.
1637 * We check them first so that request and headers are
1638 * closer to each other (accessed more often).
1639 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001640 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001641 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001642 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001643 /* we have a start of message, but we have to check
1644 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001645 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001646 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001647 if (unlikely(ptr != buf->p)) {
1648 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001649 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001650 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001651 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001652 }
Willy Tarreau26927362012-05-18 23:22:52 +02001653 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001654 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001655 hdr_idx_init(idx);
1656 state = HTTP_MSG_RPVER;
1657 goto http_msg_rpver;
1658 }
1659
1660 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1661 goto http_msg_invalid;
1662
1663 if (unlikely(*ptr == '\n'))
1664 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1665 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1666 /* stop here */
1667
Willy Tarreau8973c702007-01-21 23:58:29 +01001668 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001669 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001670 EXPECT_LF_HERE(ptr, http_msg_invalid);
1671 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1672 /* stop here */
1673
Willy Tarreau8973c702007-01-21 23:58:29 +01001674 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001675 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001676 case HTTP_MSG_RPVER_SP:
1677 case HTTP_MSG_RPCODE:
1678 case HTTP_MSG_RPCODE_SP:
1679 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001680 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001681 state, ptr, end,
1682 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001683 if (unlikely(!ptr))
1684 return;
1685
1686 /* we have a full response and we know that we have either a CR
1687 * or an LF at <ptr>.
1688 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001689 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1690
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001691 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001692 if (likely(*ptr == '\r'))
1693 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1694 goto http_msg_rpline_end;
1695
Willy Tarreau8973c702007-01-21 23:58:29 +01001696 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001697 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001698 /* msg->sol must point to the first of CR or LF. */
1699 EXPECT_LF_HERE(ptr, http_msg_invalid);
1700 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1701 /* stop here */
1702
1703 /*
1704 * Second, states that are specific to the request only
1705 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001707 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001709 /* we have a start of message, but we have to check
1710 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001711 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001712 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001713 if (likely(ptr != buf->p)) {
1714 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001715 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001716 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001717 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001718 }
Willy Tarreau26927362012-05-18 23:22:52 +02001719 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001720 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001721 state = HTTP_MSG_RQMETH;
1722 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001723 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001724
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001725 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1726 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001727
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 if (unlikely(*ptr == '\n'))
1729 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1730 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001731 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001732
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001734 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001735 EXPECT_LF_HERE(ptr, http_msg_invalid);
1736 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001737 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001738
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001740 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001741 case HTTP_MSG_RQMETH_SP:
1742 case HTTP_MSG_RQURI:
1743 case HTTP_MSG_RQURI_SP:
1744 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001745 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001746 state, ptr, end,
1747 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748 if (unlikely(!ptr))
1749 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 /* we have a full request and we know that we have either a CR
1752 * or an LF at <ptr>.
1753 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001755
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001756 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 if (likely(*ptr == '\r'))
1758 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001760
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001761 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001762 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001763 /* check for HTTP/0.9 request : no version information available.
1764 * msg->sol must point to the first of CR or LF.
1765 */
1766 if (unlikely(msg->sl.rq.v_l == 0))
1767 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001768
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001769 EXPECT_LF_HERE(ptr, http_msg_invalid);
1770 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001771 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001772
Willy Tarreau8973c702007-01-21 23:58:29 +01001773 /*
1774 * Common states below
1775 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001776 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001777 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001778 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001779 if (likely(!HTTP_IS_CRLF(*ptr))) {
1780 goto http_msg_hdr_name;
1781 }
1782
1783 if (likely(*ptr == '\r'))
1784 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1785 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001786
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001787 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001788 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001789 /* assumes msg->sol points to the first char */
1790 if (likely(HTTP_IS_TOKEN(*ptr)))
1791 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001792
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001793 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001794 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001795
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001796 if (likely(msg->err_pos < -1) || *ptr == '\n')
1797 goto http_msg_invalid;
1798
1799 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001800 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001801
1802 /* and we still accept this non-token character */
1803 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001804
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001805 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001806 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001807 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001808 if (likely(HTTP_IS_SPHT(*ptr)))
1809 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001810
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001811 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001812 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001813
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001814 if (likely(!HTTP_IS_CRLF(*ptr))) {
1815 goto http_msg_hdr_val;
1816 }
1817
1818 if (likely(*ptr == '\r'))
1819 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1820 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001821
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001822 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001823 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001824 EXPECT_LF_HERE(ptr, http_msg_invalid);
1825 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001826
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001827 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001828 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001829 if (likely(HTTP_IS_SPHT(*ptr))) {
1830 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001831 for (; buf->p + msg->sov < ptr; msg->sov++)
1832 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001833 goto http_msg_hdr_l1_sp;
1834 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001835 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001836 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001837 goto http_msg_complete_header;
1838
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001839 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001840 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001841 /* assumes msg->sol points to the first char, and msg->sov
1842 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001843 */
1844 if (likely(!HTTP_IS_CRLF(*ptr)))
1845 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001846
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001847 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001848 /* Note: we could also copy eol into ->eoh so that we have the
1849 * real header end in case it ends with lots of LWS, but is this
1850 * really needed ?
1851 */
1852 if (likely(*ptr == '\r'))
1853 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1854 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001855
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001856 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001857 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001858 EXPECT_LF_HERE(ptr, http_msg_invalid);
1859 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001860
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001861 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001862 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001863 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1864 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001865 for (; buf->p + msg->eol < ptr; msg->eol++)
1866 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 goto http_msg_hdr_val;
1868 }
1869 http_msg_complete_header:
1870 /*
1871 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001872 * Assumes msg->sol points to the first char, msg->sov points
1873 * to the first character of the value and msg->eol to the
1874 * first CR or LF so we know how the line ends. We insert last
1875 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001876 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001877 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001878 idx, idx->tail) < 0))
1879 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001880
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001881 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001882 if (likely(!HTTP_IS_CRLF(*ptr))) {
1883 goto http_msg_hdr_name;
1884 }
1885
1886 if (likely(*ptr == '\r'))
1887 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1888 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001889
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001890 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001891 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001892 /* Assumes msg->sol points to the first of either CR or LF.
1893 * Sets ->sov and ->next to the total header length, ->eoh to
1894 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1895 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 EXPECT_LF_HERE(ptr, http_msg_invalid);
1897 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001898 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001899 msg->eoh = msg->sol;
1900 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001901 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001902 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001903 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001904
1905 case HTTP_MSG_ERROR:
1906 /* this may only happen if we call http_msg_analyser() twice with an error */
1907 break;
1908
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001909 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001910#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001911 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1912 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001913#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001914 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001915 }
1916 http_msg_ood:
1917 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001918 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001919 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001920 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001921
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001922 http_msg_invalid:
1923 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001924 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001925 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001926 return;
1927}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001928
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001929/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1930 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1931 * nothing is done and 1 is returned.
1932 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001933static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001934{
1935 int delta;
1936 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001937 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001938
1939 if (msg->sl.rq.v_l != 0)
1940 return 1;
1941
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001942 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1943 if (txn->meth != HTTP_METH_GET)
1944 return 0;
1945
Willy Tarreau9b28e032012-10-12 23:49:43 +02001946 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001947 delta = 0;
1948
1949 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001950 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1951 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001952 }
1953 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001954 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001955 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001956 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001957 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001958 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001959 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001960 NULL, NULL);
1961 if (unlikely(!cur_end))
1962 return 0;
1963
1964 /* we have a full HTTP/1.0 request now and we know that
1965 * we have either a CR or an LF at <ptr>.
1966 */
1967 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1968 return 1;
1969}
1970
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001971/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001972 * and "keep-alive" values. If we already know that some headers may safely
1973 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001974 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1975 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001976 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001977 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1978 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1979 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001980 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001981 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001982void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001983{
Willy Tarreau5b154472009-12-21 20:11:07 +01001984 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001985 const char *hdr_val = "Connection";
1986 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001987
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001988 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001989 return;
1990
Willy Tarreau88d349d2010-01-25 12:15:43 +01001991 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1992 hdr_val = "Proxy-Connection";
1993 hdr_len = 16;
1994 }
1995
Willy Tarreau5b154472009-12-21 20:11:07 +01001996 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001997 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001998 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001999 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2000 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002001 if (to_del & 2)
2002 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002003 else
2004 txn->flags |= TX_CON_KAL_SET;
2005 }
2006 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2007 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002008 if (to_del & 1)
2009 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002010 else
2011 txn->flags |= TX_CON_CLO_SET;
2012 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002013 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2014 txn->flags |= TX_HDR_CONN_UPG;
2015 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002016 }
2017
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002018 txn->flags |= TX_HDR_CONN_PRS;
2019 return;
2020}
Willy Tarreau5b154472009-12-21 20:11:07 +01002021
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002022/* Apply desired changes on the Connection: header. Values may be removed and/or
2023 * added depending on the <wanted> flags, which are exclusively composed of
2024 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2025 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2026 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002027void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002028{
2029 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002030 const char *hdr_val = "Connection";
2031 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002032
2033 ctx.idx = 0;
2034
Willy Tarreau88d349d2010-01-25 12:15:43 +01002035
2036 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2037 hdr_val = "Proxy-Connection";
2038 hdr_len = 16;
2039 }
2040
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002041 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002042 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002043 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2044 if (wanted & TX_CON_KAL_SET)
2045 txn->flags |= TX_CON_KAL_SET;
2046 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002047 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002048 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002049 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2050 if (wanted & TX_CON_CLO_SET)
2051 txn->flags |= TX_CON_CLO_SET;
2052 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002053 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002054 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002055 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002056
2057 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2058 return;
2059
2060 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2061 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002062 hdr_val = "Connection: close";
2063 hdr_len = 17;
2064 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2065 hdr_val = "Proxy-Connection: close";
2066 hdr_len = 23;
2067 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002068 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002069 }
2070
2071 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2072 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002073 hdr_val = "Connection: keep-alive";
2074 hdr_len = 22;
2075 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2076 hdr_val = "Proxy-Connection: keep-alive";
2077 hdr_len = 28;
2078 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002079 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002080 }
2081 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002082}
2083
Willy Tarreauc24715e2014-04-17 15:21:20 +02002084/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
2085 * the first byte of data after the chunk size, so that we know we can forward
2086 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
2087 * the chunk size. That way it is always possible to differentiate between the
2088 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01002089 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002090 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002091 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002092static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002093{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002094 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002095 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002096 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002097 const char *end = buf->data + buf->size;
2098 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002099 unsigned int chunk = 0;
2100
2101 /* The chunk size is in the following form, though we are only
2102 * interested in the size and CRLF :
2103 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2104 */
2105 while (1) {
2106 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002107 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002108 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002109 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002110 if (c < 0) /* not a hex digit anymore */
2111 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002112 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002113 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002114 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002115 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002116 chunk = (chunk << 4) + c;
2117 }
2118
Willy Tarreaud98cf932009-12-27 22:54:55 +01002119 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002120 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002121 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002122
2123 while (http_is_spht[(unsigned char)*ptr]) {
2124 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002125 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002126 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002127 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002128 }
2129
Willy Tarreaud98cf932009-12-27 22:54:55 +01002130 /* Up to there, we know that at least one byte is present at *ptr. Check
2131 * for the end of chunk size.
2132 */
2133 while (1) {
2134 if (likely(HTTP_IS_CRLF(*ptr))) {
2135 /* we now have a CR or an LF at ptr */
2136 if (likely(*ptr == '\r')) {
2137 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002138 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002139 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002140 return 0;
2141 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002142
Willy Tarreaud98cf932009-12-27 22:54:55 +01002143 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002144 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002145 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002146 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002147 /* done */
2148 break;
2149 }
2150 else if (*ptr == ';') {
2151 /* chunk extension, ends at next CRLF */
2152 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002153 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002154 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002155 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002156
2157 while (!HTTP_IS_CRLF(*ptr)) {
2158 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002159 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002160 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002161 return 0;
2162 }
2163 /* we have a CRLF now, loop above */
2164 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002165 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002166 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002167 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002168 }
2169
Willy Tarreaud98cf932009-12-27 22:54:55 +01002170 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002171 * which may or may not be present. We save that into ->next,
2172 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002173 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002174 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002175 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002176 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002177 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002178 msg->chunk_len = chunk;
2179 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002180 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002181 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002182 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002183 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002184 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002185}
2186
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002187/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002188 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002189 * the trailers is found, it is automatically scheduled to be forwarded,
2190 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2191 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002192 * except maybe msg->next if it could parse some lines, and returns zero.
2193 * If a parse error is encountered, the function returns < 0 and does not
2194 * change anything except maybe msg->next. Note that the message must
2195 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002196 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002197 * forwarding, and that msg->next exactly matches the length of trailers
2198 * already parsed and not forwarded. It is also important to note that this
2199 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002200 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002201static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002202{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002203 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002204
Willy Tarreaua458b672012-03-05 11:17:50 +01002205 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002206 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002207 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002208 const char *ptr = b_ptr(buf, msg->next);
2209 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002210 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002211
2212 /* scan current line and stop at LF or CRLF */
2213 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002214 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002215 return 0;
2216
2217 if (*ptr == '\n') {
2218 if (!p1)
2219 p1 = ptr;
2220 p2 = ptr;
2221 break;
2222 }
2223
2224 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002225 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002226 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002227 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002228 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002229 p1 = ptr;
2230 }
2231
2232 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002233 if (ptr >= buf->data + buf->size)
2234 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002235 }
2236
2237 /* after LF; point to beginning of next line */
2238 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002239 if (p2 >= buf->data + buf->size)
2240 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002241
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002242 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002243 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002244 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002245
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002246 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002247 /* LF/CRLF at beginning of line => end of trailers at p2.
2248 * Everything was scheduled for forwarding, there's nothing
2249 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002250 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002251 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002252 msg->msg_state = HTTP_MSG_DONE;
2253 return 1;
2254 }
2255 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002256 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002257 }
2258}
2259
Willy Tarreauc24715e2014-04-17 15:21:20 +02002260/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2261 * or a possible LF alone at the end of a chunk. It automatically adjusts
2262 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002263 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002264 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2265 * not enough data are available, the function does not change anything and
2266 * returns zero. If a parse error is encountered, the function returns < 0 and
2267 * does not change anything. Note: this function is designed to parse wrapped
2268 * CRLF at the end of the buffer.
2269 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002270static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002271{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002272 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002273 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002274 int bytes;
2275
2276 /* NB: we'll check data availabilty at the end. It's not a
2277 * problem because whatever we match first will be checked
2278 * against the correct length.
2279 */
2280 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002281 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002282 if (*ptr == '\r') {
2283 bytes++;
2284 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002285 if (ptr >= buf->data + buf->size)
2286 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002287 }
2288
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002289 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002290 return 0;
2291
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002292 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002293 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002294 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002295 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002296
2297 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002298 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002299 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002300 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002301 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002302 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2303 return 1;
2304}
Willy Tarreau5b154472009-12-21 20:11:07 +01002305
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002306/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2307 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2308 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2309 * Unparsable qvalues return 1000 as "q=1.000".
2310 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002311int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002312{
2313 int q = 1000;
2314
Willy Tarreau506c69a2014-07-08 00:59:48 +02002315 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002316 goto out;
2317 q = (*qvalue++ - '0') * 1000;
2318
2319 if (*qvalue++ != '.')
2320 goto out;
2321
Willy Tarreau506c69a2014-07-08 00:59:48 +02002322 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002323 goto out;
2324 q += (*qvalue++ - '0') * 100;
2325
Willy Tarreau506c69a2014-07-08 00:59:48 +02002326 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002327 goto out;
2328 q += (*qvalue++ - '0') * 10;
2329
Willy Tarreau506c69a2014-07-08 00:59:48 +02002330 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002331 goto out;
2332 q += (*qvalue++ - '0') * 1;
2333 out:
2334 if (q > 1000)
2335 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002336 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002337 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002338 return q;
2339}
William Lallemand82fe75c2012-10-23 10:25:10 +02002340
2341/*
2342 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002343 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002344int select_compression_request_header(struct stream *s, struct buffer *req)
William Lallemand82fe75c2012-10-23 10:25:10 +02002345{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002346 struct http_txn *txn = s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002347 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002348 struct hdr_ctx ctx;
2349 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002350 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002351
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002352 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2353 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002354 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2355 */
2356 ctx.idx = 0;
2357 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2358 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002359 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2360 (ctx.vlen < 31 ||
2361 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2362 ctx.line[ctx.val + 30] < '6' ||
2363 (ctx.line[ctx.val + 30] == '6' &&
2364 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2365 s->comp_algo = NULL;
2366 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002367 }
2368
William Lallemand82fe75c2012-10-23 10:25:10 +02002369 /* search for the algo in the backend in priority or the frontend */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002370 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002371 int best_q = 0;
2372
William Lallemand82fe75c2012-10-23 10:25:10 +02002373 ctx.idx = 0;
2374 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002375 const char *qval;
2376 int q;
2377 int toklen;
2378
2379 /* try to isolate the token from the optional q-value */
2380 toklen = 0;
2381 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2382 toklen++;
2383
2384 qval = ctx.line + ctx.val + toklen;
2385 while (1) {
2386 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2387 qval++;
2388
2389 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2390 qval = NULL;
2391 break;
2392 }
2393 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002394
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002395 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2396 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002397
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002398 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2399 qval = NULL;
2400 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002401 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002402 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2403 break;
2404
2405 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2406 qval++;
2407 }
2408
2409 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002410 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002411
2412 if (q <= best_q)
2413 continue;
2414
2415 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2416 if (*(ctx.line + ctx.val) == '*' ||
Willy Tarreau615105e2015-03-28 16:40:46 +01002417 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002418 s->comp_algo = comp_algo;
2419 best_q = q;
2420 break;
2421 }
2422 }
2423 }
2424 }
2425
2426 /* remove all occurrences of the header when "compression offload" is set */
2427 if (s->comp_algo) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002428 if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002429 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2430 ctx.idx = 0;
2431 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2432 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002433 }
2434 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002435 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002436 }
2437
2438 /* identity is implicit does not require headers */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002439 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002440 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
Willy Tarreau615105e2015-03-28 16:40:46 +01002441 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002442 s->comp_algo = comp_algo;
2443 return 1;
2444 }
2445 }
2446 }
2447
2448 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002449 return 0;
2450}
2451
2452/*
2453 * Selects a comression algorithm depending of the server response.
2454 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002455int select_compression_response_header(struct stream *s, struct buffer *res)
William Lallemand82fe75c2012-10-23 10:25:10 +02002456{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002457 struct http_txn *txn = s->txn;
William Lallemand82fe75c2012-10-23 10:25:10 +02002458 struct http_msg *msg = &txn->rsp;
2459 struct hdr_ctx ctx;
2460 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002461
2462 /* no common compression algorithm was found in request header */
2463 if (s->comp_algo == NULL)
2464 goto fail;
2465
2466 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002467 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002468 goto fail;
2469
Jesse Hathaway2468d4e2015-03-06 20:16:15 +00002470 /* compress 200,201,202,203 responses only */
2471 if ((txn->status != 200) &&
2472 (txn->status != 201) &&
2473 (txn->status != 202) &&
2474 (txn->status != 203))
William Lallemandd3002612012-11-26 14:34:47 +01002475 goto fail;
2476
William Lallemand82fe75c2012-10-23 10:25:10 +02002477 /* Content-Length is null */
2478 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2479 goto fail;
2480
2481 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002482 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002483 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2484 goto fail;
2485
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002486 /* no compression when Cache-Control: no-transform is present in the message */
2487 ctx.idx = 0;
2488 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2489 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2490 goto fail;
2491 }
2492
William Lallemand82fe75c2012-10-23 10:25:10 +02002493 comp_type = NULL;
2494
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002495 /* we don't want to compress multipart content-types, nor content-types that are
2496 * not listed in the "compression type" directive if any. If no content-type was
2497 * found but configuration requires one, we don't compress either. Backend has
2498 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002499 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002500 ctx.idx = 0;
2501 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2502 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2503 goto fail;
2504
2505 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002506 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002507 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002508 if (ctx.vlen >= comp_type->name_len &&
2509 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002510 /* this Content-Type should be compressed */
2511 break;
2512 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002513 /* this Content-Type should not be compressed */
2514 if (comp_type == NULL)
2515 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002516 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002517 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002518 else { /* no content-type header */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002519 if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->comp->types))
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002520 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002521 }
2522
William Lallemandd85f9172012-11-09 17:05:39 +01002523 /* limit compression rate */
2524 if (global.comp_rate_lim > 0)
2525 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2526 goto fail;
2527
William Lallemand072a2bf2012-11-20 17:01:01 +01002528 /* limit cpu usage */
2529 if (idle_pct < compress_min_idle)
2530 goto fail;
2531
William Lallemand4c49fae2012-11-07 15:00:23 +01002532 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002533 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002534 goto fail;
2535
Willy Tarreaue7dff022015-04-03 01:14:29 +02002536 s->flags |= SF_COMP_READY;
William Lallemandec3e3892012-11-12 17:02:18 +01002537
William Lallemand82fe75c2012-10-23 10:25:10 +02002538 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002539 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002540 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2541 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2542
2543 /* add Transfer-Encoding header */
2544 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2545 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2546
2547 /*
2548 * Add Content-Encoding header when it's not identity encoding.
2549 * RFC 2616 : Identity encoding: This content-coding is used only in the
2550 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2551 * header.
2552 */
Willy Tarreau615105e2015-03-28 16:40:46 +01002553 if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002554 trash.len = 18;
2555 memcpy(trash.str, "Content-Encoding: ", trash.len);
Willy Tarreau615105e2015-03-28 16:40:46 +01002556 memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len);
2557 trash.len += s->comp_algo->ua_name_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002558 trash.str[trash.len] = '\0';
2559 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002560 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002561 return 1;
2562
2563fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002564 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002565 return 0;
2566}
2567
Willy Tarreau87b09662015-04-03 00:22:06 +02002568void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002569{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002570 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002571 int tmp = TX_CON_WANT_KAL;
2572
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002573 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2574 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002575 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2576 tmp = TX_CON_WANT_TUN;
2577
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002578 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002579 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2580 tmp = TX_CON_WANT_TUN;
2581 }
2582
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002583 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002584 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2585 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002586 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002587 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2588 tmp = TX_CON_WANT_CLO;
2589 else
2590 tmp = TX_CON_WANT_SCL;
2591 }
2592
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002593 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002594 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2595 tmp = TX_CON_WANT_CLO;
2596
2597 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2598 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2599
2600 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2601 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2602 /* parse the Connection header and possibly clean it */
2603 int to_del = 0;
2604 if ((msg->flags & HTTP_MSGF_VER_11) ||
2605 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002606 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002607 to_del |= 2; /* remove "keep-alive" */
2608 if (!(msg->flags & HTTP_MSGF_VER_11))
2609 to_del |= 1; /* remove "close" */
2610 http_parse_connection_header(txn, msg, to_del);
2611 }
2612
2613 /* check if client or config asks for explicit close in KAL/SCL */
2614 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2615 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2616 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2617 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2618 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002619 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002620 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2621}
William Lallemand82fe75c2012-10-23 10:25:10 +02002622
Willy Tarreaud787e662009-07-07 10:14:51 +02002623/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2624 * processing can continue on next analysers, or zero if it either needs more
2625 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002626 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002627 * when it has nothing left to do, and may remove any analyser when it wants to
2628 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002629 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002630int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002631{
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 /*
2633 * We will parse the partial (or complete) lines.
2634 * We will check the request syntax, and also join multi-line
2635 * headers. An index of all the lines will be elaborated while
2636 * parsing.
2637 *
2638 * For the parsing, we use a 28 states FSM.
2639 *
2640 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002641 * req->buf->p = beginning of request
2642 * req->buf->p + msg->eoh = end of processed headers / start of current one
2643 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002644 * msg->eol = end of current header or line (LF or CRLF)
2645 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002646 *
2647 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002648 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002649 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2650 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002651 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002652
Willy Tarreau59234e92008-11-30 23:51:27 +01002653 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002654 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002655 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002656 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002657 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002658
Willy Tarreau87b09662015-04-03 00:22:06 +02002659 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002660 now_ms, __FUNCTION__,
2661 s,
2662 req,
2663 req->rex, req->wex,
2664 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002665 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002666 req->analysers);
2667
Willy Tarreau52a0c602009-08-16 22:45:38 +02002668 /* we're speaking HTTP here, so let's speak HTTP to the client */
2669 s->srv_error = http_return_srv_error;
2670
Willy Tarreau83e3af02009-12-28 17:39:57 +01002671 /* There's a protected area at the end of the buffer for rewriting
2672 * purposes. We don't want to start to parse the request if the
2673 * protected area is affected, because we may have to move processed
2674 * data later, which is much more complicated.
2675 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002676 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002677 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002678 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002679 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002680 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002681 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002682 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002683 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002684 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002685 return 0;
2686 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002687 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2688 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2689 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002690 }
2691
Willy Tarreau065e8332010-01-08 00:30:20 +01002692 /* Note that we have the same problem with the response ; we
2693 * may want to send a redirect, error or anything which requires
2694 * some spare space. So we'll ensure that we have at least
2695 * maxrewrite bytes available in the response buffer before
2696 * processing that one. This will only affect pipelined
2697 * keep-alive requests.
2698 */
2699 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002700 unlikely(!channel_is_rewritable(&s->res) ||
2701 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2702 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2703 if (s->res.buf->o) {
2704 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002705 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002706 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002707 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002708 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2709 s->res.flags |= CF_WAKE_WRITE;
2710 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002711 return 0;
2712 }
2713 }
2714
Willy Tarreau9b28e032012-10-12 23:49:43 +02002715 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002716 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002717 }
2718
Willy Tarreau59234e92008-11-30 23:51:27 +01002719 /* 1: we might have to print this header in debug mode */
2720 if (unlikely((global.mode & MODE_DEBUG) &&
2721 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002722 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002724
Willy Tarreau9b28e032012-10-12 23:49:43 +02002725 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002726 /* this is a bit complex : in case of error on the request line,
2727 * we know that rq.l is still zero, so we display only the part
2728 * up to the end of the line (truncated by debug_hdr).
2729 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002730 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002731 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002732
Willy Tarreau59234e92008-11-30 23:51:27 +01002733 sol += hdr_idx_first_pos(&txn->hdr_idx);
2734 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002735
Willy Tarreau59234e92008-11-30 23:51:27 +01002736 while (cur_idx) {
2737 eol = sol + txn->hdr_idx.v[cur_idx].len;
2738 debug_hdr("clihdr", s, sol, eol);
2739 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2740 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002741 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002742 }
2743
Willy Tarreau58f10d72006-12-04 02:26:12 +01002744
Willy Tarreau59234e92008-11-30 23:51:27 +01002745 /*
2746 * Now we quickly check if we have found a full valid request.
2747 * If not so, we check the FD and buffer states before leaving.
2748 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002749 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002750 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002751 * on a keep-alive stream, if we encounter and error, close, t/o,
2752 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002753 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002754 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002755 * Last, we may increase some tracked counters' http request errors on
2756 * the cases that are deliberately the client's fault. For instance,
2757 * a timeout or connection reset is not counted as an error. However
2758 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002759 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002760
Willy Tarreau655dce92009-11-08 13:10:58 +01002761 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002762 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002763 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002764 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002765 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002766 stream_inc_http_req_ctr(s);
2767 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002768 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002769 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002770 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002771
Willy Tarreau59234e92008-11-30 23:51:27 +01002772 /* 1: Since we are in header mode, if there's no space
2773 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002774 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002775 * must terminate it now.
2776 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002777 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002778 /* FIXME: check if URI is set and return Status
2779 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002780 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002781 stream_inc_http_req_ctr(s);
2782 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002783 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002784 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002785 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002786 goto return_bad_req;
2787 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002788
Willy Tarreau59234e92008-11-30 23:51:27 +01002789 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002790 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002791 if (!(s->flags & SF_ERR_MASK))
2792 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002793
Willy Tarreaufcffa692010-01-10 14:21:19 +01002794 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002795 goto failed_keep_alive;
2796
Willy Tarreau0f228a02015-05-01 15:37:53 +02002797 if (sess->fe->options & PR_O_IGNORE_PRB)
2798 goto failed_keep_alive;
2799
Willy Tarreau59234e92008-11-30 23:51:27 +01002800 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002801 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002802 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002803 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002804 }
2805
Willy Tarreaudc979f22012-12-04 10:39:01 +01002806 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002807 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002808 msg->msg_state = HTTP_MSG_ERROR;
2809 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002810
Willy Tarreau87b09662015-04-03 00:22:06 +02002811 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002812 proxy_inc_fe_req_ctr(sess->fe);
2813 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002814 if (sess->listener->counters)
2815 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002816
Willy Tarreaue7dff022015-04-03 01:14:29 +02002817 if (!(s->flags & SF_FINST_MASK))
2818 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002819 return 0;
2820 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002821
Willy Tarreau59234e92008-11-30 23:51:27 +01002822 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002823 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002824 if (!(s->flags & SF_ERR_MASK))
2825 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002826
Willy Tarreaufcffa692010-01-10 14:21:19 +01002827 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002828 goto failed_keep_alive;
2829
Willy Tarreau0f228a02015-05-01 15:37:53 +02002830 if (sess->fe->options & PR_O_IGNORE_PRB)
2831 goto failed_keep_alive;
2832
Willy Tarreau59234e92008-11-30 23:51:27 +01002833 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002834 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002835 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002836 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002837 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002838 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01002839 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002840 msg->msg_state = HTTP_MSG_ERROR;
2841 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002842
Willy Tarreau87b09662015-04-03 00:22:06 +02002843 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002844 proxy_inc_fe_req_ctr(sess->fe);
2845 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002846 if (sess->listener->counters)
2847 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002848
Willy Tarreaue7dff022015-04-03 01:14:29 +02002849 if (!(s->flags & SF_FINST_MASK))
2850 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002851 return 0;
2852 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002853
Willy Tarreau59234e92008-11-30 23:51:27 +01002854 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002855 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002856 if (!(s->flags & SF_ERR_MASK))
2857 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002858
Willy Tarreaufcffa692010-01-10 14:21:19 +01002859 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002860 goto failed_keep_alive;
2861
Willy Tarreau0f228a02015-05-01 15:37:53 +02002862 if (sess->fe->options & PR_O_IGNORE_PRB)
2863 goto failed_keep_alive;
2864
Willy Tarreau4076a152009-04-02 15:18:36 +02002865 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002866 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002867 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002868 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002869 msg->msg_state = HTTP_MSG_ERROR;
2870 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002871
Willy Tarreau87b09662015-04-03 00:22:06 +02002872 stream_inc_http_err_ctr(s);
2873 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002874 proxy_inc_fe_req_ctr(sess->fe);
2875 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002876 if (sess->listener->counters)
2877 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002878
Willy Tarreaue7dff022015-04-03 01:14:29 +02002879 if (!(s->flags & SF_FINST_MASK))
2880 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002881 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002882 }
2883
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002884 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002885 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002886 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002887#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002888 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2889 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002890 /* We need more data, we have to re-enable quick-ack in case we
2891 * previously disabled it, otherwise we might cause the client
2892 * to delay next data.
2893 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002894 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002895 }
2896#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002897
Willy Tarreaufcffa692010-01-10 14:21:19 +01002898 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2899 /* If the client starts to talk, let's fall back to
2900 * request timeout processing.
2901 */
2902 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002903 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002904 }
2905
Willy Tarreau59234e92008-11-30 23:51:27 +01002906 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002907 if (!tick_isset(req->analyse_exp)) {
2908 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2909 (txn->flags & TX_WAIT_NEXT_RQ) &&
2910 tick_isset(s->be->timeout.httpka))
2911 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2912 else
2913 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2914 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002915
Willy Tarreau59234e92008-11-30 23:51:27 +01002916 /* we're not ready yet */
2917 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002918
2919 failed_keep_alive:
2920 /* Here we process low-level errors for keep-alive requests. In
2921 * short, if the request is not the first one and it experiences
2922 * a timeout, read error or shutdown, we just silently close so
2923 * that the client can try again.
2924 */
2925 txn->status = 0;
2926 msg->msg_state = HTTP_MSG_RQBEFORE;
2927 req->analysers = 0;
2928 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002929 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002930 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau350f4872014-11-28 14:42:25 +01002931 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002932 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002933 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002934
Willy Tarreaud787e662009-07-07 10:14:51 +02002935 /* OK now we have a complete HTTP request with indexed headers. Let's
2936 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002937 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002938 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002939 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002940 * byte after the last LF. msg->sov points to the first byte of data.
2941 * msg->eol cannot be trusted because it may have been left uninitialized
2942 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002943 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002944
Willy Tarreau87b09662015-04-03 00:22:06 +02002945 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002946 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002947
Willy Tarreaub16a5742010-01-10 14:46:16 +01002948 if (txn->flags & TX_WAIT_NEXT_RQ) {
2949 /* kill the pending keep-alive timeout */
2950 txn->flags &= ~TX_WAIT_NEXT_RQ;
2951 req->analyse_exp = TICK_ETERNITY;
2952 }
2953
2954
Willy Tarreaud787e662009-07-07 10:14:51 +02002955 /* Maybe we found in invalid header name while we were configured not
2956 * to block on that, so we have to capture it now.
2957 */
2958 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002959 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002960
Willy Tarreau59234e92008-11-30 23:51:27 +01002961 /*
2962 * 1: identify the method
2963 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002964 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002965
2966 /* we can make use of server redirect on GET and HEAD */
2967 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002968 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002969
Willy Tarreau59234e92008-11-30 23:51:27 +01002970 /*
2971 * 2: check if the URI matches the monitor_uri.
2972 * We have to do this for every request which gets in, because
2973 * the monitor-uri is defined by the frontend.
2974 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002975 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2976 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002977 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002978 sess->fe->monitor_uri,
2979 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002980 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002981 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002982 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002983 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002984
Willy Tarreaue7dff022015-04-03 01:14:29 +02002985 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002986 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002987
Willy Tarreau59234e92008-11-30 23:51:27 +01002988 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002989 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002990 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002991
Willy Tarreau59234e92008-11-30 23:51:27 +01002992 ret = acl_pass(ret);
2993 if (cond->pol == ACL_COND_UNLESS)
2994 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002995
Willy Tarreau59234e92008-11-30 23:51:27 +01002996 if (ret) {
2997 /* we fail this request, let's return 503 service unavail */
2998 txn->status = 503;
Willy Tarreau350f4872014-11-28 14:42:25 +01002999 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02003000 if (!(s->flags & SF_ERR_MASK))
3001 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01003002 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01003003 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003004 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01003005
Willy Tarreau59234e92008-11-30 23:51:27 +01003006 /* nothing to fail, let's reply normaly */
3007 txn->status = 200;
Willy Tarreau350f4872014-11-28 14:42:25 +01003008 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02003009 if (!(s->flags & SF_ERR_MASK))
3010 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01003011 goto return_prx_cond;
3012 }
3013
3014 /*
3015 * 3: Maybe we have to copy the original REQURI for the logs ?
3016 * Note: we cannot log anymore if the request has been
3017 * classified as invalid.
3018 */
3019 if (unlikely(s->logs.logwait & LW_REQ)) {
3020 /* we have a complete HTTP request that we must log */
3021 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
3022 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003023
Willy Tarreau59234e92008-11-30 23:51:27 +01003024 if (urilen >= REQURI_LEN)
3025 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02003026 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01003027 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003028
Willy Tarreaud79a3b22012-12-28 09:40:16 +01003029 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01003030 s->do_log(s);
3031 } else {
3032 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003033 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003034 }
Willy Tarreau06619262006-12-17 08:37:22 +01003035
Willy Tarreau91852eb2015-05-01 13:26:00 +02003036 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
3037 * exactly one digit "." one digit. This check may be disabled using
3038 * option accept-invalid-http-request.
3039 */
3040 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
3041 if (msg->sl.rq.v_l != 8) {
3042 msg->err_pos = msg->sl.rq.v;
3043 goto return_bad_req;
3044 }
3045
3046 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
3047 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
3048 req->buf->p[msg->sl.rq.v + 6] != '.' ||
3049 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
3050 msg->err_pos = msg->sl.rq.v + 4;
3051 goto return_bad_req;
3052 }
3053 }
Willy Tarreau13317662015-05-01 13:47:08 +02003054 else {
3055 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
3056 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
3057 goto return_bad_req;
3058 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02003059
Willy Tarreau5b154472009-12-21 20:11:07 +01003060 /* ... and check if the request is HTTP/1.1 or above */
3061 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003062 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
3063 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
3064 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003065 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01003066
3067 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01003068 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01003069
Willy Tarreau88d349d2010-01-25 12:15:43 +01003070 /* if the frontend has "option http-use-proxy-header", we'll check if
3071 * we have what looks like a proxied connection instead of a connection,
3072 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
3073 * Note that this is *not* RFC-compliant, however browsers and proxies
3074 * happen to do that despite being non-standard :-(
3075 * We consider that a request not beginning with either '/' or '*' is
3076 * a proxied connection, which covers both "scheme://location" and
3077 * CONNECT ip:port.
3078 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003079 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003080 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01003081 txn->flags |= TX_USE_PX_CONN;
3082
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003083 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003084 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003085
Willy Tarreau59234e92008-11-30 23:51:27 +01003086 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003087 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003088 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003089 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003090
Willy Tarreau557f1992015-05-01 10:05:17 +02003091 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3092 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003093 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003094 * The length of a message body is determined by one of the following
3095 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003096 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003097 * 1. Any response to a HEAD request and any response with a 1xx
3098 * (Informational), 204 (No Content), or 304 (Not Modified) status
3099 * code is always terminated by the first empty line after the
3100 * header fields, regardless of the header fields present in the
3101 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003102 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003103 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3104 * the connection will become a tunnel immediately after the empty
3105 * line that concludes the header fields. A client MUST ignore any
3106 * Content-Length or Transfer-Encoding header fields received in
3107 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003108 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003109 * 3. If a Transfer-Encoding header field is present and the chunked
3110 * transfer coding (Section 4.1) is the final encoding, the message
3111 * body length is determined by reading and decoding the chunked
3112 * data until the transfer coding indicates the data is complete.
3113 *
3114 * If a Transfer-Encoding header field is present in a response and
3115 * the chunked transfer coding is not the final encoding, the
3116 * message body length is determined by reading the connection until
3117 * it is closed by the server. If a Transfer-Encoding header field
3118 * is present in a request and the chunked transfer coding is not
3119 * the final encoding, the message body length cannot be determined
3120 * reliably; the server MUST respond with the 400 (Bad Request)
3121 * status code and then close the connection.
3122 *
3123 * If a message is received with both a Transfer-Encoding and a
3124 * Content-Length header field, the Transfer-Encoding overrides the
3125 * Content-Length. Such a message might indicate an attempt to
3126 * perform request smuggling (Section 9.5) or response splitting
3127 * (Section 9.4) and ought to be handled as an error. A sender MUST
3128 * remove the received Content-Length field prior to forwarding such
3129 * a message downstream.
3130 *
3131 * 4. If a message is received without Transfer-Encoding and with
3132 * either multiple Content-Length header fields having differing
3133 * field-values or a single Content-Length header field having an
3134 * invalid value, then the message framing is invalid and the
3135 * recipient MUST treat it as an unrecoverable error. If this is a
3136 * request message, the server MUST respond with a 400 (Bad Request)
3137 * status code and then close the connection. If this is a response
3138 * message received by a proxy, the proxy MUST close the connection
3139 * to the server, discard the received response, and send a 502 (Bad
3140 * Gateway) response to the client. If this is a response message
3141 * received by a user agent, the user agent MUST close the
3142 * connection to the server and discard the received response.
3143 *
3144 * 5. If a valid Content-Length header field is present without
3145 * Transfer-Encoding, its decimal value defines the expected message
3146 * body length in octets. If the sender closes the connection or
3147 * the recipient times out before the indicated number of octets are
3148 * received, the recipient MUST consider the message to be
3149 * incomplete and close the connection.
3150 *
3151 * 6. If this is a request message and none of the above are true, then
3152 * the message body length is zero (no message body is present).
3153 *
3154 * 7. Otherwise, this is a response message without a declared message
3155 * body length, so the message body length is determined by the
3156 * number of octets received prior to the server closing the
3157 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003158 */
3159
Willy Tarreau32b47f42009-10-18 20:55:02 +02003160 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003161 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003162 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003163 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003164 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3165 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003166 /* chunked not last, return badreq */
3167 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003168 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003169 }
3170
Willy Tarreau1c913912015-04-30 10:57:51 +02003171 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003172 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003173 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3174 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3175 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3176 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003177 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003178 signed long long cl;
3179
Willy Tarreauad14f752011-09-02 20:33:27 +02003180 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003181 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003182 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003183 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003184
Willy Tarreauad14f752011-09-02 20:33:27 +02003185 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003186 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003187 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003188 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003189
Willy Tarreauad14f752011-09-02 20:33:27 +02003190 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003191 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003192 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003193 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003194
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003195 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003196 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003197 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003198 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003199
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003200 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003201 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003202 }
3203
Willy Tarreau34dfc602015-05-01 10:09:49 +02003204 /* even bodyless requests have a known length */
3205 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003206
Willy Tarreau179085c2014-04-28 16:48:56 +02003207 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3208 * only change if both the request and the config reference something else.
3209 * Option httpclose by itself sets tunnel mode where headers are mangled.
3210 * However, if another mode is set, it will affect it (eg: server-close/
3211 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3212 * if FE and BE have the same settings (common). The method consists in
3213 * checking if options changed between the two calls (implying that either
3214 * one is non-null, or one of them is non-null and we are there for the first
3215 * time.
3216 */
3217 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003218 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003219 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003220
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003221 /* we may have to wait for the request's body */
3222 if ((s->be->options & PR_O_WREQ_BODY) &&
3223 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3224 req->analysers |= AN_REQ_HTTP_BODY;
3225
Willy Tarreaud787e662009-07-07 10:14:51 +02003226 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003227 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003228 req->analyse_exp = TICK_ETERNITY;
3229 return 1;
3230
3231 return_bad_req:
3232 /* We centralize bad requests processing here */
3233 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3234 /* we detected a parsing error. We want to archive this request
3235 * in the dedicated proxy area for later troubleshooting.
3236 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003237 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003238 }
3239
3240 txn->req.msg_state = HTTP_MSG_ERROR;
3241 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01003242 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003243
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003244 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003245 if (sess->listener->counters)
3246 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003247
3248 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003249 if (!(s->flags & SF_ERR_MASK))
3250 s->flags |= SF_ERR_PRXCOND;
3251 if (!(s->flags & SF_FINST_MASK))
3252 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003253
3254 req->analysers = 0;
3255 req->analyse_exp = TICK_ETERNITY;
3256 return 0;
3257}
3258
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003259
Willy Tarreau347a35d2013-11-22 17:51:09 +01003260/* This function prepares an applet to handle the stats. It can deal with the
3261 * "100-continue" expectation, check that admin rules are met for POST requests,
3262 * and program a response message if something was unexpected. It cannot fail
3263 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003264 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003265 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003266 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003267 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003268int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003269{
3270 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003271 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003272 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003273 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003274 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003275 struct uri_auth *uri_auth = s->be->uri_auth;
3276 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003277 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003278
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003279 appctx = si_appctx(si);
3280 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3281 appctx->st1 = appctx->st2 = 0;
3282 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3283 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003284 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003285 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003286
3287 uri = msg->chn->buf->p + msg->sl.rq.u;
3288 lookup = uri + uri_auth->uri_len;
3289
3290 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3291 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003292 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003293 break;
3294 }
3295 }
3296
3297 if (uri_auth->refresh) {
3298 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3299 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003300 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003301 break;
3302 }
3303 }
3304 }
3305
3306 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3307 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003308 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003309 break;
3310 }
3311 }
3312
3313 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3314 if (memcmp(h, ";st=", 4) == 0) {
3315 int i;
3316 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003317 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003318 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3319 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003320 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003321 break;
3322 }
3323 }
3324 break;
3325 }
3326 }
3327
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003328 appctx->ctx.stats.scope_str = 0;
3329 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003330 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3331 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3332 int itx = 0;
3333 const char *h2;
3334 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3335 const char *err;
3336
3337 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3338 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003339 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003340 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3341 itx++;
3342 h++;
3343 }
3344
3345 if (itx > STAT_SCOPE_TXT_MAXLEN)
3346 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003347 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003348
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003349 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003350 memcpy(scope_txt, h2, itx);
3351 scope_txt[itx] = '\0';
3352 err = invalid_char(scope_txt);
3353 if (err) {
3354 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003355 appctx->ctx.stats.scope_str = 0;
3356 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003357 }
3358 break;
3359 }
3360 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003361
3362 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003363 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003364 int ret = 1;
3365
3366 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003367 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003368 ret = acl_pass(ret);
3369 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3370 ret = !ret;
3371 }
3372
3373 if (ret) {
3374 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003375 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003376 break;
3377 }
3378 }
3379
3380 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003381 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003382 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003383 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003384 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3385 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003386 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003387 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003388 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003389 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3390 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003391 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003392 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003393 else {
3394 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003395 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003396 }
3397
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003398 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003399 return 1;
3400}
3401
Lukas Tribus67db8df2013-06-23 17:37:13 +02003402/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3403 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3404 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003405void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003406{
3407#ifdef IP_TOS
3408 if (from.ss_family == AF_INET)
3409 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3410#endif
3411#ifdef IPV6_TCLASS
3412 if (from.ss_family == AF_INET6) {
3413 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3414 /* v4-mapped addresses need IP_TOS */
3415 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3416 else
3417 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3418 }
3419#endif
3420}
3421
Willy Tarreau87b09662015-04-03 00:22:06 +02003422int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003423 const char* name, unsigned int name_len,
3424 const char *str, struct my_regex *re,
3425 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003426{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003427 struct hdr_ctx ctx;
3428 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003429 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003430 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3431 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003432 struct chunk *output = get_trash_chunk();
3433
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003434 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003435
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003436 /* Choose the header browsing function. */
3437 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003438 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003439 http_find_hdr_func = http_find_header2;
3440 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003441 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003442 http_find_hdr_func = http_find_full_header2;
3443 break;
3444 default: /* impossible */
3445 return -1;
3446 }
3447
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003448 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3449 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003450 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003451 char *val = ctx.line + ctx.val;
3452 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003453
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003454 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3455 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003456
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003457 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003458 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003459 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003460
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003461 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003462
3463 hdr->len += delta;
3464 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003465
3466 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003467 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003468 }
3469
3470 return 0;
3471}
3472
Willy Tarreau87b09662015-04-03 00:22:06 +02003473static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003474 const char* name, unsigned int name_len,
3475 struct list *fmt, struct my_regex *re,
3476 int action)
3477{
3478 struct chunk *replace = get_trash_chunk();
3479
3480 replace->len = build_logline(s, replace->str, replace->size, fmt);
3481 if (replace->len >= replace->size - 1)
3482 return -1;
3483
3484 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3485}
3486
Willy Tarreau87b09662015-04-03 00:22:06 +02003487/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003488 * transaction <txn>. Returns the verdict of the first rule that prevents
3489 * further processing of the request (auth, deny, ...), and defaults to
3490 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3491 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3492 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003493 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003494enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003495http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003496{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003497 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003498 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003499 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003500 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003501 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003502 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003503 int act_flags = 0;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003504
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003505 /* If "the current_rule_list" match the executed rule list, we are in
3506 * resume condition. If a resume is needed it is always in the action
3507 * and never in the ACL or converters. In this case, we initialise the
3508 * current rule, and go to the action execution point.
3509 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003510 if (s->current_rule) {
3511 rule = s->current_rule;
3512 s->current_rule = NULL;
3513 if (s->current_rule_list == rules)
3514 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003515 }
3516 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003517
Willy Tarreauff011f22011-01-06 17:51:27 +01003518 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003519
Willy Tarreau96257ec2012-12-27 10:46:37 +01003520 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003521 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003522 int ret;
3523
Willy Tarreau192252e2015-04-04 01:47:55 +02003524 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003525 ret = acl_pass(ret);
3526
Willy Tarreauff011f22011-01-06 17:51:27 +01003527 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003528 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003529
3530 if (!ret) /* condition not matched */
3531 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003532 }
3533
Willy Tarreauacc98002015-09-27 23:34:39 +02003534 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003535resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003536 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003537 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003538 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003539
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003540 case ACT_ACTION_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003541 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003542 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003543
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003544 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003545 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003546 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003547 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003548
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003549 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003550 /* Auth might be performed on regular http-req rules as well as on stats */
3551 auth_realm = rule->arg.auth.realm;
3552 if (!auth_realm) {
3553 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3554 auth_realm = STATS_DEFAULT_REALM;
3555 else
3556 auth_realm = px->id;
3557 }
3558 /* send 401/407 depending on whether we use a proxy or not. We still
3559 * count one error, because normal browsing won't significantly
3560 * increase the counter but brute force attempts will.
3561 */
3562 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3563 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3564 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003565 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003566 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003567
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003568 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003569 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3570 return HTTP_RULE_RES_BADREQ;
3571 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003572
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003573 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003574 s->task->nice = rule->arg.nice;
3575 break;
3576
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003577 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003578 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003579 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003580 break;
3581
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003582 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003583#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003584 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003585 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003586#endif
3587 break;
3588
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003589 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003590 s->logs.level = rule->arg.loglevel;
3591 break;
3592
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003593 case ACT_HTTP_REPLACE_HDR:
3594 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003595 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3596 rule->arg.hdr_add.name_len,
3597 &rule->arg.hdr_add.fmt,
3598 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003599 return HTTP_RULE_RES_BADREQ;
3600 break;
3601
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003602 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003603 ctx.idx = 0;
3604 /* remove all occurrences of the header */
3605 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3606 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3607 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003608 }
Willy Tarreau85603282015-01-21 20:39:27 +01003609 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003610
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003611 case ACT_HTTP_SET_HDR:
3612 case ACT_HTTP_ADD_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003613 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3614 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3615 trash.len = rule->arg.hdr_add.name_len;
3616 trash.str[trash.len++] = ':';
3617 trash.str[trash.len++] = ' ';
3618 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01003619
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003620 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003621 /* remove all occurrences of the header */
3622 ctx.idx = 0;
3623 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3624 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3625 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3626 }
3627 }
3628
Willy Tarreau96257ec2012-12-27 10:46:37 +01003629 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3630 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003631
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003632 case ACT_HTTP_DEL_ACL:
3633 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003634 struct pat_ref *ref;
3635 char *key;
3636 int len;
3637
3638 /* collect reference */
3639 ref = pat_ref_lookup(rule->arg.map.ref);
3640 if (!ref)
3641 continue;
3642
3643 /* collect key */
3644 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3645 key = trash.str;
3646 key[len] = '\0';
3647
3648 /* perform update */
3649 /* returned code: 1=ok, 0=ko */
3650 pat_ref_delete(ref, key);
3651
3652 break;
3653 }
3654
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003655 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003656 struct pat_ref *ref;
3657 char *key;
3658 struct chunk *trash_key;
3659 int len;
3660
3661 trash_key = get_trash_chunk();
3662
3663 /* collect reference */
3664 ref = pat_ref_lookup(rule->arg.map.ref);
3665 if (!ref)
3666 continue;
3667
3668 /* collect key */
3669 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3670 key = trash_key->str;
3671 key[len] = '\0';
3672
3673 /* perform update */
3674 /* add entry only if it does not already exist */
3675 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003676 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003677
3678 break;
3679 }
3680
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003681 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003682 struct pat_ref *ref;
3683 char *key, *value;
3684 struct chunk *trash_key, *trash_value;
3685 int len;
3686
3687 trash_key = get_trash_chunk();
3688 trash_value = get_trash_chunk();
3689
3690 /* collect reference */
3691 ref = pat_ref_lookup(rule->arg.map.ref);
3692 if (!ref)
3693 continue;
3694
3695 /* collect key */
3696 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3697 key = trash_key->str;
3698 key[len] = '\0';
3699
3700 /* collect value */
3701 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3702 value = trash_value->str;
3703 value[len] = '\0';
3704
3705 /* perform update */
3706 if (pat_ref_find_elt(ref, key) != NULL)
3707 /* update entry if it exists */
3708 pat_ref_set(ref, key, value, NULL);
3709 else
3710 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003711 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003712
3713 break;
3714 }
William Lallemand73025dd2014-04-24 14:38:37 +02003715
Thierry FOURNIER42148732015-09-02 17:17:33 +02003716 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003717 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3718 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003719
Willy Tarreauacc98002015-09-27 23:34:39 +02003720 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003721 case ACT_RET_ERR:
3722 case ACT_RET_CONT:
3723 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003724 case ACT_RET_STOP:
3725 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003726 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003727 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003728 return HTTP_RULE_RES_YIELD;
3729 }
William Lallemand73025dd2014-04-24 14:38:37 +02003730 break;
3731
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003732 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003733 /* Note: only the first valid tracking parameter of each
3734 * applies.
3735 */
3736
3737 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3738 struct stktable *t;
3739 struct stksess *ts;
3740 struct stktable_key *key;
3741 void *ptr;
3742
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003743 t = rule->arg.trk_ctr.table.t;
3744 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
Willy Tarreau09448f72014-06-25 18:12:15 +02003745
3746 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003747 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003748
3749 /* let's count a new HTTP request as it's the first time we do it */
3750 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3751 if (ptr)
3752 stktable_data_cast(ptr, http_req_cnt)++;
3753
3754 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3755 if (ptr)
3756 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3757 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3758
3759 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003760 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003761 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3762 }
3763 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003764 break;
3765
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003766 case ACT_HTTP_REQ_SET_SRC:
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003767 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
3768 struct sample *smp;
3769
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02003770 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003771
3772 if (smp) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003773 if (smp->data.type == SMP_T_IPV4) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003774 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003775 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003776 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003777 } else if (smp->data.type == SMP_T_IPV6) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003778 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003779 memcpy(&((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003780 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0;
3781 }
3782 }
3783 }
3784 break;
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003785
3786 /* other flags exists, but normaly, they never be matched. */
3787 default:
3788 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003789 }
3790 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003791
3792 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003793 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003794}
3795
Willy Tarreau71241ab2012-12-27 11:30:54 +01003796
Willy Tarreau51d861a2015-05-22 17:30:48 +02003797/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3798 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3799 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3800 * is returned, the process can continue the evaluation of next rule list. If
3801 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3802 * is returned, it means the operation could not be processed and a server error
3803 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3804 * deny rule. If *YIELD is returned, the caller must call again the function
3805 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003806 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003807static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003808http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003809{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003810 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003811 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003812 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003813 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003814 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003815 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003816
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003817 /* If "the current_rule_list" match the executed rule list, we are in
3818 * resume condition. If a resume is needed it is always in the action
3819 * and never in the ACL or converters. In this case, we initialise the
3820 * current rule, and go to the action execution point.
3821 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003822 if (s->current_rule) {
3823 rule = s->current_rule;
3824 s->current_rule = NULL;
3825 if (s->current_rule_list == rules)
3826 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003827 }
3828 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003829
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003830 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003831
3832 /* check optional condition */
3833 if (rule->cond) {
3834 int ret;
3835
Willy Tarreau192252e2015-04-04 01:47:55 +02003836 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003837 ret = acl_pass(ret);
3838
3839 if (rule->cond->pol == ACL_COND_UNLESS)
3840 ret = !ret;
3841
3842 if (!ret) /* condition not matched */
3843 continue;
3844 }
3845
Willy Tarreauacc98002015-09-27 23:34:39 +02003846 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003847resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003848 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003849 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003850 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003851
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003852 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003853 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003854 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003855
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003856 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003857 s->task->nice = rule->arg.nice;
3858 break;
3859
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003860 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003861 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003862 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003863 break;
3864
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003865 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003866#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003867 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003868 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003869#endif
3870 break;
3871
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003872 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003873 s->logs.level = rule->arg.loglevel;
3874 break;
3875
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003876 case ACT_HTTP_REPLACE_HDR:
3877 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003878 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3879 rule->arg.hdr_add.name_len,
3880 &rule->arg.hdr_add.fmt,
3881 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003882 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003883 break;
3884
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003885 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003886 ctx.idx = 0;
3887 /* remove all occurrences of the header */
3888 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3889 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3890 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3891 }
Willy Tarreau85603282015-01-21 20:39:27 +01003892 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003893
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003894 case ACT_HTTP_SET_HDR:
3895 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003896 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3897 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3898 trash.len = rule->arg.hdr_add.name_len;
3899 trash.str[trash.len++] = ':';
3900 trash.str[trash.len++] = ' ';
3901 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01003902
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003903 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003904 /* remove all occurrences of the header */
3905 ctx.idx = 0;
3906 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3907 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3908 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3909 }
3910 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003911 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3912 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003913
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003914 case ACT_HTTP_DEL_ACL:
3915 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003916 struct pat_ref *ref;
3917 char *key;
3918 int len;
3919
3920 /* collect reference */
3921 ref = pat_ref_lookup(rule->arg.map.ref);
3922 if (!ref)
3923 continue;
3924
3925 /* collect key */
3926 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3927 key = trash.str;
3928 key[len] = '\0';
3929
3930 /* perform update */
3931 /* returned code: 1=ok, 0=ko */
3932 pat_ref_delete(ref, key);
3933
3934 break;
3935 }
3936
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003937 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003938 struct pat_ref *ref;
3939 char *key;
3940 struct chunk *trash_key;
3941 int len;
3942
3943 trash_key = get_trash_chunk();
3944
3945 /* collect reference */
3946 ref = pat_ref_lookup(rule->arg.map.ref);
3947 if (!ref)
3948 continue;
3949
3950 /* collect key */
3951 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3952 key = trash_key->str;
3953 key[len] = '\0';
3954
3955 /* perform update */
3956 /* check if the entry already exists */
3957 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003958 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003959
3960 break;
3961 }
3962
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003963 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003964 struct pat_ref *ref;
3965 char *key, *value;
3966 struct chunk *trash_key, *trash_value;
3967 int len;
3968
3969 trash_key = get_trash_chunk();
3970 trash_value = get_trash_chunk();
3971
3972 /* collect reference */
3973 ref = pat_ref_lookup(rule->arg.map.ref);
3974 if (!ref)
3975 continue;
3976
3977 /* collect key */
3978 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3979 key = trash_key->str;
3980 key[len] = '\0';
3981
3982 /* collect value */
3983 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3984 value = trash_value->str;
3985 value[len] = '\0';
3986
3987 /* perform update */
3988 if (pat_ref_find_elt(ref, key) != NULL)
3989 /* update entry if it exists */
3990 pat_ref_set(ref, key, value, NULL);
3991 else
3992 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003993 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003994
3995 break;
3996 }
William Lallemand73025dd2014-04-24 14:38:37 +02003997
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003998 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003999 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
4000 return HTTP_RULE_RES_BADREQ;
4001 return HTTP_RULE_RES_DONE;
4002
Thierry FOURNIER42148732015-09-02 17:17:33 +02004003 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02004004 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
4005 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02004006
Willy Tarreauacc98002015-09-27 23:34:39 +02004007 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004008 case ACT_RET_ERR:
4009 case ACT_RET_CONT:
4010 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02004011 case ACT_RET_STOP:
4012 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004013 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02004014 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004015 return HTTP_RULE_RES_YIELD;
4016 }
William Lallemand73025dd2014-04-24 14:38:37 +02004017 break;
4018
Thierry FOURNIER22e49012015-08-05 19:13:48 +02004019 /* other flags exists, but normaly, they never be matched. */
4020 default:
4021 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004022 }
4023 }
4024
4025 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004026 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004027}
4028
4029
Willy Tarreau71241ab2012-12-27 11:30:54 +01004030/* Perform an HTTP redirect based on the information in <rule>. The function
4031 * returns non-zero on success, or zero in case of a, irrecoverable error such
4032 * as too large a request to build a valid response.
4033 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004034static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004035{
Willy Tarreaub329a312015-05-22 16:27:37 +02004036 struct http_msg *req = &txn->req;
4037 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004038 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004039 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004040
4041 /* build redirect message */
4042 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004043 case 308:
4044 msg_fmt = HTTP_308;
4045 break;
4046 case 307:
4047 msg_fmt = HTTP_307;
4048 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004049 case 303:
4050 msg_fmt = HTTP_303;
4051 break;
4052 case 301:
4053 msg_fmt = HTTP_301;
4054 break;
4055 case 302:
4056 default:
4057 msg_fmt = HTTP_302;
4058 break;
4059 }
4060
4061 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
4062 return 0;
4063
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004064 location = trash.str + trash.len;
4065
Willy Tarreau71241ab2012-12-27 11:30:54 +01004066 switch(rule->type) {
4067 case REDIRECT_TYPE_SCHEME: {
4068 const char *path;
4069 const char *host;
4070 struct hdr_ctx ctx;
4071 int pathlen;
4072 int hostlen;
4073
4074 host = "";
4075 hostlen = 0;
4076 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004077 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004078 host = ctx.line + ctx.val;
4079 hostlen = ctx.vlen;
4080 }
4081
4082 path = http_get_path(txn);
4083 /* build message using path */
4084 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004085 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004086 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4087 int qs = 0;
4088 while (qs < pathlen) {
4089 if (path[qs] == '?') {
4090 pathlen = qs;
4091 break;
4092 }
4093 qs++;
4094 }
4095 }
4096 } else {
4097 path = "/";
4098 pathlen = 1;
4099 }
4100
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004101 if (rule->rdr_str) { /* this is an old "redirect" rule */
4102 /* check if we can add scheme + "://" + host + path */
4103 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
4104 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004105
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004106 /* add scheme */
4107 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4108 trash.len += rule->rdr_len;
4109 }
4110 else {
4111 /* add scheme with executing log format */
4112 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004113
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004114 /* check if we can add scheme + "://" + host + path */
4115 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
4116 return 0;
4117 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004118 /* add "://" */
4119 memcpy(trash.str + trash.len, "://", 3);
4120 trash.len += 3;
4121
4122 /* add host */
4123 memcpy(trash.str + trash.len, host, hostlen);
4124 trash.len += hostlen;
4125
4126 /* add path */
4127 memcpy(trash.str + trash.len, path, pathlen);
4128 trash.len += pathlen;
4129
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004130 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004131 if (trash.len && trash.str[trash.len - 1] != '/' &&
4132 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4133 if (trash.len > trash.size - 5)
4134 return 0;
4135 trash.str[trash.len] = '/';
4136 trash.len++;
4137 }
4138
4139 break;
4140 }
4141 case REDIRECT_TYPE_PREFIX: {
4142 const char *path;
4143 int pathlen;
4144
4145 path = http_get_path(txn);
4146 /* build message using path */
4147 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004148 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004149 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4150 int qs = 0;
4151 while (qs < pathlen) {
4152 if (path[qs] == '?') {
4153 pathlen = qs;
4154 break;
4155 }
4156 qs++;
4157 }
4158 }
4159 } else {
4160 path = "/";
4161 pathlen = 1;
4162 }
4163
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004164 if (rule->rdr_str) { /* this is an old "redirect" rule */
4165 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4166 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004167
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004168 /* add prefix. Note that if prefix == "/", we don't want to
4169 * add anything, otherwise it makes it hard for the user to
4170 * configure a self-redirection.
4171 */
4172 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4173 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4174 trash.len += rule->rdr_len;
4175 }
4176 }
4177 else {
4178 /* add prefix with executing log format */
4179 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4180
4181 /* Check length */
4182 if (trash.len + pathlen > trash.size - 4)
4183 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004184 }
4185
4186 /* add path */
4187 memcpy(trash.str + trash.len, path, pathlen);
4188 trash.len += pathlen;
4189
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004190 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004191 if (trash.len && trash.str[trash.len - 1] != '/' &&
4192 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4193 if (trash.len > trash.size - 5)
4194 return 0;
4195 trash.str[trash.len] = '/';
4196 trash.len++;
4197 }
4198
4199 break;
4200 }
4201 case REDIRECT_TYPE_LOCATION:
4202 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004203 if (rule->rdr_str) { /* this is an old "redirect" rule */
4204 if (trash.len + rule->rdr_len > trash.size - 4)
4205 return 0;
4206
4207 /* add location */
4208 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4209 trash.len += rule->rdr_len;
4210 }
4211 else {
4212 /* add location with executing log format */
4213 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004214
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004215 /* Check left length */
4216 if (trash.len > trash.size - 4)
4217 return 0;
4218 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004219 break;
4220 }
4221
4222 if (rule->cookie_len) {
4223 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4224 trash.len += 14;
4225 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4226 trash.len += rule->cookie_len;
4227 memcpy(trash.str + trash.len, "\r\n", 2);
4228 trash.len += 2;
4229 }
4230
4231 /* add end of headers and the keep-alive/close status.
4232 * We may choose to set keep-alive if the Location begins
4233 * with a slash, because the client will come back to the
4234 * same server.
4235 */
4236 txn->status = rule->code;
4237 /* let's log the request time */
4238 s->logs.tv_request = now;
4239
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004240 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004241 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004242 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004243 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4244 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4245 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004246 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004247 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4248 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4249 trash.len += 30;
4250 } else {
4251 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4252 trash.len += 24;
4253 }
4254 }
4255 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4256 trash.len += 4;
Willy Tarreaub329a312015-05-22 16:27:37 +02004257 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004258 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004259 bi_fast_delete(req->chn->buf, req->sov);
4260 req->next -= req->sov;
4261 req->sov = 0;
4262 s->req.analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004263 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreaub329a312015-05-22 16:27:37 +02004264 req->msg_state = HTTP_MSG_CLOSED;
4265 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004266 /* Trim any possible response */
4267 res->chn->buf->i = 0;
4268 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004269 } else {
4270 /* keep-alive not possible */
4271 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4272 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4273 trash.len += 29;
4274 } else {
4275 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4276 trash.len += 23;
4277 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004278 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreaub329a312015-05-22 16:27:37 +02004279 req->chn->analysers = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004280 }
4281
Willy Tarreaue7dff022015-04-03 01:14:29 +02004282 if (!(s->flags & SF_ERR_MASK))
4283 s->flags |= SF_ERR_LOCAL;
4284 if (!(s->flags & SF_FINST_MASK))
4285 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004286
4287 return 1;
4288}
4289
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004290/* This stream analyser runs all HTTP request processing which is common to
4291 * frontends and backends, which means blocking ACLs, filters, connection-close,
4292 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004293 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004294 * either needs more data or wants to immediately abort the request (eg: deny,
4295 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004296 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004297int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004298{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004299 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004300 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004301 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004302 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004303 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004304 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004305
Willy Tarreau655dce92009-11-08 13:10:58 +01004306 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004307 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004308 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004309 }
4310
Willy Tarreau87b09662015-04-03 00:22:06 +02004311 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02004312 now_ms, __FUNCTION__,
4313 s,
4314 req,
4315 req->rex, req->wex,
4316 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004317 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004318 req->analysers);
4319
Willy Tarreau65410832014-04-28 21:25:43 +02004320 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004321 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004322
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004323 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004324 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004325 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004326
Willy Tarreau0b748332014-04-29 00:13:29 +02004327 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004328 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4329 goto return_prx_yield;
4330
Willy Tarreau0b748332014-04-29 00:13:29 +02004331 case HTTP_RULE_RES_CONT:
4332 case HTTP_RULE_RES_STOP: /* nothing to do */
4333 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004334
Willy Tarreau0b748332014-04-29 00:13:29 +02004335 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4336 if (txn->flags & TX_CLTARPIT)
4337 goto tarpit;
4338 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004339
Willy Tarreau0b748332014-04-29 00:13:29 +02004340 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4341 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004342
Willy Tarreau0b748332014-04-29 00:13:29 +02004343 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004344 goto done;
4345
Willy Tarreau0b748332014-04-29 00:13:29 +02004346 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4347 goto return_bad_req;
4348 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004349 }
4350
Willy Tarreau52542592014-04-28 18:33:26 +02004351 /* OK at this stage, we know that the request was accepted according to
4352 * the http-request rules, we can check for the stats. Note that the
4353 * URI is detected *before* the req* rules in order not to be affected
4354 * by a possible reqrep, while they are processed *after* so that a
4355 * reqdeny can still block them. This clearly needs to change in 1.6!
4356 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004357 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004358 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004359 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004360 txn->status = 500;
4361 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004362 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004363
Willy Tarreaue7dff022015-04-03 01:14:29 +02004364 if (!(s->flags & SF_ERR_MASK))
4365 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004366 goto return_prx_cond;
4367 }
4368
4369 /* parse the whole stats request and extract the relevant information */
4370 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004371 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004372 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004373
Willy Tarreau0b748332014-04-29 00:13:29 +02004374 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4375 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004376
Willy Tarreau0b748332014-04-29 00:13:29 +02004377 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4378 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004379 }
4380
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004381 /* evaluate the req* rules except reqadd */
4382 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004383 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004384 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004385
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004386 if (txn->flags & TX_CLDENY)
4387 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004388
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004389 if (txn->flags & TX_CLTARPIT)
4390 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004391 }
Willy Tarreau06619262006-12-17 08:37:22 +01004392
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004393 /* add request headers from the rule sets in the same order */
4394 list_for_each_entry(wl, &px->req_add, list) {
4395 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004396 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004397 ret = acl_pass(ret);
4398 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4399 ret = !ret;
4400 if (!ret)
4401 continue;
4402 }
4403
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004404 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004405 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004406 }
4407
Willy Tarreau52542592014-04-28 18:33:26 +02004408
4409 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004410 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004411 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004412 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4413 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004414
Willy Tarreaue7dff022015-04-03 01:14:29 +02004415 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4416 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4417 if (!(s->flags & SF_FINST_MASK))
4418 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004419
Willy Tarreau70730dd2014-04-24 18:06:27 +02004420 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004421 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004422 select_compression_request_header(s, req->buf);
4423
4424 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004425 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004426 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004427 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004428
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004429 /* check whether we have some ACLs set to redirect this request */
4430 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004431 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004432 int ret;
4433
Willy Tarreau192252e2015-04-04 01:47:55 +02004434 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004435 ret = acl_pass(ret);
4436 if (rule->cond->pol == ACL_COND_UNLESS)
4437 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004438 if (!ret)
4439 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004440 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004441 if (!http_apply_redirect_rule(rule, s, txn))
4442 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004443 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004444 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004445
Willy Tarreau2be39392010-01-03 17:24:51 +01004446 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4447 * If this happens, then the data will not come immediately, so we must
4448 * send all what we have without waiting. Note that due to the small gain
4449 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004450 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004451 * itself once used.
4452 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004453 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004454
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004455 done: /* done with this analyser, continue with next ones that the calling
4456 * points will have set, if any.
4457 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004458 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004459 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4460 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004461 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004462
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004463 tarpit:
4464 /* When a connection is tarpitted, we use the tarpit timeout,
4465 * which may be the same as the connect timeout if unspecified.
4466 * If unset, then set it to zero because we really want it to
4467 * eventually expire. We build the tarpit as an analyser.
4468 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004469 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004470
4471 /* wipe the request out so that we can drop the connection early
4472 * if the client closes first.
4473 */
4474 channel_dont_connect(req);
4475 req->analysers = 0; /* remove switching rules etc... */
4476 req->analysers |= AN_REQ_HTTP_TARPIT;
4477 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4478 if (!req->analyse_exp)
4479 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004480 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004481 sess->fe->fe_counters.denied_req++;
4482 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004483 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004484 if (sess->listener->counters)
4485 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004486 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004487
4488 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004489 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004490 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004491 s->logs.tv_request = now;
CJ Ess108b1dd2015-04-07 12:03:37 -04004492 stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004493 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004494 sess->fe->fe_counters.denied_req++;
4495 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004496 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004497 if (sess->listener->counters)
4498 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004499 goto return_prx_cond;
4500
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004501 return_bad_req:
4502 /* We centralize bad requests processing here */
4503 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4504 /* we detected a parsing error. We want to archive this request
4505 * in the dedicated proxy area for later troubleshooting.
4506 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004507 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004508 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004509
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004510 txn->req.msg_state = HTTP_MSG_ERROR;
4511 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004512 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004513
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004514 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004515 if (sess->listener->counters)
4516 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004517
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004518 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004519 if (!(s->flags & SF_ERR_MASK))
4520 s->flags |= SF_ERR_PRXCOND;
4521 if (!(s->flags & SF_FINST_MASK))
4522 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004523
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004524 req->analysers = 0;
4525 req->analyse_exp = TICK_ETERNITY;
4526 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004527
4528 return_prx_yield:
4529 channel_dont_connect(req);
4530 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004531}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004532
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004533/* This function performs all the processing enabled for the current request.
4534 * It returns 1 if the processing can continue on next analysers, or zero if it
4535 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004536 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004537 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004538int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004539{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004540 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004541 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004542 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004543 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004544
Willy Tarreau655dce92009-11-08 13:10:58 +01004545 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004546 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004547 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004548 return 0;
4549 }
4550
Willy Tarreau87b09662015-04-03 00:22:06 +02004551 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004552 now_ms, __FUNCTION__,
4553 s,
4554 req,
4555 req->rex, req->wex,
4556 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004557 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004558 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004559
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004560 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004561 select_compression_request_header(s, req->buf);
4562
Willy Tarreau59234e92008-11-30 23:51:27 +01004563 /*
4564 * Right now, we know that we have processed the entire headers
4565 * and that unwanted requests have been filtered out. We can do
4566 * whatever we want with the remaining request. Also, now we
4567 * may have separate values for ->fe, ->be.
4568 */
Willy Tarreau06619262006-12-17 08:37:22 +01004569
Willy Tarreau59234e92008-11-30 23:51:27 +01004570 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004571 * If HTTP PROXY is set we simply get remote server address parsing
4572 * incoming request. Note that this requires that a connection is
4573 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004574 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004575 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004576 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004577 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004578
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004579 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004580 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004581 txn->req.msg_state = HTTP_MSG_ERROR;
4582 txn->status = 500;
4583 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004584 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004585
Willy Tarreaue7dff022015-04-03 01:14:29 +02004586 if (!(s->flags & SF_ERR_MASK))
4587 s->flags |= SF_ERR_RESOURCE;
4588 if (!(s->flags & SF_FINST_MASK))
4589 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004590
4591 return 0;
4592 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004593
4594 path = http_get_path(txn);
4595 url2sa(req->buf->p + msg->sl.rq.u,
4596 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004597 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004598 /* if the path was found, we have to remove everything between
4599 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4600 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4601 * u_l characters by a single "/".
4602 */
4603 if (path) {
4604 char *cur_ptr = req->buf->p;
4605 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4606 int delta;
4607
4608 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4609 http_msg_move_end(&txn->req, delta);
4610 cur_end += delta;
4611 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4612 goto return_bad_req;
4613 }
4614 else {
4615 char *cur_ptr = req->buf->p;
4616 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4617 int delta;
4618
4619 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4620 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4621 http_msg_move_end(&txn->req, delta);
4622 cur_end += delta;
4623 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4624 goto return_bad_req;
4625 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004626 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004627
Willy Tarreau59234e92008-11-30 23:51:27 +01004628 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004629 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004630 * Note that doing so might move headers in the request, but
4631 * the fields will stay coherent and the URI will not move.
4632 * This should only be performed in the backend.
4633 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02004634 if ((s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004635 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4636 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004637
William Lallemanda73203e2012-03-12 12:48:57 +01004638 /* add unique-id if "header-unique-id" is specified */
4639
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004640 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004641 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4642 goto return_bad_req;
4643 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004644 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004645 }
William Lallemanda73203e2012-03-12 12:48:57 +01004646
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004647 if (sess->fe->header_unique_id && s->unique_id) {
4648 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004649 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004650 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004651 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004652 goto return_bad_req;
4653 }
4654
Cyril Bontéb21570a2009-11-29 20:04:48 +01004655 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004656 * 9: add X-Forwarded-For if either the frontend or the backend
4657 * asks for it.
4658 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004659 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004660 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004661 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4662 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4663 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004664 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004665 /* The header is set to be added only if none is present
4666 * and we found it, so don't do anything.
4667 */
4668 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004669 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004670 /* Add an X-Forwarded-For header unless the source IP is
4671 * in the 'except' network range.
4672 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004673 if ((!sess->fe->except_mask.s_addr ||
4674 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4675 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004676 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004677 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004678 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004679 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004680 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004681 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004682
4683 /* Note: we rely on the backend to get the header name to be used for
4684 * x-forwarded-for, because the header is really meant for the backends.
4685 * However, if the backend did not specify any option, we have to rely
4686 * on the frontend's header name.
4687 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004688 if (s->be->fwdfor_hdr_len) {
4689 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004690 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004691 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004692 len = sess->fe->fwdfor_hdr_len;
4693 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004694 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004695 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01004696
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004697 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004698 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004699 }
4700 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004701 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004702 /* FIXME: for the sake of completeness, we should also support
4703 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004704 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004705 int len;
4706 char pn[INET6_ADDRSTRLEN];
4707 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004708 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004709 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004710
Willy Tarreau59234e92008-11-30 23:51:27 +01004711 /* Note: we rely on the backend to get the header name to be used for
4712 * x-forwarded-for, because the header is really meant for the backends.
4713 * However, if the backend did not specify any option, we have to rely
4714 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004715 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004716 if (s->be->fwdfor_hdr_len) {
4717 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004718 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004719 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004720 len = sess->fe->fwdfor_hdr_len;
4721 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004722 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004723 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004724
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004725 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004726 goto return_bad_req;
4727 }
4728 }
4729
4730 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004731 * 10: add X-Original-To if either the frontend or the backend
4732 * asks for it.
4733 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004734 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004735
4736 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004737 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004738 /* Add an X-Original-To header unless the destination IP is
4739 * in the 'except' network range.
4740 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004741 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004742
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004743 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004744 ((!sess->fe->except_mask_to.s_addr ||
4745 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4746 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004747 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004748 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02004749 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004750 int len;
4751 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004752 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004753
4754 /* Note: we rely on the backend to get the header name to be used for
4755 * x-original-to, because the header is really meant for the backends.
4756 * However, if the backend did not specify any option, we have to rely
4757 * on the frontend's header name.
4758 */
4759 if (s->be->orgto_hdr_len) {
4760 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004761 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004762 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004763 len = sess->fe->orgto_hdr_len;
4764 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004765 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004766 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004767
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004768 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004769 goto return_bad_req;
4770 }
4771 }
4772 }
4773
Willy Tarreau50fc7772012-11-11 22:19:57 +01004774 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4775 * If an "Upgrade" token is found, the header is left untouched in order not to have
4776 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4777 * "Upgrade" is present in the Connection header.
4778 */
4779 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4780 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004781 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004782 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004783 unsigned int want_flags = 0;
4784
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004785 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004786 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004787 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004788 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004789 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004790 want_flags |= TX_CON_CLO_SET;
4791 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004792 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004793 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004794 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004795 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004796 want_flags |= TX_CON_KAL_SET;
4797 }
4798
4799 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004800 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004801 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004802
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004803
Willy Tarreau522d6c02009-12-06 18:49:18 +01004804 /* If we have no server assigned yet and we're balancing on url_param
4805 * with a POST request, we may be interested in checking the body for
4806 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004807 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004808 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004809 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004810 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004811 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004812 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004813 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004814
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004815 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004816 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004817#ifdef TCP_QUICKACK
4818 /* We expect some data from the client. Unless we know for sure
4819 * we already have a full request, we have to re-enable quick-ack
4820 * in case we previously disabled it, otherwise we might cause
4821 * the client to delay further data.
4822 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004823 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004824 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004825 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004826 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004827 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004828#endif
4829 }
Willy Tarreau03945942009-12-22 16:50:27 +01004830
Willy Tarreau59234e92008-11-30 23:51:27 +01004831 /*************************************************************
4832 * OK, that's finished for the headers. We have done what we *
4833 * could. Let's switch to the DATA state. *
4834 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004835 req->analyse_exp = TICK_ETERNITY;
4836 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004837
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004838 /* if the server closes the connection, we want to immediately react
4839 * and close the socket to save packets and syscalls.
4840 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004841 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004842 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004843
Willy Tarreau59234e92008-11-30 23:51:27 +01004844 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004845 /* OK let's go on with the BODY now */
4846 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004847
Willy Tarreau59234e92008-11-30 23:51:27 +01004848 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004849 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004850 /* we detected a parsing error. We want to archive this request
4851 * in the dedicated proxy area for later troubleshooting.
4852 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004853 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004854 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004855
Willy Tarreau59234e92008-11-30 23:51:27 +01004856 txn->req.msg_state = HTTP_MSG_ERROR;
4857 txn->status = 400;
4858 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004859 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004860
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004861 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004862 if (sess->listener->counters)
4863 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004864
Willy Tarreaue7dff022015-04-03 01:14:29 +02004865 if (!(s->flags & SF_ERR_MASK))
4866 s->flags |= SF_ERR_PRXCOND;
4867 if (!(s->flags & SF_FINST_MASK))
4868 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004869 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004870}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004871
Willy Tarreau60b85b02008-11-30 23:28:40 +01004872/* This function is an analyser which processes the HTTP tarpit. It always
4873 * returns zero, at the beginning because it prevents any other processing
4874 * from occurring, and at the end because it terminates the request.
4875 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004876int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004877{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004878 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004879
4880 /* This connection is being tarpitted. The CLIENT side has
4881 * already set the connect expiration date to the right
4882 * timeout. We just have to check that the client is still
4883 * there and that the timeout has not expired.
4884 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004885 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004886 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004887 !tick_is_expired(req->analyse_exp, now_ms))
4888 return 0;
4889
4890 /* We will set the queue timer to the time spent, just for
4891 * logging purposes. We fake a 500 server error, so that the
4892 * attacker will not suspect his connection has been tarpitted.
4893 * It will not cause trouble to the logs because we can exclude
4894 * the tarpitted connections by filtering on the 'PT' status flags.
4895 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004896 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4897
4898 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004899 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004900 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004901
4902 req->analysers = 0;
4903 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004904
Willy Tarreaue7dff022015-04-03 01:14:29 +02004905 if (!(s->flags & SF_ERR_MASK))
4906 s->flags |= SF_ERR_PRXCOND;
4907 if (!(s->flags & SF_FINST_MASK))
4908 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004909 return 0;
4910}
4911
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004912/* This function is an analyser which waits for the HTTP request body. It waits
4913 * for either the buffer to be full, or the full advertised contents to have
4914 * reached the buffer. It must only be called after the standard HTTP request
4915 * processing has occurred, because it expects the request to be parsed and will
4916 * look for the Expect header. It may send a 100-Continue interim response. It
4917 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4918 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4919 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004920 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004921int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004922{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004923 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004924 struct http_txn *txn = s->txn;
4925 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004926
4927 /* We have to parse the HTTP request body to find any required data.
4928 * "balance url_param check_post" should have been the only way to get
4929 * into this. We were brought here after HTTP header analysis, so all
4930 * related structures are ready.
4931 */
4932
Willy Tarreau890988f2014-04-10 11:59:33 +02004933 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4934 /* This is the first call */
4935 if (msg->msg_state < HTTP_MSG_BODY)
4936 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004937
Willy Tarreau890988f2014-04-10 11:59:33 +02004938 if (msg->msg_state < HTTP_MSG_100_SENT) {
4939 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4940 * send an HTTP/1.1 100 Continue intermediate response.
4941 */
4942 if (msg->flags & HTTP_MSGF_VER_11) {
4943 struct hdr_ctx ctx;
4944 ctx.idx = 0;
4945 /* Expect is allowed in 1.1, look for it */
4946 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4947 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004948 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004949 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004950 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004951 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004952 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004953
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004954 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004955 * req->buf->p still points to the beginning of the message. We
4956 * must save the body in msg->next because it survives buffer
4957 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004958 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004959 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004960
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004961 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004962 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4963 else
4964 msg->msg_state = HTTP_MSG_DATA;
4965 }
4966
Willy Tarreau890988f2014-04-10 11:59:33 +02004967 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4968 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004969 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004970 goto missing_data;
4971
4972 /* OK we have everything we need now */
4973 goto http_end;
4974 }
4975
4976 /* OK here we're parsing a chunked-encoded message */
4977
Willy Tarreau522d6c02009-12-06 18:49:18 +01004978 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004979 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004980 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004981 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004982 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004983 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004984
Willy Tarreau115acb92009-12-26 13:56:06 +01004985 if (!ret)
4986 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004987 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004988 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004989 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004990 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004991 }
4992
Willy Tarreaud98cf932009-12-27 22:54:55 +01004993 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004994 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4995 * for at least a whole chunk or the whole content length bytes after
4996 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004997 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004998 if (msg->msg_state == HTTP_MSG_TRAILERS)
4999 goto http_end;
5000
Willy Tarreaue115b492015-05-01 23:05:14 +02005001 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01005002 goto http_end;
5003
5004 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02005005 /* we get here if we need to wait for more data. If the buffer is full,
5006 * we have the maximum we can expect.
5007 */
5008 if (buffer_full(req->buf, global.tune.maxrewrite))
5009 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01005010
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005011 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01005012 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01005013 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02005014
Willy Tarreaue7dff022015-04-03 01:14:29 +02005015 if (!(s->flags & SF_ERR_MASK))
5016 s->flags |= SF_ERR_CLITO;
5017 if (!(s->flags & SF_FINST_MASK))
5018 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005019 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005020 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005021
5022 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005023 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005024 /* Not enough data. We'll re-use the http-request
5025 * timeout here. Ideally, we should set the timeout
5026 * relative to the accept() date. We just set the
5027 * request timeout once at the beginning of the
5028 * request.
5029 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005030 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005031 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005032 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005033 return 0;
5034 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005035
5036 http_end:
5037 /* The situation will not evolve, so let's give up on the analysis. */
5038 s->logs.tv_request = now; /* update the request timer to reflect full request */
5039 req->analysers &= ~an_bit;
5040 req->analyse_exp = TICK_ETERNITY;
5041 return 1;
5042
5043 return_bad_req: /* let's centralize all bad requests */
5044 txn->req.msg_state = HTTP_MSG_ERROR;
5045 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005046 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005047
Willy Tarreaue7dff022015-04-03 01:14:29 +02005048 if (!(s->flags & SF_ERR_MASK))
5049 s->flags |= SF_ERR_PRXCOND;
5050 if (!(s->flags & SF_FINST_MASK))
5051 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005052
Willy Tarreau522d6c02009-12-06 18:49:18 +01005053 return_err_msg:
5054 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005055 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005056 if (sess->listener->counters)
5057 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005058 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005059}
5060
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005061/* send a server's name with an outgoing request over an established connection.
5062 * Note: this function is designed to be called once the request has been scheduled
5063 * for being forwarded. This is the reason why it rewinds the buffer before
5064 * proceeding.
5065 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005066int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005067
5068 struct hdr_ctx ctx;
5069
Mark Lamourinec2247f02012-01-04 13:02:01 -05005070 char *hdr_name = be->server_id_hdr_name;
5071 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005072 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005073 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005074 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005075
William Lallemandd9e90662012-01-30 17:27:17 +01005076 ctx.idx = 0;
5077
Willy Tarreau211cdec2014-04-17 20:18:08 +02005078 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005079 if (old_o) {
5080 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005081 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005082 txn->req.next += old_o;
5083 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005084 }
5085
Willy Tarreau9b28e032012-10-12 23:49:43 +02005086 old_i = chn->buf->i;
5087 while (http_find_header2(hdr_name, hdr_name_len, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005088 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005089 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005090 }
5091
5092 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005093 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005094 memcpy(hdr_val, hdr_name, hdr_name_len);
5095 hdr_val += hdr_name_len;
5096 *hdr_val++ = ':';
5097 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005098 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5099 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005100
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005101 if (old_o) {
5102 /* If this was a forwarded request, we must readjust the amount of
5103 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005104 * variations. Note that the current state is >= HTTP_MSG_BODY,
5105 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005106 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005107 old_o += chn->buf->i - old_i;
5108 b_adv(chn->buf, old_o);
5109 txn->req.next -= old_o;
5110 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005111 }
5112
Mark Lamourinec2247f02012-01-04 13:02:01 -05005113 return 0;
5114}
5115
Willy Tarreau610ecce2010-01-04 21:15:02 +01005116/* Terminate current transaction and prepare a new one. This is very tricky
5117 * right now but it works.
5118 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005119void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005120{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005121 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005122 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01005123 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02005124 struct connection *srv_conn;
5125 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005126 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005127
Willy Tarreau610ecce2010-01-04 21:15:02 +01005128 /* FIXME: We need a more portable way of releasing a backend's and a
5129 * server's connections. We need a safer way to reinitialize buffer
5130 * flags. We also need a more accurate method for computing per-request
5131 * data.
5132 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005133 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005134
Willy Tarreau4213a112013-12-15 10:25:42 +01005135 /* unless we're doing keep-alive, we want to quickly close the connection
5136 * to the server.
5137 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005138 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005139 !si_conn_ready(&s->si[1])) {
5140 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5141 si_shutr(&s->si[1]);
5142 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005143 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005144
Willy Tarreaue7dff022015-04-03 01:14:29 +02005145 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01005146 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005147 if (unlikely(s->srv_conn))
5148 sess_change_server(s, NULL);
5149 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005150
5151 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005152 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005153
Willy Tarreaueee5b512015-04-03 23:46:31 +02005154 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005155 int n;
5156
Willy Tarreaueee5b512015-04-03 23:46:31 +02005157 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005158 if (n < 1 || n > 5)
5159 n = 0;
5160
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005161 if (fe->mode == PR_MODE_HTTP) {
5162 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005163 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005164 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005165 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005166 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01005167 (be->mode == PR_MODE_HTTP)) {
5168 be->be_counters.p.http.rsp[n]++;
5169 be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005170 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau858b1032015-12-07 17:04:59 +01005171 be->be_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005172 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005173 }
5174
5175 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005176 s->logs.bytes_in -= s->req.buf->i;
5177 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005178
5179 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005180 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005181 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005182 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005183 s->do_log(s);
5184 }
5185
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005186 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005187 stream_stop_content_counters(s);
5188 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005189
Willy Tarreau610ecce2010-01-04 21:15:02 +01005190 s->logs.accept_date = date; /* user-visible date for logging */
5191 s->logs.tv_accept = now; /* corrected date for internal use */
5192 tv_zero(&s->logs.tv_request);
5193 s->logs.t_queue = -1;
5194 s->logs.t_connect = -1;
5195 s->logs.t_data = -1;
5196 s->logs.t_close = 0;
5197 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5198 s->logs.srv_queue_size = 0; /* we will get this number soon */
5199
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005200 s->logs.bytes_in = s->req.total = s->req.buf->i;
5201 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005202
5203 if (s->pend_pos)
5204 pendconn_free(s->pend_pos);
5205
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005206 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005207 if (s->flags & SF_CURR_SESS) {
5208 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005209 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005210 }
Willy Tarreau858b1032015-12-07 17:04:59 +01005211 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005212 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005213 }
5214
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005215 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005216
Willy Tarreau4213a112013-12-15 10:25:42 +01005217 /* only release our endpoint if we don't intend to reuse the
5218 * connection.
5219 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005220 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005221 !si_conn_ready(&s->si[1])) {
5222 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005223 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005224 }
5225
Willy Tarreau350f4872014-11-28 14:42:25 +01005226 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5227 s->si[1].err_type = SI_ET_NONE;
5228 s->si[1].conn_retries = 0; /* used for logging too */
5229 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005230 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 +01005231 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);
5232 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 +02005233 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5234 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5235 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005236
Willy Tarreaueee5b512015-04-03 23:46:31 +02005237 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005238 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005239 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005240
5241 if (prev_status == 401 || prev_status == 407) {
5242 /* In HTTP keep-alive mode, if we receive a 401, we still have
5243 * a chance of being able to send the visitor again to the same
5244 * server over the same connection. This is required by some
5245 * broken protocols such as NTLM, and anyway whenever there is
5246 * an opportunity for sending the challenge to the proper place,
5247 * it's better to do it (at least it helps with debugging).
5248 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005249 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005250 if (srv_conn)
5251 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005252 }
5253
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005254 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005255 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005256
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005257 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005258 s->req.flags |= CF_NEVER_WAIT;
5259 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005260 }
5261
Willy Tarreau610ecce2010-01-04 21:15:02 +01005262 /* if the request buffer is not empty, it means we're
5263 * about to process another request, so send pending
5264 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005265 * Just don't do this if the buffer is close to be full,
5266 * because the request will wait for it to flush a little
5267 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005268 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005269 if (s->req.buf->i) {
5270 if (s->res.buf->o &&
5271 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5272 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5273 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005274 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005275
Willy Tarreau714ea782015-11-25 20:11:11 +01005276 /* we're removing the analysers, we MUST re-enable events detection.
5277 * We don't enable close on the response channel since it's either
5278 * already closed, or in keep-alive with an idle connection handler.
5279 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005280 channel_auto_read(&s->req);
5281 channel_auto_close(&s->req);
5282 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005283
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005284 /* we're in keep-alive with an idle connection, monitor it if not already done */
5285 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005286 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005287 if (!srv)
5288 si_idle_conn(&s->si[1], NULL);
5289 else if ((srv_conn->flags & CO_FL_PRIVATE) ||
Willy Tarreau858b1032015-12-07 17:04:59 +01005290 ((be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR))
Willy Tarreau8dff9982015-08-04 20:45:52 +02005291 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005292 else if (prev_flags & TX_NOT_FIRST)
5293 /* note: we check the request, not the connection, but
5294 * this is valid for strategies SAFE and AGGR, and in
5295 * case of ALWS, we don't care anyway.
5296 */
5297 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005298 else
5299 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005300 }
Willy Tarreau27375622013-12-17 00:00:28 +01005301
Willy Tarreauc29d0cd2015-09-23 12:18:14 +02005302 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005303 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005304}
5305
5306
5307/* This function updates the request state machine according to the response
5308 * state machine and buffer flags. It returns 1 if it changes anything (flag
5309 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5310 * it is only used to find when a request/response couple is complete. Both
5311 * this function and its equivalent should loop until both return zero. It
5312 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5313 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005314int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005315{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005316 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005317 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005318 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005319 unsigned int old_state = txn->req.msg_state;
5320
Willy Tarreau610ecce2010-01-04 21:15:02 +01005321 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5322 return 0;
5323
5324 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005325 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005326 * We can shut the read side unless we want to abort_on_close,
5327 * or we have a POST request. The issue with POST requests is
5328 * that some browsers still send a CRLF after the request, and
5329 * this CRLF must be read so that it does not remain in the kernel
5330 * buffers, otherwise a close could cause an RST on some systems
5331 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005332 * Note that if we're using keep-alive on the client side, we'd
5333 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005334 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005335 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005336 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005337 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5338 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5339 !(s->be->options & PR_O_ABRT_CLOSE) &&
5340 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005341 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005342
Willy Tarreau40f151a2012-12-20 12:10:09 +01005343 /* if the server closes the connection, we want to immediately react
5344 * and close the socket to save packets and syscalls.
5345 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005346 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005347
Willy Tarreau7f876a12015-11-18 11:59:55 +01005348 /* In any case we've finished parsing the request so we must
5349 * disable Nagle when sending data because 1) we're not going
5350 * to shut this side, and 2) the server is waiting for us to
5351 * send pending data.
5352 */
5353 chn->flags |= CF_NEVER_WAIT;
5354
Willy Tarreau610ecce2010-01-04 21:15:02 +01005355 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5356 goto wait_other_side;
5357
5358 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5359 /* The server has not finished to respond, so we
5360 * don't want to move in order not to upset it.
5361 */
5362 goto wait_other_side;
5363 }
5364
5365 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5366 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005367 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005368 txn->req.msg_state = HTTP_MSG_TUNNEL;
5369 goto wait_other_side;
5370 }
5371
5372 /* When we get here, it means that both the request and the
5373 * response have finished receiving. Depending on the connection
5374 * mode, we'll have to wait for the last bytes to leave in either
5375 * direction, and sometimes for a close to be effective.
5376 */
5377
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005378 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5379 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005380 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5381 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005382 }
5383 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5384 /* Option forceclose is set, or either side wants to close,
5385 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005386 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005387 * once both states are CLOSED.
5388 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005389 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5390 channel_shutr_now(chn);
5391 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005392 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005393 }
5394 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005395 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5396 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005397 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005398 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5399 channel_auto_read(chn);
5400 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005401 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005402 }
5403
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005404 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005405 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005406 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005407
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005408 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005409 txn->req.msg_state = HTTP_MSG_CLOSING;
5410 goto http_msg_closing;
5411 }
5412 else {
5413 txn->req.msg_state = HTTP_MSG_CLOSED;
5414 goto http_msg_closed;
5415 }
5416 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005417 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005418 }
5419
5420 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5421 http_msg_closing:
5422 /* nothing else to forward, just waiting for the output buffer
5423 * to be empty and for the shutw_now to take effect.
5424 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005425 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005426 txn->req.msg_state = HTTP_MSG_CLOSED;
5427 goto http_msg_closed;
5428 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005429 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005430 txn->req.msg_state = HTTP_MSG_ERROR;
5431 goto wait_other_side;
5432 }
5433 }
5434
5435 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5436 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005437 /* see above in MSG_DONE why we only do this in these states */
5438 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5439 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5440 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005441 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005442 goto wait_other_side;
5443 }
5444
5445 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005446 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005447}
5448
5449
5450/* This function updates the response state machine according to the request
5451 * state machine and buffer flags. It returns 1 if it changes anything (flag
5452 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5453 * it is only used to find when a request/response couple is complete. Both
5454 * this function and its equivalent should loop until both return zero. It
5455 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5456 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005457int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005458{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005459 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005460 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005461 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005462 unsigned int old_state = txn->rsp.msg_state;
5463
Willy Tarreau610ecce2010-01-04 21:15:02 +01005464 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5465 return 0;
5466
5467 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5468 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005469 * still monitor the server connection for a possible close
5470 * while the request is being uploaded, so we don't disable
5471 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005472 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005473 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005474
5475 if (txn->req.msg_state == HTTP_MSG_ERROR)
5476 goto wait_other_side;
5477
5478 if (txn->req.msg_state < HTTP_MSG_DONE) {
5479 /* The client seems to still be sending data, probably
5480 * because we got an error response during an upload.
5481 * We have the choice of either breaking the connection
5482 * or letting it pass through. Let's do the later.
5483 */
5484 goto wait_other_side;
5485 }
5486
5487 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5488 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005489 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005490 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005491 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005492 goto wait_other_side;
5493 }
5494
5495 /* When we get here, it means that both the request and the
5496 * response have finished receiving. Depending on the connection
5497 * mode, we'll have to wait for the last bytes to leave in either
5498 * direction, and sometimes for a close to be effective.
5499 */
5500
5501 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5502 /* Server-close mode : shut read and wait for the request
5503 * side to close its output buffer. The caller will detect
5504 * when we're in DONE and the other is in CLOSED and will
5505 * catch that for the final cleanup.
5506 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005507 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5508 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005509 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005510 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5511 /* Option forceclose is set, or either side wants to close,
5512 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005513 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005514 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005515 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005516 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5517 channel_shutr_now(chn);
5518 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005519 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005520 }
5521 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005522 /* The last possible modes are keep-alive and tunnel. Tunnel will
5523 * need to forward remaining data. Keep-alive will need to monitor
5524 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005525 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005526 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005527 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005528 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5529 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005530 }
5531
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005532 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005533 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005534 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005535 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5536 goto http_msg_closing;
5537 }
5538 else {
5539 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5540 goto http_msg_closed;
5541 }
5542 }
5543 goto wait_other_side;
5544 }
5545
5546 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5547 http_msg_closing:
5548 /* nothing else to forward, just waiting for the output buffer
5549 * to be empty and for the shutw_now to take effect.
5550 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005551 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005552 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5553 goto http_msg_closed;
5554 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005555 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005556 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005557 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005558 if (objt_server(s->target))
5559 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005560 goto wait_other_side;
5561 }
5562 }
5563
5564 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5565 http_msg_closed:
5566 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005567 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005568 channel_auto_close(chn);
5569 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005570 goto wait_other_side;
5571 }
5572
5573 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005574 /* We force the response to leave immediately if we're waiting for the
5575 * other side, since there is no pending shutdown to push it out.
5576 */
5577 if (!channel_is_empty(chn))
5578 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005579 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005580}
5581
5582
5583/* Resync the request and response state machines. Return 1 if either state
5584 * changes.
5585 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005586int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005587{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005588 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005589 int old_req_state = txn->req.msg_state;
5590 int old_res_state = txn->rsp.msg_state;
5591
Willy Tarreau610ecce2010-01-04 21:15:02 +01005592 http_sync_req_state(s);
5593 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005594 if (!http_sync_res_state(s))
5595 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005596 if (!http_sync_req_state(s))
5597 break;
5598 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005599
Willy Tarreau610ecce2010-01-04 21:15:02 +01005600 /* OK, both state machines agree on a compatible state.
5601 * There are a few cases we're interested in :
5602 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5603 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5604 * directions, so let's simply disable both analysers.
5605 * - HTTP_MSG_CLOSED on the response only means we must abort the
5606 * request.
5607 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5608 * with server-close mode means we've completed one request and we
5609 * must re-initialize the server connection.
5610 */
5611
5612 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5613 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5614 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5615 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005616 s->req.analysers = 0;
5617 channel_auto_close(&s->req);
5618 channel_auto_read(&s->req);
5619 s->res.analysers = 0;
5620 channel_auto_close(&s->res);
5621 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005622 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005623 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005624 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005625 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005626 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005627 s->res.analysers = 0;
5628 channel_auto_close(&s->res);
5629 channel_auto_read(&s->res);
5630 s->req.analysers = 0;
5631 channel_abort(&s->req);
5632 channel_auto_close(&s->req);
5633 channel_auto_read(&s->req);
5634 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005635 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005636 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5637 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005638 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005639 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5640 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5641 /* server-close/keep-alive: terminate this transaction,
5642 * possibly killing the server connection and reinitialize
5643 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005644 */
5645 http_end_txn_clean_session(s);
5646 }
5647
Willy Tarreau610ecce2010-01-04 21:15:02 +01005648 return txn->req.msg_state != old_req_state ||
5649 txn->rsp.msg_state != old_res_state;
5650}
5651
Willy Tarreaud98cf932009-12-27 22:54:55 +01005652/* This function is an analyser which forwards request body (including chunk
5653 * sizes if any). It is called as soon as we must forward, even if we forward
5654 * zero byte. The only situation where it must not be called is when we're in
5655 * tunnel mode and we want to forward till the close. It's used both to forward
5656 * remaining data and to resync after end of body. It expects the msg_state to
5657 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005658 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005659 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005660 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005661 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005662int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005663{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005664 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005665 struct http_txn *txn = s->txn;
5666 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005667
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005668 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5669 return 0;
5670
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005671 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005672 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005673 /* Output closed while we were sending data. We must abort and
5674 * wake the other side up.
5675 */
5676 msg->msg_state = HTTP_MSG_ERROR;
5677 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005678 return 1;
5679 }
5680
Willy Tarreaud98cf932009-12-27 22:54:55 +01005681 /* Note that we don't have to send 100-continue back because we don't
5682 * need the data to complete our job, and it's up to the server to
5683 * decide whether to return 100, 417 or anything else in return of
5684 * an "Expect: 100-continue" header.
5685 */
5686
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005687 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005688 /* we have msg->sov which points to the first byte of message
5689 * body, and req->buf.p still points to the beginning of the
5690 * message. We forward the headers now, as we don't need them
5691 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005692 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005693 b_adv(req->buf, msg->sov);
5694 msg->next -= msg->sov;
5695 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005696
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005697 /* The previous analysers guarantee that the state is somewhere
5698 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5699 * msg->next are always correct.
5700 */
5701 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5702 if (msg->flags & HTTP_MSGF_TE_CHNK)
5703 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5704 else
5705 msg->msg_state = HTTP_MSG_DATA;
5706 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005707 }
5708
Willy Tarreau7ba23542014-04-17 21:50:00 +02005709 /* Some post-connect processing might want us to refrain from starting to
5710 * forward data. Currently, the only reason for this is "balance url_param"
5711 * whichs need to parse/process the request after we've enabled forwarding.
5712 */
5713 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005714 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005715 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005716 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005717 goto missing_data;
5718 }
5719 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5720 }
5721
Willy Tarreau80a92c02014-03-12 10:41:13 +01005722 /* in most states, we should abort in case of early close */
5723 channel_auto_close(req);
5724
Willy Tarreauefdf0942014-04-24 20:08:57 +02005725 if (req->to_forward) {
5726 /* We can't process the buffer's contents yet */
5727 req->flags |= CF_WAKE_WRITE;
5728 goto missing_data;
5729 }
5730
Willy Tarreaud98cf932009-12-27 22:54:55 +01005731 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005732 if (msg->msg_state == HTTP_MSG_DATA) {
5733 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005734 /* we may have some pending data starting at req->buf->p */
5735 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005736 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005737 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005738 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005739 msg->next += msg->chunk_len;
5740 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005741
5742 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005743 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005744 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005745 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005746 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005747 }
5748 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005749 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005750 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005751 * TRAILERS state.
5752 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005753 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005754
Willy Tarreau54d23df2012-10-25 19:04:45 +02005755 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005756 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005757 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005758 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005759 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005760 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005761 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005762 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005763 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005764 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005765 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005766 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005767 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005768
5769 if (ret == 0)
5770 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005771 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005772 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005773 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005774 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005775 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005776 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005777 /* we're in MSG_CHUNK_SIZE now */
5778 }
5779 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005780 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005781
5782 if (ret == 0)
5783 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005784 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005785 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005786 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005787 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005788 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005789 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005790 /* we're in HTTP_MSG_DONE now */
5791 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005792 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005793 int old_state = msg->msg_state;
5794
Willy Tarreau610ecce2010-01-04 21:15:02 +01005795 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005796
5797 /* we may have some pending data starting at req->buf->p
5798 * such as last chunk of data or trailers.
5799 */
5800 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005801 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005802 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005803 msg->next = 0;
5804
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005805 /* we don't want to forward closes on DONE except in
5806 * tunnel mode.
5807 */
5808 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005809 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005810 if (http_resync_states(s)) {
5811 /* some state changes occurred, maybe the analyser
5812 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005813 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005814 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005815 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005816 /* request errors are most likely due to
5817 * the server aborting the transfer.
5818 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005819 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005820 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005821 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005822 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005823 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005824 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005825 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005826 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005827
5828 /* If "option abortonclose" is set on the backend, we
5829 * want to monitor the client's connection and forward
5830 * any shutdown notification to the server, which will
5831 * decide whether to close or to go on processing the
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005832 * request. We only do that in tunnel mode, and not in
5833 * other modes since it can be abused to exhaust source
5834 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005835 */
5836 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005837 channel_auto_read(req);
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005838 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5839 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5840 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005841 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005842 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005843 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005844 /* POST requests may require to read extra CRLF
5845 * sent by broken browsers and which could cause
5846 * an RST to be sent upon close on some systems
5847 * (eg: Linux).
5848 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005849 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005850 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005851
Willy Tarreau610ecce2010-01-04 21:15:02 +01005852 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005853 }
5854 }
5855
Willy Tarreaud98cf932009-12-27 22:54:55 +01005856 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005857 /* we may have some pending data starting at req->buf->p */
5858 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005859 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005860 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5861
Willy Tarreaubed410e2014-04-22 08:19:34 +02005862 msg->next = 0;
5863 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5864
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005865 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005866 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005867 if (!(s->flags & SF_ERR_MASK))
5868 s->flags |= SF_ERR_CLICL;
5869 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005870 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005871 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005872 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005873 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005874 }
5875
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005876 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005877 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005878 if (objt_server(s->target))
5879 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005880
5881 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005882 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005883
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005884 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005885 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005886 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005887
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005888 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005889 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005890 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005891 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005892 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005893
Willy Tarreau5c620922011-05-11 19:56:11 +02005894 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005895 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005896 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005897 * modes are already handled by the stream sock layer. We must not do
5898 * this in content-length mode because it could present the MSG_MORE
5899 * flag with the last block of forwarded data, which would cause an
5900 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005901 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005902 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005903 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005904
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005905 return 0;
5906
Willy Tarreaud98cf932009-12-27 22:54:55 +01005907 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005908 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005909 if (sess->listener->counters)
5910 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005911
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005912 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005913 /* we may have some pending data starting at req->buf->p */
5914 b_adv(req->buf, msg->next);
5915 msg->next = 0;
5916
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005917 txn->req.msg_state = HTTP_MSG_ERROR;
5918 if (txn->status) {
5919 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005920 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005921 } else {
5922 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005923 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005924 }
5925 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005926 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005927
Willy Tarreaue7dff022015-04-03 01:14:29 +02005928 if (!(s->flags & SF_ERR_MASK))
5929 s->flags |= SF_ERR_PRXCOND;
5930 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005931 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005932 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005933 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005934 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005935 }
5936 return 0;
5937
5938 aborted_xfer:
5939 txn->req.msg_state = HTTP_MSG_ERROR;
5940 if (txn->status) {
5941 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005942 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005943 } else {
5944 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005945 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005946 }
5947 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005948 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005949
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005950 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005951 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005952 if (objt_server(s->target))
5953 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005954
Willy Tarreaue7dff022015-04-03 01:14:29 +02005955 if (!(s->flags & SF_ERR_MASK))
5956 s->flags |= SF_ERR_SRVCL;
5957 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005958 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005959 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005960 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005961 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005962 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005963 return 0;
5964}
5965
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005966/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5967 * processing can continue on next analysers, or zero if it either needs more
5968 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005969 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005970 * when it has nothing left to do, and may remove any analyser when it wants to
5971 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005972 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005973int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005974{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005975 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005976 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005977 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005978 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005979 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005980 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005981 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005982
Willy Tarreau87b09662015-04-03 00:22:06 +02005983 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 +02005984 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005985 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005986 rep,
5987 rep->rex, rep->wex,
5988 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005989 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005990 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005991
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005992 /*
5993 * Now parse the partial (or complete) lines.
5994 * We will check the response syntax, and also join multi-line
5995 * headers. An index of all the lines will be elaborated while
5996 * parsing.
5997 *
5998 * For the parsing, we use a 28 states FSM.
5999 *
6000 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02006001 * rep->buf->p = beginning of response
6002 * rep->buf->p + msg->eoh = end of processed headers / start of current one
6003 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02006004 * msg->eol = end of current header or line (LF or CRLF)
6005 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006006 */
6007
Willy Tarreau628c40c2014-04-24 19:11:26 +02006008 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01006009 /* There's a protected area at the end of the buffer for rewriting
6010 * purposes. We don't want to start to parse the request if the
6011 * protected area is affected, because we may have to move processed
6012 * data later, which is much more complicated.
6013 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006014 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01006015 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02006016 /* some data has still not left the buffer, wake us once that's done */
6017 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
6018 goto abort_response;
6019 channel_dont_close(rep);
6020 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01006021 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02006022 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01006023 }
6024
Willy Tarreau379357a2013-06-08 12:55:46 +02006025 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
6026 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
6027 buffer_slow_realign(rep->buf);
6028
Willy Tarreau9b28e032012-10-12 23:49:43 +02006029 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01006030 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01006031 }
6032
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006033 /* 1: we might have to print this header in debug mode */
6034 if (unlikely((global.mode & MODE_DEBUG) &&
6035 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02006036 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006037 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006038
Willy Tarreau9b28e032012-10-12 23:49:43 +02006039 sol = rep->buf->p;
6040 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006041 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006042
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006043 sol += hdr_idx_first_pos(&txn->hdr_idx);
6044 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006045
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006046 while (cur_idx) {
6047 eol = sol + txn->hdr_idx.v[cur_idx].len;
6048 debug_hdr("srvhdr", s, sol, eol);
6049 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
6050 cur_idx = txn->hdr_idx.v[cur_idx].next;
6051 }
6052 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006053
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006054 /*
6055 * Now we quickly check if we have found a full valid response.
6056 * If not so, we check the FD and buffer states before leaving.
6057 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01006058 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006059 * responses are checked first.
6060 *
6061 * Depending on whether the client is still there or not, we
6062 * may send an error response back or not. Note that normally
6063 * we should only check for HTTP status there, and check I/O
6064 * errors somewhere else.
6065 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006066
Willy Tarreau655dce92009-11-08 13:10:58 +01006067 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006068 /* Invalid response */
6069 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6070 /* we detected a parsing error. We want to archive this response
6071 * in the dedicated proxy area for later troubleshooting.
6072 */
6073 hdr_response_bad:
6074 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006075 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006076
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006077 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006078 if (objt_server(s->target)) {
6079 objt_server(s->target)->counters.failed_resp++;
6080 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006081 }
Willy Tarreau64648412010-03-05 10:41:54 +01006082 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006083 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006084 rep->analysers = 0;
6085 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006086 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006087 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006088 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006089
Willy Tarreaue7dff022015-04-03 01:14:29 +02006090 if (!(s->flags & SF_ERR_MASK))
6091 s->flags |= SF_ERR_PRXCOND;
6092 if (!(s->flags & SF_FINST_MASK))
6093 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006094
6095 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006096 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006097
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006098 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006099 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006100 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006101 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006102 goto hdr_response_bad;
6103 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006104
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006105 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006106 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006107 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006108 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006109 else if (txn->flags & TX_NOT_FIRST)
6110 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006111
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006112 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006113 if (objt_server(s->target)) {
6114 objt_server(s->target)->counters.failed_resp++;
6115 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006116 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006117
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006118 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006119 rep->analysers = 0;
6120 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006121 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006122 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006123 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006124
Willy Tarreaue7dff022015-04-03 01:14:29 +02006125 if (!(s->flags & SF_ERR_MASK))
6126 s->flags |= SF_ERR_SRVCL;
6127 if (!(s->flags & SF_FINST_MASK))
6128 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006129 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006130 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006131
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006132 /* read timeout : return a 504 to the client. */
6133 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006134 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006135 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006136
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006137 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006138 if (objt_server(s->target)) {
6139 objt_server(s->target)->counters.failed_resp++;
6140 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006141 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006142
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006143 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006144 rep->analysers = 0;
6145 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006146 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006147 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006148 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006149
Willy Tarreaue7dff022015-04-03 01:14:29 +02006150 if (!(s->flags & SF_ERR_MASK))
6151 s->flags |= SF_ERR_SRVTO;
6152 if (!(s->flags & SF_FINST_MASK))
6153 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006154 return 0;
6155 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006156
Willy Tarreauf003d372012-11-26 13:35:37 +01006157 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006158 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006159 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006160 s->be->be_counters.cli_aborts++;
6161 if (objt_server(s->target))
6162 objt_server(s->target)->counters.cli_aborts++;
6163
6164 rep->analysers = 0;
6165 channel_auto_close(rep);
6166
6167 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006168 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006169 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006170
Willy Tarreaue7dff022015-04-03 01:14:29 +02006171 if (!(s->flags & SF_ERR_MASK))
6172 s->flags |= SF_ERR_CLICL;
6173 if (!(s->flags & SF_FINST_MASK))
6174 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006175
Willy Tarreau87b09662015-04-03 00:22:06 +02006176 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006177 return 0;
6178 }
6179
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006180 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006181 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006182 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006183 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006184 else if (txn->flags & TX_NOT_FIRST)
6185 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006186
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006187 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006188 if (objt_server(s->target)) {
6189 objt_server(s->target)->counters.failed_resp++;
6190 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006191 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006192
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006193 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006194 rep->analysers = 0;
6195 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006196 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006197 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006198 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006199
Willy Tarreaue7dff022015-04-03 01:14:29 +02006200 if (!(s->flags & SF_ERR_MASK))
6201 s->flags |= SF_ERR_SRVCL;
6202 if (!(s->flags & SF_FINST_MASK))
6203 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006204 return 0;
6205 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006206
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006207 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006208 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006209 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006210 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006211 else if (txn->flags & TX_NOT_FIRST)
6212 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006213
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006214 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006215 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006216 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006217
Willy Tarreaue7dff022015-04-03 01:14:29 +02006218 if (!(s->flags & SF_ERR_MASK))
6219 s->flags |= SF_ERR_CLICL;
6220 if (!(s->flags & SF_FINST_MASK))
6221 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006222
Willy Tarreau87b09662015-04-03 00:22:06 +02006223 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006224 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006225 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006226
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006227 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006228 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006229 return 0;
6230 }
6231
6232 /* More interesting part now : we know that we have a complete
6233 * response which at least looks like HTTP. We have an indicator
6234 * of each header's length, so we can parse them quickly.
6235 */
6236
6237 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006238 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006239
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006240 /*
6241 * 1: get the status code
6242 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006243 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006244 if (n < 1 || n > 5)
6245 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006246 /* when the client triggers a 4xx from the server, it's most often due
6247 * to a missing object or permission. These events should be tracked
6248 * because if they happen often, it may indicate a brute force or a
6249 * vulnerability scan.
6250 */
6251 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006252 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006253
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006254 if (objt_server(s->target))
6255 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006256
Willy Tarreau91852eb2015-05-01 13:26:00 +02006257 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6258 * exactly one digit "." one digit. This check may be disabled using
6259 * option accept-invalid-http-response.
6260 */
6261 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6262 if (msg->sl.st.v_l != 8) {
6263 msg->err_pos = 0;
6264 goto hdr_response_bad;
6265 }
6266
6267 if (rep->buf->p[4] != '/' ||
6268 !isdigit((unsigned char)rep->buf->p[5]) ||
6269 rep->buf->p[6] != '.' ||
6270 !isdigit((unsigned char)rep->buf->p[7])) {
6271 msg->err_pos = 4;
6272 goto hdr_response_bad;
6273 }
6274 }
6275
Willy Tarreau5b154472009-12-21 20:11:07 +01006276 /* check if the response is HTTP/1.1 or above */
6277 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006278 ((rep->buf->p[5] > '1') ||
6279 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006280 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006281
6282 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006283 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 +01006284
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006285 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006286 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006287
Willy Tarreau9b28e032012-10-12 23:49:43 +02006288 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006289
Willy Tarreau39650402010-03-15 19:44:39 +01006290 /* Adjust server's health based on status code. Note: status codes 501
6291 * and 505 are triggered on demand by client request, so we must not
6292 * count them as server failures.
6293 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006294 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006295 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006296 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006297 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006298 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006299 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006300
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006301 /*
6302 * 2: check for cacheability.
6303 */
6304
6305 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006306 case 100:
6307 /*
6308 * We may be facing a 100-continue response, in which case this
6309 * is not the right response, and we're waiting for the next one.
6310 * Let's allow this response to go to the client and wait for the
6311 * next one.
6312 */
6313 hdr_idx_init(&txn->hdr_idx);
6314 msg->next -= channel_forward(rep, msg->next);
6315 msg->msg_state = HTTP_MSG_RPBEFORE;
6316 txn->status = 0;
6317 s->logs.t_data = -1; /* was not a response yet */
6318 goto next_one;
6319
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006320 case 200:
6321 case 203:
6322 case 206:
6323 case 300:
6324 case 301:
6325 case 410:
6326 /* RFC2616 @13.4:
6327 * "A response received with a status code of
6328 * 200, 203, 206, 300, 301 or 410 MAY be stored
6329 * by a cache (...) unless a cache-control
6330 * directive prohibits caching."
6331 *
6332 * RFC2616 @9.5: POST method :
6333 * "Responses to this method are not cacheable,
6334 * unless the response includes appropriate
6335 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006336 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006337 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006338 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006339 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6340 break;
6341 default:
6342 break;
6343 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006344
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006345 /*
6346 * 3: we may need to capture headers
6347 */
6348 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006349 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006350 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006351 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006352
Willy Tarreau557f1992015-05-01 10:05:17 +02006353 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6354 * by RFC7230#3.3.3 :
6355 *
6356 * The length of a message body is determined by one of the following
6357 * (in order of precedence):
6358 *
6359 * 1. Any response to a HEAD request and any response with a 1xx
6360 * (Informational), 204 (No Content), or 304 (Not Modified) status
6361 * code is always terminated by the first empty line after the
6362 * header fields, regardless of the header fields present in the
6363 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006364 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006365 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6366 * the connection will become a tunnel immediately after the empty
6367 * line that concludes the header fields. A client MUST ignore any
6368 * Content-Length or Transfer-Encoding header fields received in
6369 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006370 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006371 * 3. If a Transfer-Encoding header field is present and the chunked
6372 * transfer coding (Section 4.1) is the final encoding, the message
6373 * body length is determined by reading and decoding the chunked
6374 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006375 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006376 * If a Transfer-Encoding header field is present in a response and
6377 * the chunked transfer coding is not the final encoding, the
6378 * message body length is determined by reading the connection until
6379 * it is closed by the server. If a Transfer-Encoding header field
6380 * is present in a request and the chunked transfer coding is not
6381 * the final encoding, the message body length cannot be determined
6382 * reliably; the server MUST respond with the 400 (Bad Request)
6383 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006384 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006385 * If a message is received with both a Transfer-Encoding and a
6386 * Content-Length header field, the Transfer-Encoding overrides the
6387 * Content-Length. Such a message might indicate an attempt to
6388 * perform request smuggling (Section 9.5) or response splitting
6389 * (Section 9.4) and ought to be handled as an error. A sender MUST
6390 * remove the received Content-Length field prior to forwarding such
6391 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006392 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006393 * 4. If a message is received without Transfer-Encoding and with
6394 * either multiple Content-Length header fields having differing
6395 * field-values or a single Content-Length header field having an
6396 * invalid value, then the message framing is invalid and the
6397 * recipient MUST treat it as an unrecoverable error. If this is a
6398 * request message, the server MUST respond with a 400 (Bad Request)
6399 * status code and then close the connection. If this is a response
6400 * message received by a proxy, the proxy MUST close the connection
6401 * to the server, discard the received response, and send a 502 (Bad
6402 * Gateway) response to the client. If this is a response message
6403 * received by a user agent, the user agent MUST close the
6404 * connection to the server and discard the received response.
6405 *
6406 * 5. If a valid Content-Length header field is present without
6407 * Transfer-Encoding, its decimal value defines the expected message
6408 * body length in octets. If the sender closes the connection or
6409 * the recipient times out before the indicated number of octets are
6410 * received, the recipient MUST consider the message to be
6411 * incomplete and close the connection.
6412 *
6413 * 6. If this is a request message and none of the above are true, then
6414 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006415 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006416 * 7. Otherwise, this is a response message without a declared message
6417 * body length, so the message body length is determined by the
6418 * number of octets received prior to the server closing the
6419 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006420 */
6421
6422 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006423 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006424 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006425 * FIXME: should we parse anyway and return an error on chunked encoding ?
6426 */
6427 if (txn->meth == HTTP_METH_HEAD ||
6428 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006429 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006430 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006431 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006432 goto skip_content_length;
6433 }
6434
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006435 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006436 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006437 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006438 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006439 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6440 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006441 /* bad transfer-encoding (chunked followed by something else) */
6442 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006443 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006444 break;
6445 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006446 }
6447
Willy Tarreau1c913912015-04-30 10:57:51 +02006448 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006449 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006450 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006451 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6452 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6453 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006454 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006455 signed long long cl;
6456
Willy Tarreauad14f752011-09-02 20:33:27 +02006457 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006458 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006459 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006460 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006461
Willy Tarreauad14f752011-09-02 20:33:27 +02006462 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006463 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006464 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006465 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006466
Willy Tarreauad14f752011-09-02 20:33:27 +02006467 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006468 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006469 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006470 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006471
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006472 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006473 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006474 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006475 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006476
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006477 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006478 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006479 }
6480
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006481 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006482 select_compression_response_header(s, rep->buf);
6483
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006484skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006485 /* Now we have to check if we need to modify the Connection header.
6486 * This is more difficult on the response than it is on the request,
6487 * because we can have two different HTTP versions and we don't know
6488 * how the client will interprete a response. For instance, let's say
6489 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6490 * HTTP/1.1 response without any header. Maybe it will bound itself to
6491 * HTTP/1.0 because it only knows about it, and will consider the lack
6492 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6493 * the lack of header as a keep-alive. Thus we will use two flags
6494 * indicating how a request MAY be understood by the client. In case
6495 * of multiple possibilities, we'll fix the header to be explicit. If
6496 * ambiguous cases such as both close and keepalive are seen, then we
6497 * will fall back to explicit close. Note that we won't take risks with
6498 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006499 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006500 */
6501
Willy Tarreaudc008c52010-02-01 16:20:08 +01006502 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6503 txn->status == 101)) {
6504 /* Either we've established an explicit tunnel, or we're
6505 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006506 * to understand the next protocols. We have to switch to tunnel
6507 * mode, so that we transfer the request and responses then let
6508 * this protocol pass unmodified. When we later implement specific
6509 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006510 * header which contains information about that protocol for
6511 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006512 */
6513 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6514 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006515 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6516 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006517 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006518 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006519 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006520
Willy Tarreau70dffda2014-01-30 03:07:23 +01006521 /* this situation happens when combining pretend-keepalive with httpclose. */
6522 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006523 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006524 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006525 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6526
Willy Tarreau60466522010-01-18 19:08:45 +01006527 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006528 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006529 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6530 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006531
Willy Tarreau60466522010-01-18 19:08:45 +01006532 /* now adjust header transformations depending on current state */
6533 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6534 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6535 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006536 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006537 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006538 }
Willy Tarreau60466522010-01-18 19:08:45 +01006539 else { /* SCL / KAL */
6540 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006541 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006542 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006543 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006544
Willy Tarreau60466522010-01-18 19:08:45 +01006545 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006546 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006547
Willy Tarreau60466522010-01-18 19:08:45 +01006548 /* Some keep-alive responses are converted to Server-close if
6549 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006550 */
Willy Tarreau60466522010-01-18 19:08:45 +01006551 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6552 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006553 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006554 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006555 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006556 }
6557
Willy Tarreau7959a552013-09-23 16:44:27 +02006558 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006559 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006560
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006561 /* end of job, return OK */
6562 rep->analysers &= ~an_bit;
6563 rep->analyse_exp = TICK_ETERNITY;
6564 channel_auto_close(rep);
6565 return 1;
6566
6567 abort_keep_alive:
6568 /* A keep-alive request to the server failed on a network error.
6569 * The client is required to retry. We need to close without returning
6570 * any other information so that the client retries.
6571 */
6572 txn->status = 0;
6573 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006574 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006575 channel_auto_close(rep);
6576 s->logs.logwait = 0;
6577 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006578 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006579 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006580 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006581 return 0;
6582}
6583
6584/* This function performs all the processing enabled for the current response.
6585 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006586 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006587 * other functions. It works like process_request (see indications above).
6588 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006589int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006590{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006591 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006592 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006593 struct http_msg *msg = &txn->rsp;
6594 struct proxy *cur_proxy;
6595 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006596 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006597
Willy Tarreau87b09662015-04-03 00:22:06 +02006598 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 +02006599 now_ms, __FUNCTION__,
6600 s,
6601 rep,
6602 rep->rex, rep->wex,
6603 rep->flags,
6604 rep->buf->i,
6605 rep->analysers);
6606
6607 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6608 return 0;
6609
Willy Tarreau70730dd2014-04-24 18:06:27 +02006610 /* The stats applet needs to adjust the Connection header but we don't
6611 * apply any filter there.
6612 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006613 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6614 rep->analysers &= ~an_bit;
6615 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006616 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006617 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006618
Willy Tarreau58975672014-04-24 21:13:57 +02006619 /*
6620 * We will have to evaluate the filters.
6621 * As opposed to version 1.2, now they will be evaluated in the
6622 * filters order and not in the header order. This means that
6623 * each filter has to be validated among all headers.
6624 *
6625 * Filters are tried with ->be first, then with ->fe if it is
6626 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006627 *
6628 * Maybe we are in resume condiion. In this case I choose the
6629 * "struct proxy" which contains the rule list matching the resume
6630 * pointer. If none of theses "struct proxy" match, I initialise
6631 * the process with the first one.
6632 *
6633 * In fact, I check only correspondance betwwen the current list
6634 * pointer and the ->fe rule list. If it doesn't match, I initialize
6635 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006636 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006637 if (s->current_rule_list == &sess->fe->http_res_rules)
6638 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006639 else
6640 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006641 while (1) {
6642 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006643
Willy Tarreau58975672014-04-24 21:13:57 +02006644 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006645 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006646 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006647
Willy Tarreau51d861a2015-05-22 17:30:48 +02006648 if (ret == HTTP_RULE_RES_BADREQ)
6649 goto return_srv_prx_502;
6650
6651 if (ret == HTTP_RULE_RES_DONE) {
6652 rep->analysers &= ~an_bit;
6653 rep->analyse_exp = TICK_ETERNITY;
6654 return 1;
6655 }
6656 }
6657
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006658 /* we need to be called again. */
6659 if (ret == HTTP_RULE_RES_YIELD) {
6660 channel_dont_close(rep);
6661 return 0;
6662 }
6663
Willy Tarreau58975672014-04-24 21:13:57 +02006664 /* try headers filters */
6665 if (rule_set->rsp_exp != NULL) {
6666 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6667 return_bad_resp:
6668 if (objt_server(s->target)) {
6669 objt_server(s->target)->counters.failed_resp++;
6670 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006671 }
Willy Tarreau58975672014-04-24 21:13:57 +02006672 s->be->be_counters.failed_resp++;
6673 return_srv_prx_502:
6674 rep->analysers = 0;
6675 txn->status = 502;
6676 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006677 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006678 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006679 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006680 if (!(s->flags & SF_ERR_MASK))
6681 s->flags |= SF_ERR_PRXCOND;
6682 if (!(s->flags & SF_FINST_MASK))
6683 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006684 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006685 }
Willy Tarreau58975672014-04-24 21:13:57 +02006686 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006687
Willy Tarreau58975672014-04-24 21:13:57 +02006688 /* has the response been denied ? */
6689 if (txn->flags & TX_SVDENY) {
6690 if (objt_server(s->target))
6691 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006692
Willy Tarreau58975672014-04-24 21:13:57 +02006693 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006694 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006695 if (sess->listener->counters)
6696 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006697
Willy Tarreau58975672014-04-24 21:13:57 +02006698 goto return_srv_prx_502;
6699 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006700
Willy Tarreau58975672014-04-24 21:13:57 +02006701 /* add response headers from the rule sets in the same order */
6702 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006703 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006704 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006705 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006706 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006707 ret = acl_pass(ret);
6708 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6709 ret = !ret;
6710 if (!ret)
6711 continue;
6712 }
6713 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6714 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006715 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006716
Willy Tarreau58975672014-04-24 21:13:57 +02006717 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006718 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006719 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006720 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006721 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006722
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006723 /* After this point, this anayzer can't return yield, so we can
6724 * remove the bit corresponding to this analyzer from the list.
6725 *
6726 * Note that the intermediate returns and goto found previously
6727 * reset the analyzers.
6728 */
6729 rep->analysers &= ~an_bit;
6730 rep->analyse_exp = TICK_ETERNITY;
6731
Willy Tarreau58975672014-04-24 21:13:57 +02006732 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006733 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006734 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006735
Willy Tarreau58975672014-04-24 21:13:57 +02006736 /*
6737 * Now check for a server cookie.
6738 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006739 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006740 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006741
Willy Tarreau58975672014-04-24 21:13:57 +02006742 /*
6743 * Check for cache-control or pragma headers if required.
6744 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006745 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 +02006746 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006747
Willy Tarreau58975672014-04-24 21:13:57 +02006748 /*
6749 * Add server cookie in the response if needed
6750 */
6751 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6752 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006753 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006754 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6755 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6756 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6757 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6758 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006759 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006760 /* the server is known, it's not the one the client requested, or the
6761 * cookie's last seen date needs to be refreshed. We have to
6762 * insert a set-cookie here, except if we want to insert only on POST
6763 * requests and this one isn't. Note that servers which don't have cookies
6764 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006765 */
Willy Tarreau58975672014-04-24 21:13:57 +02006766 if (!objt_server(s->target)->cookie) {
6767 chunk_printf(&trash,
6768 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6769 s->be->cookie_name);
6770 }
6771 else {
6772 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006773
Willy Tarreau58975672014-04-24 21:13:57 +02006774 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6775 /* emit last_date, which is mandatory */
6776 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6777 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6778 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006779
Willy Tarreau58975672014-04-24 21:13:57 +02006780 if (s->be->cookie_maxlife) {
6781 /* emit first_date, which is either the original one or
6782 * the current date.
6783 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006784 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006785 s30tob64(txn->cookie_first_date ?
6786 txn->cookie_first_date >> 2 :
6787 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006788 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006789 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006790 }
Willy Tarreau58975672014-04-24 21:13:57 +02006791 chunk_appendf(&trash, "; path=/");
6792 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006793
Willy Tarreau58975672014-04-24 21:13:57 +02006794 if (s->be->cookie_domain)
6795 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006796
Willy Tarreau58975672014-04-24 21:13:57 +02006797 if (s->be->ck_opts & PR_CK_HTTPONLY)
6798 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006799
Willy Tarreau58975672014-04-24 21:13:57 +02006800 if (s->be->ck_opts & PR_CK_SECURE)
6801 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006802
Willy Tarreau58975672014-04-24 21:13:57 +02006803 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6804 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006805
Willy Tarreau58975672014-04-24 21:13:57 +02006806 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006807 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006808 /* the server did not change, only the date was updated */
6809 txn->flags |= TX_SCK_UPDATED;
6810 else
6811 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006812
Willy Tarreau58975672014-04-24 21:13:57 +02006813 /* Here, we will tell an eventual cache on the client side that we don't
6814 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6815 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6816 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006817 */
Willy Tarreau58975672014-04-24 21:13:57 +02006818 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006819
Willy Tarreau58975672014-04-24 21:13:57 +02006820 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006821
Willy Tarreau58975672014-04-24 21:13:57 +02006822 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6823 "Cache-control: private", 22) < 0))
6824 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006825 }
Willy Tarreau58975672014-04-24 21:13:57 +02006826 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006827
Willy Tarreau58975672014-04-24 21:13:57 +02006828 /*
6829 * Check if result will be cacheable with a cookie.
6830 * We'll block the response if security checks have caught
6831 * nasty things such as a cacheable cookie.
6832 */
6833 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6834 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6835 (s->be->options & PR_O_CHK_CACHE)) {
6836 /* we're in presence of a cacheable response containing
6837 * a set-cookie header. We'll block it as requested by
6838 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006839 */
Willy Tarreau58975672014-04-24 21:13:57 +02006840 if (objt_server(s->target))
6841 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006842
Willy Tarreau58975672014-04-24 21:13:57 +02006843 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006844 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006845 if (sess->listener->counters)
6846 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006847
Willy Tarreau58975672014-04-24 21:13:57 +02006848 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6849 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6850 send_log(s->be, LOG_ALERT,
6851 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6852 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6853 goto return_srv_prx_502;
6854 }
Willy Tarreau03945942009-12-22 16:50:27 +01006855
Willy Tarreau70730dd2014-04-24 18:06:27 +02006856 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006857 /*
6858 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6859 * If an "Upgrade" token is found, the header is left untouched in order
6860 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006861 * if anything but "Upgrade" is present in the Connection header. We don't
6862 * want to touch any 101 response either since it's switching to another
6863 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006864 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006865 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006866 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006867 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006868 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6869 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006870
Willy Tarreau58975672014-04-24 21:13:57 +02006871 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6872 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6873 /* we want a keep-alive response here. Keep-alive header
6874 * required if either side is not 1.1.
6875 */
6876 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6877 want_flags |= TX_CON_KAL_SET;
6878 }
6879 else {
6880 /* we want a close response here. Close header required if
6881 * the server is 1.1, regardless of the client.
6882 */
6883 if (msg->flags & HTTP_MSGF_VER_11)
6884 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006885 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006886
Willy Tarreau58975672014-04-24 21:13:57 +02006887 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6888 http_change_connection_header(txn, msg, want_flags);
6889 }
6890
6891 skip_header_mangling:
6892 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6893 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6894 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006895
Willy Tarreau58975672014-04-24 21:13:57 +02006896 /* if the user wants to log as soon as possible, without counting
6897 * bytes from the server, then this is the right moment. We have
6898 * to temporarily assign bytes_out to log what we currently have.
6899 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006900 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006901 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6902 s->logs.bytes_out = txn->rsp.eoh;
6903 s->do_log(s);
6904 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006905 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006906 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006907}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006908
Willy Tarreaud98cf932009-12-27 22:54:55 +01006909/* This function is an analyser which forwards response body (including chunk
6910 * sizes if any). It is called as soon as we must forward, even if we forward
6911 * zero byte. The only situation where it must not be called is when we're in
6912 * tunnel mode and we want to forward till the close. It's used both to forward
6913 * remaining data and to resync after end of body. It expects the msg_state to
6914 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006915 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006916 *
6917 * It is capable of compressing response data both in content-length mode and
6918 * in chunked mode. The state machines follows different flows depending on
6919 * whether content-length and chunked modes are used, since there are no
6920 * trailers in content-length :
6921 *
6922 * chk-mode cl-mode
6923 * ,----- BODY -----.
6924 * / \
6925 * V size > 0 V chk-mode
6926 * .--> SIZE -------------> DATA -------------> CRLF
6927 * | | size == 0 | last byte |
6928 * | v final crlf v inspected |
6929 * | TRAILERS -----------> DONE |
6930 * | |
6931 * `----------------------------------------------'
6932 *
6933 * Compression only happens in the DATA state, and must be flushed in final
6934 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6935 * is performed at once on final states for all bytes parsed, or when leaving
6936 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006937 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006938int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006939{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006940 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006941 struct http_txn *txn = s->txn;
6942 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006943 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006944 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006945 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006946
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006947 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6948 return 0;
6949
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006950 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006951 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006952 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006953 /* Output closed while we were sending data. We must abort and
6954 * wake the other side up.
6955 */
6956 msg->msg_state = HTTP_MSG_ERROR;
6957 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006958 return 1;
6959 }
6960
Willy Tarreau4fe41902010-06-07 22:27:41 +02006961 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006962 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006963
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006964 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006965 /* we have msg->sov which points to the first byte of message
6966 * body, and res->buf.p still points to the beginning of the
6967 * message. We forward the headers now, as we don't need them
6968 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006969 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006970 b_adv(res->buf, msg->sov);
6971 msg->next -= msg->sov;
6972 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006973
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006974 /* The previous analysers guarantee that the state is somewhere
6975 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6976 * msg->next are always correct.
6977 */
6978 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6979 if (msg->flags & HTTP_MSGF_TE_CHNK)
6980 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6981 else
6982 msg->msg_state = HTTP_MSG_DATA;
6983 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006984 }
6985
Willy Tarreauefdf0942014-04-24 20:08:57 +02006986 if (res->to_forward) {
6987 /* We can't process the buffer's contents yet */
6988 res->flags |= CF_WAKE_WRITE;
6989 goto missing_data;
6990 }
6991
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006992 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6993 /* We need a compression buffer in the DATA state to put the
6994 * output of compressed data, and in CRLF state to let the
6995 * TRAILERS state finish the job of removing the trailing CRLF.
6996 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006997 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006998 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006999 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02007000 goto aborted_xfer; /* no memory */
7001 }
7002
7003 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01007004 if (ret < 0) {
7005 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02007006 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01007007 }
William Lallemand82fe75c2012-10-23 10:25:10 +02007008 compressing = 1;
7009 }
7010
Willy Tarreaud98cf932009-12-27 22:54:55 +01007011 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007012 switch (msg->msg_state - HTTP_MSG_DATA) {
7013 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02007014 /* we may have some pending data starting at res->buf->p */
7015 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02007016 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01007017 if (ret < 0)
7018 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02007019
Willy Tarreaud5a67832014-04-21 10:54:27 +02007020 if (msg->chunk_len) {
7021 /* input empty or output full */
7022 if (res->buf->i > msg->next)
7023 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02007024 goto missing_data;
7025 }
William Lallemandbf3ae612012-11-19 12:35:37 +01007026 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007027 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02007028 if (msg->chunk_len > res->buf->i - msg->next) {
7029 /* output full */
7030 res->flags |= CF_WAKE_WRITE;
7031 goto missing_data;
7032 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007033 msg->next += msg->chunk_len;
7034 msg->chunk_len = 0;
7035 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01007036
7037 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01007038 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02007039 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01007040 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01007041 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007042 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01007043 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007044 /* fall through for HTTP_MSG_CHUNK_CRLF */
7045
7046 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
7047 /* we want the CRLF after the data */
7048
7049 ret = http_skip_chunk_crlf(msg);
7050 if (ret == 0)
7051 goto missing_data;
7052 else if (ret < 0) {
7053 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007054 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007055 goto return_bad_res;
7056 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007057 /* we're in MSG_CHUNK_SIZE now, fall through */
7058
7059 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01007060 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02007061 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01007062 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01007063 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007064
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007065 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02007066 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01007067 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007068 else if (ret < 0) {
7069 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007070 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007071 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007072 }
Willy Tarreau0161d622013-04-02 01:26:55 +02007073 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007074 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01007075
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007076 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02007077 if (unlikely(compressing)) {
7078 /* we need to flush output contents before syncing FSMs */
7079 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7080 compressing = 0;
7081 }
7082
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007083 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007084 if (ret == 0)
7085 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007086 else if (ret < 0) {
7087 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007088 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007089 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007090 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02007091 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007092
7093 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01007094 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007095 if (unlikely(compressing)) {
7096 /* we need to flush output contents before syncing FSMs */
7097 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7098 compressing = 0;
7099 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007100
Willy Tarreauc623c172014-04-18 09:53:50 +02007101 /* we may have some pending data starting at res->buf->p
7102 * such as a last chunk of data or trailers.
7103 */
7104 b_adv(res->buf, msg->next);
7105 msg->next = 0;
7106
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007107 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02007108 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007109 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7110 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007111 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02007112
Willy Tarreau610ecce2010-01-04 21:15:02 +01007113 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01007114 /* some state changes occurred, maybe the analyser
7115 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01007116 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007117 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007118 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007119 /* response errors are most likely due to
7120 * the client aborting the transfer.
7121 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007122 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007123 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007124 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007125 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01007126 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007127 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007128 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01007129 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007130 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007131 }
7132 }
7133
Willy Tarreaud98cf932009-12-27 22:54:55 +01007134 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02007135 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007136 if (unlikely(compressing)) {
7137 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02007138 compressing = 0;
7139 }
Willy Tarreauf003d372012-11-26 13:35:37 +01007140
Willy Tarreauc623c172014-04-18 09:53:50 +02007141 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
7142 b_adv(res->buf, msg->next);
7143 msg->next = 0;
7144 msg->chunk_len -= channel_forward(res, msg->chunk_len);
7145 }
7146
Willy Tarreauf003d372012-11-26 13:35:37 +01007147 if (res->flags & CF_SHUTW)
7148 goto aborted_xfer;
7149
7150 /* stop waiting for data if the input is closed before the end. If the
7151 * client side was already closed, it means that the client has aborted,
7152 * so we don't want to count this as a server abort. Otherwise it's a
7153 * server abort.
7154 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007155 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007156 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01007157 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007158 if (!(s->flags & SF_ERR_MASK))
7159 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007160 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007161 if (objt_server(s->target))
7162 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007163 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01007164 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01007165
Willy Tarreau40dba092010-03-04 18:14:51 +01007166 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007167 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01007168 goto return_bad_res;
7169
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007170 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007171 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007172 * Similarly, with keep-alive on the client side, we don't want to forward a
7173 * close.
7174 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007175 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007176 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7177 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007178 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007179
Willy Tarreau5c620922011-05-11 19:56:11 +02007180 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007181 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007182 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007183 * modes are already handled by the stream sock layer. We must not do
7184 * this in content-length mode because it could present the MSG_MORE
7185 * flag with the last block of forwarded data, which would cause an
7186 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007187 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007188 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007189 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007190
Willy Tarreau87b09662015-04-03 00:22:06 +02007191 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007192 return 0;
7193
Willy Tarreau40dba092010-03-04 18:14:51 +01007194 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007195 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007196 if (objt_server(s->target))
7197 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007198
7199 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02007200 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02007201 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02007202 compressing = 0;
7203 }
7204
Willy Tarreauc623c172014-04-18 09:53:50 +02007205 /* we may have some pending data starting at res->buf->p */
7206 if (s->comp_algo == NULL) {
7207 b_adv(res->buf, msg->next);
7208 msg->next = 0;
7209 }
7210
Willy Tarreaud98cf932009-12-27 22:54:55 +01007211 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007212 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007213 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007214 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007215 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007216 if (objt_server(s->target))
7217 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007218
Willy Tarreaue7dff022015-04-03 01:14:29 +02007219 if (!(s->flags & SF_ERR_MASK))
7220 s->flags |= SF_ERR_PRXCOND;
7221 if (!(s->flags & SF_FINST_MASK))
7222 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007223 return 0;
7224
7225 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02007226 if (unlikely(compressing)) {
7227 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
7228 compressing = 0;
7229 }
7230
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007231 txn->rsp.msg_state = HTTP_MSG_ERROR;
7232 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007233 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007234 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007235 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007236
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007237 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007238 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007239 if (objt_server(s->target))
7240 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007241
Willy Tarreaue7dff022015-04-03 01:14:29 +02007242 if (!(s->flags & SF_ERR_MASK))
7243 s->flags |= SF_ERR_CLICL;
7244 if (!(s->flags & SF_FINST_MASK))
7245 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007246 return 0;
7247}
7248
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007249/* Iterate the same filter through all request headers.
7250 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007251 * Since it can manage the switch to another backend, it updates the per-proxy
7252 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007253 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007254int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007255{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007256 char *cur_ptr, *cur_end, *cur_next;
7257 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007258 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007259 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007260 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007261
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007262 last_hdr = 0;
7263
Willy Tarreau9b28e032012-10-12 23:49:43 +02007264 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007265 old_idx = 0;
7266
7267 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007268 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007269 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007270 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007271 (exp->action == ACT_ALLOW ||
7272 exp->action == ACT_DENY ||
7273 exp->action == ACT_TARPIT))
7274 return 0;
7275
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007276 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007277 if (!cur_idx)
7278 break;
7279
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007280 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007281 cur_ptr = cur_next;
7282 cur_end = cur_ptr + cur_hdr->len;
7283 cur_next = cur_end + cur_hdr->cr + 1;
7284
7285 /* Now we have one header between cur_ptr and cur_end,
7286 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007287 */
7288
Willy Tarreau15a53a42015-01-21 13:39:42 +01007289 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007290 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007291 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007292 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007293 last_hdr = 1;
7294 break;
7295
7296 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007297 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007298 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007299 break;
7300
7301 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007302 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007303 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007304 break;
7305
7306 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007307 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7308 if (trash.len < 0)
7309 return -1;
7310
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007311 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007312 /* FIXME: if the user adds a newline in the replacement, the
7313 * index will not be recalculated for now, and the new line
7314 * will not be counted as a new header.
7315 */
7316
7317 cur_end += delta;
7318 cur_next += delta;
7319 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007320 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007321 break;
7322
7323 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007324 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007325 cur_next += delta;
7326
Willy Tarreaufa355d42009-11-29 18:12:29 +01007327 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007328 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7329 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007330 cur_hdr->len = 0;
7331 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007332 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007333 break;
7334
7335 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007336 }
7337
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007338 /* keep the link from this header to next one in case of later
7339 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007340 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007341 old_idx = cur_idx;
7342 }
7343 return 0;
7344}
7345
7346
7347/* Apply the filter to the request line.
7348 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7349 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007350 * Since it can manage the switch to another backend, it updates the per-proxy
7351 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007352 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007353int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007354{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007355 char *cur_ptr, *cur_end;
7356 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007357 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007358 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007359
Willy Tarreau3d300592007-03-18 18:34:41 +01007360 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007361 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007362 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007363 (exp->action == ACT_ALLOW ||
7364 exp->action == ACT_DENY ||
7365 exp->action == ACT_TARPIT))
7366 return 0;
7367 else if (exp->action == ACT_REMOVE)
7368 return 0;
7369
7370 done = 0;
7371
Willy Tarreau9b28e032012-10-12 23:49:43 +02007372 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007373 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007374
7375 /* Now we have the request line between cur_ptr and cur_end */
7376
Willy Tarreau15a53a42015-01-21 13:39:42 +01007377 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007378 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007379 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007380 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007381 done = 1;
7382 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007383
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007384 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007385 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007386 done = 1;
7387 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007388
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007389 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007390 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007391 done = 1;
7392 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007393
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007394 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007395 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7396 if (trash.len < 0)
7397 return -1;
7398
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007399 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007400 /* FIXME: if the user adds a newline in the replacement, the
7401 * index will not be recalculated for now, and the new line
7402 * will not be counted as a new header.
7403 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007404
Willy Tarreaufa355d42009-11-29 18:12:29 +01007405 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007406 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007407 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007408 HTTP_MSG_RQMETH,
7409 cur_ptr, cur_end + 1,
7410 NULL, NULL);
7411 if (unlikely(!cur_end))
7412 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007413
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007414 /* we have a full request and we know that we have either a CR
7415 * or an LF at <ptr>.
7416 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007417 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7418 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007419 /* there is no point trying this regex on headers */
7420 return 1;
7421 }
7422 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007423 return done;
7424}
Willy Tarreau97de6242006-12-27 17:18:38 +01007425
Willy Tarreau58f10d72006-12-04 02:26:12 +01007426
Willy Tarreau58f10d72006-12-04 02:26:12 +01007427
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007428/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007429 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007430 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007431 * unparsable request. Since it can manage the switch to another backend, it
7432 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007433 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007434int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007435{
Willy Tarreau192252e2015-04-04 01:47:55 +02007436 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007437 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007438 struct hdr_exp *exp;
7439
7440 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007441 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007442
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007443 /*
7444 * The interleaving of transformations and verdicts
7445 * makes it difficult to decide to continue or stop
7446 * the evaluation.
7447 */
7448
Willy Tarreau6c123b12010-01-28 20:22:06 +01007449 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7450 break;
7451
Willy Tarreau3d300592007-03-18 18:34:41 +01007452 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007453 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007454 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007455 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007456
7457 /* if this filter had a condition, evaluate it now and skip to
7458 * next filter if the condition does not match.
7459 */
7460 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007461 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007462 ret = acl_pass(ret);
7463 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7464 ret = !ret;
7465
7466 if (!ret)
7467 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007468 }
7469
7470 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007471 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007472 if (unlikely(ret < 0))
7473 return -1;
7474
7475 if (likely(ret == 0)) {
7476 /* The filter did not match the request, it can be
7477 * iterated through all headers.
7478 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007479 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7480 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007481 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007482 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007483 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007484}
7485
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007486
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007487/* Find the end of a cookie value contained between <s> and <e>. It works the
7488 * same way as with headers above except that the semi-colon also ends a token.
7489 * See RFC2965 for more information. Note that it requires a valid header to
7490 * return a valid result.
7491 */
7492char *find_cookie_value_end(char *s, const char *e)
7493{
7494 int quoted, qdpair;
7495
7496 quoted = qdpair = 0;
7497 for (; s < e; s++) {
7498 if (qdpair) qdpair = 0;
7499 else if (quoted) {
7500 if (*s == '\\') qdpair = 1;
7501 else if (*s == '"') quoted = 0;
7502 }
7503 else if (*s == '"') quoted = 1;
7504 else if (*s == ',' || *s == ';') return s;
7505 }
7506 return s;
7507}
7508
7509/* Delete a value in a header between delimiters <from> and <next> in buffer
7510 * <buf>. The number of characters displaced is returned, and the pointer to
7511 * the first delimiter is updated if required. The function tries as much as
7512 * possible to respect the following principles :
7513 * - replace <from> delimiter by the <next> one unless <from> points to a
7514 * colon, in which case <next> is simply removed
7515 * - set exactly one space character after the new first delimiter, unless
7516 * there are not enough characters in the block being moved to do so.
7517 * - remove unneeded spaces before the previous delimiter and after the new
7518 * one.
7519 *
7520 * It is the caller's responsibility to ensure that :
7521 * - <from> points to a valid delimiter or the colon ;
7522 * - <next> points to a valid delimiter or the final CR/LF ;
7523 * - there are non-space chars before <from> ;
7524 * - there is a CR/LF at or after <next>.
7525 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007526int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007527{
7528 char *prev = *from;
7529
7530 if (*prev == ':') {
7531 /* We're removing the first value, preserve the colon and add a
7532 * space if possible.
7533 */
7534 if (!http_is_crlf[(unsigned char)*next])
7535 next++;
7536 prev++;
7537 if (prev < next)
7538 *prev++ = ' ';
7539
7540 while (http_is_spht[(unsigned char)*next])
7541 next++;
7542 } else {
7543 /* Remove useless spaces before the old delimiter. */
7544 while (http_is_spht[(unsigned char)*(prev-1)])
7545 prev--;
7546 *from = prev;
7547
7548 /* copy the delimiter and if possible a space if we're
7549 * not at the end of the line.
7550 */
7551 if (!http_is_crlf[(unsigned char)*next]) {
7552 *prev++ = *next++;
7553 if (prev + 1 < next)
7554 *prev++ = ' ';
7555 while (http_is_spht[(unsigned char)*next])
7556 next++;
7557 }
7558 }
7559 return buffer_replace2(buf, prev, next, NULL, 0);
7560}
7561
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007562/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007563 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007564 * desirable to call it only when needed. This code is quite complex because
7565 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7566 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007567 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007568void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007569{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007570 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007571 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007572 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007573 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007574 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7575 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007576
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007577 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007578 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007579 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007580
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007581 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007582 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007583 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007584
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007585 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007586 hdr_beg = hdr_next;
7587 hdr_end = hdr_beg + cur_hdr->len;
7588 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007589
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007590 /* We have one full header between hdr_beg and hdr_end, and the
7591 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007592 * "Cookie:" headers.
7593 */
7594
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007595 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007596 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007597 old_idx = cur_idx;
7598 continue;
7599 }
7600
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007601 del_from = NULL; /* nothing to be deleted */
7602 preserve_hdr = 0; /* assume we may kill the whole header */
7603
Willy Tarreau58f10d72006-12-04 02:26:12 +01007604 /* Now look for cookies. Conforming to RFC2109, we have to support
7605 * attributes whose name begin with a '$', and associate them with
7606 * the right cookie, if we want to delete this cookie.
7607 * So there are 3 cases for each cookie read :
7608 * 1) it's a special attribute, beginning with a '$' : ignore it.
7609 * 2) it's a server id cookie that we *MAY* want to delete : save
7610 * some pointers on it (last semi-colon, beginning of cookie...)
7611 * 3) it's an application cookie : we *MAY* have to delete a previous
7612 * "special" cookie.
7613 * At the end of loop, if a "special" cookie remains, we may have to
7614 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007615 * *MUST* delete it.
7616 *
7617 * Note: RFC2965 is unclear about the processing of spaces around
7618 * the equal sign in the ATTR=VALUE form. A careful inspection of
7619 * the RFC explicitly allows spaces before it, and not within the
7620 * tokens (attrs or values). An inspection of RFC2109 allows that
7621 * too but section 10.1.3 lets one think that spaces may be allowed
7622 * after the equal sign too, resulting in some (rare) buggy
7623 * implementations trying to do that. So let's do what servers do.
7624 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7625 * allowed quoted strings in values, with any possible character
7626 * after a backslash, including control chars and delimitors, which
7627 * causes parsing to become ambiguous. Browsers also allow spaces
7628 * within values even without quotes.
7629 *
7630 * We have to keep multiple pointers in order to support cookie
7631 * removal at the beginning, middle or end of header without
7632 * corrupting the header. All of these headers are valid :
7633 *
7634 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7635 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7636 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7637 * | | | | | | | | |
7638 * | | | | | | | | hdr_end <--+
7639 * | | | | | | | +--> next
7640 * | | | | | | +----> val_end
7641 * | | | | | +-----------> val_beg
7642 * | | | | +--------------> equal
7643 * | | | +----------------> att_end
7644 * | | +---------------------> att_beg
7645 * | +--------------------------> prev
7646 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007647 */
7648
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007649 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7650 /* Iterate through all cookies on this line */
7651
7652 /* find att_beg */
7653 att_beg = prev + 1;
7654 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7655 att_beg++;
7656
7657 /* find att_end : this is the first character after the last non
7658 * space before the equal. It may be equal to hdr_end.
7659 */
7660 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007661
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007662 while (equal < hdr_end) {
7663 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007664 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007665 if (http_is_spht[(unsigned char)*equal++])
7666 continue;
7667 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007668 }
7669
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007670 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7671 * is between <att_beg> and <equal>, both may be identical.
7672 */
7673
7674 /* look for end of cookie if there is an equal sign */
7675 if (equal < hdr_end && *equal == '=') {
7676 /* look for the beginning of the value */
7677 val_beg = equal + 1;
7678 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7679 val_beg++;
7680
7681 /* find the end of the value, respecting quotes */
7682 next = find_cookie_value_end(val_beg, hdr_end);
7683
7684 /* make val_end point to the first white space or delimitor after the value */
7685 val_end = next;
7686 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7687 val_end--;
7688 } else {
7689 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007690 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007691
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007692 /* We have nothing to do with attributes beginning with '$'. However,
7693 * they will automatically be removed if a header before them is removed,
7694 * since they're supposed to be linked together.
7695 */
7696 if (*att_beg == '$')
7697 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007698
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007699 /* Ignore cookies with no equal sign */
7700 if (equal == next) {
7701 /* This is not our cookie, so we must preserve it. But if we already
7702 * scheduled another cookie for removal, we cannot remove the
7703 * complete header, but we can remove the previous block itself.
7704 */
7705 preserve_hdr = 1;
7706 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007707 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007708 val_end += delta;
7709 next += delta;
7710 hdr_end += delta;
7711 hdr_next += delta;
7712 cur_hdr->len += delta;
7713 http_msg_move_end(&txn->req, delta);
7714 prev = del_from;
7715 del_from = NULL;
7716 }
7717 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007718 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007719
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007720 /* if there are spaces around the equal sign, we need to
7721 * strip them otherwise we'll get trouble for cookie captures,
7722 * or even for rewrites. Since this happens extremely rarely,
7723 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007724 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007725 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7726 int stripped_before = 0;
7727 int stripped_after = 0;
7728
7729 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007730 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007731 equal += stripped_before;
7732 val_beg += stripped_before;
7733 }
7734
7735 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007736 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007737 val_beg += stripped_after;
7738 stripped_before += stripped_after;
7739 }
7740
7741 val_end += stripped_before;
7742 next += stripped_before;
7743 hdr_end += stripped_before;
7744 hdr_next += stripped_before;
7745 cur_hdr->len += stripped_before;
7746 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007747 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007748 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007749
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007750 /* First, let's see if we want to capture this cookie. We check
7751 * that we don't already have a client side cookie, because we
7752 * can only capture one. Also as an optimisation, we ignore
7753 * cookies shorter than the declared name.
7754 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007755 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7756 (val_end - att_beg >= sess->fe->capture_namelen) &&
7757 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007758 int log_len = val_end - att_beg;
7759
7760 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7761 Alert("HTTP logging : out of memory.\n");
7762 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007763 if (log_len > sess->fe->capture_len)
7764 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007765 memcpy(txn->cli_cookie, att_beg, log_len);
7766 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007767 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007768 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007769
Willy Tarreaubca99692010-10-06 19:25:55 +02007770 /* Persistence cookies in passive, rewrite or insert mode have the
7771 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007772 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007773 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007774 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007775 * For cookies in prefix mode, the form is :
7776 *
7777 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007778 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007779 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7780 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7781 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007782 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007783
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007784 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7785 * have the server ID between val_beg and delim, and the original cookie between
7786 * delim+1 and val_end. Otherwise, delim==val_end :
7787 *
7788 * Cookie: NAME=SRV; # in all but prefix modes
7789 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7790 * | || || | |+-> next
7791 * | || || | +--> val_end
7792 * | || || +---------> delim
7793 * | || |+------------> val_beg
7794 * | || +-------------> att_end = equal
7795 * | |+-----------------> att_beg
7796 * | +------------------> prev
7797 * +-------------------------> hdr_beg
7798 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007799
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007800 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007801 for (delim = val_beg; delim < val_end; delim++)
7802 if (*delim == COOKIE_DELIM)
7803 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007804 } else {
7805 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007806 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007807 /* Now check if the cookie contains a date field, which would
7808 * appear after a vertical bar ('|') just after the server name
7809 * and before the delimiter.
7810 */
7811 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7812 if (vbar1) {
7813 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007814 * right is the last seen date. It is a base64 encoded
7815 * 30-bit value representing the UNIX date since the
7816 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007817 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007818 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007819 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007820 if (val_end - vbar1 >= 5) {
7821 val = b64tos30(vbar1);
7822 if (val > 0)
7823 txn->cookie_last_date = val << 2;
7824 }
7825 /* look for a second vertical bar */
7826 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7827 if (vbar1 && (val_end - vbar1 > 5)) {
7828 val = b64tos30(vbar1 + 1);
7829 if (val > 0)
7830 txn->cookie_first_date = val << 2;
7831 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007832 }
7833 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007834
Willy Tarreauf64d1412010-10-07 20:06:11 +02007835 /* if the cookie has an expiration date and the proxy wants to check
7836 * it, then we do that now. We first check if the cookie is too old,
7837 * then only if it has expired. We detect strict overflow because the
7838 * time resolution here is not great (4 seconds). Cookies with dates
7839 * in the future are ignored if their offset is beyond one day. This
7840 * allows an admin to fix timezone issues without expiring everyone
7841 * and at the same time avoids keeping unwanted side effects for too
7842 * long.
7843 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007844 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7845 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007846 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007847 txn->flags &= ~TX_CK_MASK;
7848 txn->flags |= TX_CK_OLD;
7849 delim = val_beg; // let's pretend we have not found the cookie
7850 txn->cookie_first_date = 0;
7851 txn->cookie_last_date = 0;
7852 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007853 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7854 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007855 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007856 txn->flags &= ~TX_CK_MASK;
7857 txn->flags |= TX_CK_EXPIRED;
7858 delim = val_beg; // let's pretend we have not found the cookie
7859 txn->cookie_first_date = 0;
7860 txn->cookie_last_date = 0;
7861 }
7862
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007863 /* Here, we'll look for the first running server which supports the cookie.
7864 * This allows to share a same cookie between several servers, for example
7865 * to dedicate backup servers to specific servers only.
7866 * However, to prevent clients from sticking to cookie-less backup server
7867 * when they have incidentely learned an empty cookie, we simply ignore
7868 * empty cookies and mark them as invalid.
7869 * The same behaviour is applied when persistence must be ignored.
7870 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007871 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007872 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007873
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007874 while (srv) {
7875 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7876 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007877 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007878 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007879 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007880 /* we found the server and we can use it */
7881 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007882 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007883 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007884 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007885 break;
7886 } else {
7887 /* we found a server, but it's down,
7888 * mark it as such and go on in case
7889 * another one is available.
7890 */
7891 txn->flags &= ~TX_CK_MASK;
7892 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007893 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007894 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007895 srv = srv->next;
7896 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007897
Willy Tarreauf64d1412010-10-07 20:06:11 +02007898 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007899 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007900 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007901 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007902 txn->flags |= TX_CK_UNUSED;
7903 else
7904 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007905 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007906
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007907 /* depending on the cookie mode, we may have to either :
7908 * - delete the complete cookie if we're in insert+indirect mode, so that
7909 * the server never sees it ;
7910 * - remove the server id from the cookie value, and tag the cookie as an
7911 * application cookie so that it does not get accidentely removed later,
7912 * if we're in cookie prefix mode
7913 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007914 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007915 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007916
Willy Tarreau9b28e032012-10-12 23:49:43 +02007917 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007918 val_end += delta;
7919 next += delta;
7920 hdr_end += delta;
7921 hdr_next += delta;
7922 cur_hdr->len += delta;
7923 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007924
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007925 del_from = NULL;
7926 preserve_hdr = 1; /* we want to keep this cookie */
7927 }
7928 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007929 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007930 del_from = prev;
7931 }
7932 } else {
7933 /* This is not our cookie, so we must preserve it. But if we already
7934 * scheduled another cookie for removal, we cannot remove the
7935 * complete header, but we can remove the previous block itself.
7936 */
7937 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007938
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007939 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007940 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007941 if (att_beg >= del_from)
7942 att_beg += delta;
7943 if (att_end >= del_from)
7944 att_end += delta;
7945 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007946 val_end += delta;
7947 next += delta;
7948 hdr_end += delta;
7949 hdr_next += delta;
7950 cur_hdr->len += delta;
7951 http_msg_move_end(&txn->req, delta);
7952 prev = del_from;
7953 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007954 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007955 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007956
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007957 /* continue with next cookie on this header line */
7958 att_beg = next;
7959 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007960
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007961 /* There are no more cookies on this line.
7962 * We may still have one (or several) marked for deletion at the
7963 * end of the line. We must do this now in two ways :
7964 * - if some cookies must be preserved, we only delete from the
7965 * mark to the end of line ;
7966 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007967 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007968 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007969 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007970 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007971 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007972 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007973 cur_hdr->len += delta;
7974 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007975 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007976
7977 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007978 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7979 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007980 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007981 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007982 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007983 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007984 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007985 }
7986
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007987 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007988 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007989 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007990}
7991
7992
Willy Tarreaua15645d2007-03-18 16:22:39 +01007993/* Iterate the same filter through all response headers contained in <rtr>.
7994 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7995 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007996int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007997{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007998 char *cur_ptr, *cur_end, *cur_next;
7999 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008000 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008001 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008002 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008003
8004 last_hdr = 0;
8005
Willy Tarreau9b28e032012-10-12 23:49:43 +02008006 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008007 old_idx = 0;
8008
8009 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008010 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008011 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008012 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008013 (exp->action == ACT_ALLOW ||
8014 exp->action == ACT_DENY))
8015 return 0;
8016
8017 cur_idx = txn->hdr_idx.v[old_idx].next;
8018 if (!cur_idx)
8019 break;
8020
8021 cur_hdr = &txn->hdr_idx.v[cur_idx];
8022 cur_ptr = cur_next;
8023 cur_end = cur_ptr + cur_hdr->len;
8024 cur_next = cur_end + cur_hdr->cr + 1;
8025
8026 /* Now we have one header between cur_ptr and cur_end,
8027 * and the next header starts at cur_next.
8028 */
8029
Willy Tarreau15a53a42015-01-21 13:39:42 +01008030 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008031 switch (exp->action) {
8032 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008033 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008034 last_hdr = 1;
8035 break;
8036
8037 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008038 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008039 last_hdr = 1;
8040 break;
8041
8042 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008043 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8044 if (trash.len < 0)
8045 return -1;
8046
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008047 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008048 /* FIXME: if the user adds a newline in the replacement, the
8049 * index will not be recalculated for now, and the new line
8050 * will not be counted as a new header.
8051 */
8052
8053 cur_end += delta;
8054 cur_next += delta;
8055 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008056 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008057 break;
8058
8059 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008060 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008061 cur_next += delta;
8062
Willy Tarreaufa355d42009-11-29 18:12:29 +01008063 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008064 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8065 txn->hdr_idx.used--;
8066 cur_hdr->len = 0;
8067 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008068 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008069 break;
8070
8071 }
8072 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008073
8074 /* keep the link from this header to next one in case of later
8075 * removal of next header.
8076 */
8077 old_idx = cur_idx;
8078 }
8079 return 0;
8080}
8081
8082
8083/* Apply the filter to the status line in the response buffer <rtr>.
8084 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8085 * or -1 if a replacement resulted in an invalid status line.
8086 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008087int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008088{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008089 char *cur_ptr, *cur_end;
8090 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008091 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008092 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008093
8094
Willy Tarreau3d300592007-03-18 18:34:41 +01008095 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008096 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008097 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008098 (exp->action == ACT_ALLOW ||
8099 exp->action == ACT_DENY))
8100 return 0;
8101 else if (exp->action == ACT_REMOVE)
8102 return 0;
8103
8104 done = 0;
8105
Willy Tarreau9b28e032012-10-12 23:49:43 +02008106 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008107 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008108
8109 /* Now we have the status line between cur_ptr and cur_end */
8110
Willy Tarreau15a53a42015-01-21 13:39:42 +01008111 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008112 switch (exp->action) {
8113 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008114 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008115 done = 1;
8116 break;
8117
8118 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008119 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008120 done = 1;
8121 break;
8122
8123 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008124 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8125 if (trash.len < 0)
8126 return -1;
8127
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008128 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008129 /* FIXME: if the user adds a newline in the replacement, the
8130 * index will not be recalculated for now, and the new line
8131 * will not be counted as a new header.
8132 */
8133
Willy Tarreaufa355d42009-11-29 18:12:29 +01008134 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008135 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008136 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008137 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008138 cur_ptr, cur_end + 1,
8139 NULL, NULL);
8140 if (unlikely(!cur_end))
8141 return -1;
8142
8143 /* we have a full respnse and we know that we have either a CR
8144 * or an LF at <ptr>.
8145 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008146 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008147 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008148 /* there is no point trying this regex on headers */
8149 return 1;
8150 }
8151 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008152 return done;
8153}
8154
8155
8156
8157/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008158 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008159 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8160 * unparsable response.
8161 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008162int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008163{
Willy Tarreau192252e2015-04-04 01:47:55 +02008164 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008165 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008166 struct hdr_exp *exp;
8167
8168 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008169 int ret;
8170
8171 /*
8172 * The interleaving of transformations and verdicts
8173 * makes it difficult to decide to continue or stop
8174 * the evaluation.
8175 */
8176
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008177 if (txn->flags & TX_SVDENY)
8178 break;
8179
Willy Tarreau3d300592007-03-18 18:34:41 +01008180 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008181 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8182 exp->action == ACT_PASS)) {
8183 exp = exp->next;
8184 continue;
8185 }
8186
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008187 /* if this filter had a condition, evaluate it now and skip to
8188 * next filter if the condition does not match.
8189 */
8190 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008191 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008192 ret = acl_pass(ret);
8193 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8194 ret = !ret;
8195 if (!ret)
8196 continue;
8197 }
8198
Willy Tarreaua15645d2007-03-18 16:22:39 +01008199 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008200 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008201 if (unlikely(ret < 0))
8202 return -1;
8203
8204 if (likely(ret == 0)) {
8205 /* The filter did not match the response, it can be
8206 * iterated through all headers.
8207 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008208 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8209 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008210 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008211 }
8212 return 0;
8213}
8214
8215
Willy Tarreaua15645d2007-03-18 16:22:39 +01008216/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008217 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008218 * desirable to call it only when needed. This function is also used when we
8219 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008220 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008221void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008222{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008223 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008224 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008225 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008226 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008227 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008228 char *hdr_beg, *hdr_end, *hdr_next;
8229 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008230
Willy Tarreaua15645d2007-03-18 16:22:39 +01008231 /* Iterate through the headers.
8232 * we start with the start line.
8233 */
8234 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008235 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008236
8237 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8238 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008239 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008240
8241 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008242 hdr_beg = hdr_next;
8243 hdr_end = hdr_beg + cur_hdr->len;
8244 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008245
Willy Tarreau24581ba2010-08-31 22:39:35 +02008246 /* We have one full header between hdr_beg and hdr_end, and the
8247 * next header starts at hdr_next. We're only interested in
8248 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008249 */
8250
Willy Tarreau24581ba2010-08-31 22:39:35 +02008251 is_cookie2 = 0;
8252 prev = hdr_beg + 10;
8253 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008254 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008255 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8256 if (!val) {
8257 old_idx = cur_idx;
8258 continue;
8259 }
8260 is_cookie2 = 1;
8261 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008262 }
8263
Willy Tarreau24581ba2010-08-31 22:39:35 +02008264 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8265 * <prev> points to the colon.
8266 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008267 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008268
Willy Tarreau24581ba2010-08-31 22:39:35 +02008269 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8270 * check-cache is enabled) and we are not interested in checking
8271 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008272 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008273 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008274 return;
8275
Willy Tarreau24581ba2010-08-31 22:39:35 +02008276 /* OK so now we know we have to process this response cookie.
8277 * The format of the Set-Cookie header is slightly different
8278 * from the format of the Cookie header in that it does not
8279 * support the comma as a cookie delimiter (thus the header
8280 * cannot be folded) because the Expires attribute described in
8281 * the original Netscape's spec may contain an unquoted date
8282 * with a comma inside. We have to live with this because
8283 * many browsers don't support Max-Age and some browsers don't
8284 * support quoted strings. However the Set-Cookie2 header is
8285 * clean.
8286 *
8287 * We have to keep multiple pointers in order to support cookie
8288 * removal at the beginning, middle or end of header without
8289 * corrupting the header (in case of set-cookie2). A special
8290 * pointer, <scav> points to the beginning of the set-cookie-av
8291 * fields after the first semi-colon. The <next> pointer points
8292 * either to the end of line (set-cookie) or next unquoted comma
8293 * (set-cookie2). All of these headers are valid :
8294 *
8295 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8296 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8297 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8298 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8299 * | | | | | | | | | |
8300 * | | | | | | | | +-> next hdr_end <--+
8301 * | | | | | | | +------------> scav
8302 * | | | | | | +--------------> val_end
8303 * | | | | | +--------------------> val_beg
8304 * | | | | +----------------------> equal
8305 * | | | +------------------------> att_end
8306 * | | +----------------------------> att_beg
8307 * | +------------------------------> prev
8308 * +-----------------------------------------> hdr_beg
8309 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008310
Willy Tarreau24581ba2010-08-31 22:39:35 +02008311 for (; prev < hdr_end; prev = next) {
8312 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008313
Willy Tarreau24581ba2010-08-31 22:39:35 +02008314 /* find att_beg */
8315 att_beg = prev + 1;
8316 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8317 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008318
Willy Tarreau24581ba2010-08-31 22:39:35 +02008319 /* find att_end : this is the first character after the last non
8320 * space before the equal. It may be equal to hdr_end.
8321 */
8322 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008323
Willy Tarreau24581ba2010-08-31 22:39:35 +02008324 while (equal < hdr_end) {
8325 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8326 break;
8327 if (http_is_spht[(unsigned char)*equal++])
8328 continue;
8329 att_end = equal;
8330 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008331
Willy Tarreau24581ba2010-08-31 22:39:35 +02008332 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8333 * is between <att_beg> and <equal>, both may be identical.
8334 */
8335
8336 /* look for end of cookie if there is an equal sign */
8337 if (equal < hdr_end && *equal == '=') {
8338 /* look for the beginning of the value */
8339 val_beg = equal + 1;
8340 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8341 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008342
Willy Tarreau24581ba2010-08-31 22:39:35 +02008343 /* find the end of the value, respecting quotes */
8344 next = find_cookie_value_end(val_beg, hdr_end);
8345
8346 /* make val_end point to the first white space or delimitor after the value */
8347 val_end = next;
8348 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8349 val_end--;
8350 } else {
8351 /* <equal> points to next comma, semi-colon or EOL */
8352 val_beg = val_end = next = equal;
8353 }
8354
8355 if (next < hdr_end) {
8356 /* Set-Cookie2 supports multiple cookies, and <next> points to
8357 * a colon or semi-colon before the end. So skip all attr-value
8358 * pairs and look for the next comma. For Set-Cookie, since
8359 * commas are permitted in values, skip to the end.
8360 */
8361 if (is_cookie2)
8362 next = find_hdr_value_end(next, hdr_end);
8363 else
8364 next = hdr_end;
8365 }
8366
8367 /* Now everything is as on the diagram above */
8368
8369 /* Ignore cookies with no equal sign */
8370 if (equal == val_end)
8371 continue;
8372
8373 /* If there are spaces around the equal sign, we need to
8374 * strip them otherwise we'll get trouble for cookie captures,
8375 * or even for rewrites. Since this happens extremely rarely,
8376 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008377 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008378 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8379 int stripped_before = 0;
8380 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008381
Willy Tarreau24581ba2010-08-31 22:39:35 +02008382 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008383 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008384 equal += stripped_before;
8385 val_beg += stripped_before;
8386 }
8387
8388 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008389 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008390 val_beg += stripped_after;
8391 stripped_before += stripped_after;
8392 }
8393
8394 val_end += stripped_before;
8395 next += stripped_before;
8396 hdr_end += stripped_before;
8397 hdr_next += stripped_before;
8398 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008399 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008400 }
8401
8402 /* First, let's see if we want to capture this cookie. We check
8403 * that we don't already have a server side cookie, because we
8404 * can only capture one. Also as an optimisation, we ignore
8405 * cookies shorter than the declared name.
8406 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008407 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008408 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008409 (val_end - att_beg >= sess->fe->capture_namelen) &&
8410 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008411 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008412 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008413 Alert("HTTP logging : out of memory.\n");
8414 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008415 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008416 if (log_len > sess->fe->capture_len)
8417 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008418 memcpy(txn->srv_cookie, att_beg, log_len);
8419 txn->srv_cookie[log_len] = 0;
8420 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008421 }
8422
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008423 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008424 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008425 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008426 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8427 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008428 /* assume passive cookie by default */
8429 txn->flags &= ~TX_SCK_MASK;
8430 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008431
8432 /* If the cookie is in insert mode on a known server, we'll delete
8433 * this occurrence because we'll insert another one later.
8434 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008435 * a direct access.
8436 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008437 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008438 /* The "preserve" flag was set, we don't want to touch the
8439 * server's cookie.
8440 */
8441 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008442 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008443 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008444 /* this cookie must be deleted */
8445 if (*prev == ':' && next == hdr_end) {
8446 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008447 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008448 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8449 txn->hdr_idx.used--;
8450 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008451 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008452 hdr_next += delta;
8453 http_msg_move_end(&txn->rsp, delta);
8454 /* note: while both invalid now, <next> and <hdr_end>
8455 * are still equal, so the for() will stop as expected.
8456 */
8457 } else {
8458 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008459 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008460 next = prev;
8461 hdr_end += delta;
8462 hdr_next += delta;
8463 cur_hdr->len += delta;
8464 http_msg_move_end(&txn->rsp, delta);
8465 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008466 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008467 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008468 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008469 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008470 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008471 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008472 * with this server since we know it.
8473 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008474 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008475 next += delta;
8476 hdr_end += delta;
8477 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008478 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008479 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008480
Willy Tarreauf1348312010-10-07 15:54:11 +02008481 txn->flags &= ~TX_SCK_MASK;
8482 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008483 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008484 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008485 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008486 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008487 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008488 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008489 next += delta;
8490 hdr_end += delta;
8491 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008492 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008493 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008494
Willy Tarreau827aee92011-03-10 16:55:02 +01008495 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008496 txn->flags &= ~TX_SCK_MASK;
8497 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008498 }
8499 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008500 /* that's done for this cookie, check the next one on the same
8501 * line when next != hdr_end (only if is_cookie2).
8502 */
8503 }
8504 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008505 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008506 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008507}
8508
8509
Willy Tarreaua15645d2007-03-18 16:22:39 +01008510/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008511 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008512 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008513void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008514{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008515 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008516 char *p1, *p2;
8517
8518 char *cur_ptr, *cur_end, *cur_next;
8519 int cur_idx;
8520
Willy Tarreau5df51872007-11-25 16:20:08 +01008521 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008522 return;
8523
8524 /* Iterate through the headers.
8525 * we start with the start line.
8526 */
8527 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008528 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008529
8530 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8531 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008532 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008533
8534 cur_hdr = &txn->hdr_idx.v[cur_idx];
8535 cur_ptr = cur_next;
8536 cur_end = cur_ptr + cur_hdr->len;
8537 cur_next = cur_end + cur_hdr->cr + 1;
8538
8539 /* We have one full header between cur_ptr and cur_end, and the
8540 * next header starts at cur_next. We're only interested in
8541 * "Cookie:" headers.
8542 */
8543
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008544 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8545 if (val) {
8546 if ((cur_end - (cur_ptr + val) >= 8) &&
8547 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8548 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8549 return;
8550 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008551 }
8552
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008553 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8554 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008555 continue;
8556
8557 /* OK, right now we know we have a cache-control header at cur_ptr */
8558
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008559 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008560
8561 if (p1 >= cur_end) /* no more info */
8562 continue;
8563
8564 /* p1 is at the beginning of the value */
8565 p2 = p1;
8566
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008567 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008568 p2++;
8569
8570 /* we have a complete value between p1 and p2 */
8571 if (p2 < cur_end && *p2 == '=') {
8572 /* we have something of the form no-cache="set-cookie" */
8573 if ((cur_end - p1 >= 21) &&
8574 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8575 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008576 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008577 continue;
8578 }
8579
8580 /* OK, so we know that either p2 points to the end of string or to a comma */
8581 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008582 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008583 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8584 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8585 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008586 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008587 return;
8588 }
8589
8590 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008591 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008592 continue;
8593 }
8594 }
8595}
8596
Willy Tarreau58f10d72006-12-04 02:26:12 +01008597
Willy Tarreaub2513902006-12-17 14:52:38 +01008598/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008599 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008600 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008601 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008602 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008603 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008604 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008605 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008606 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008607int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008608{
8609 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008610 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008611 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008612
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008613 if (!uri_auth)
8614 return 0;
8615
Cyril Bonté70be45d2010-10-12 00:14:35 +02008616 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008617 return 0;
8618
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008619 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008620 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008621 return 0;
8622
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008623 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008624 return 0;
8625
Willy Tarreaub2513902006-12-17 14:52:38 +01008626 return 1;
8627}
8628
Willy Tarreau4076a152009-04-02 15:18:36 +02008629/*
8630 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008631 * By default it tries to report the error position as msg->err_pos. However if
8632 * this one is not set, it will then report msg->next, which is the last known
8633 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008634 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008635 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008636void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008637 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008638 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008639{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008640 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008641 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008642 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008643
Willy Tarreau9b28e032012-10-12 23:49:43 +02008644 es->len = MIN(chn->buf->i, sizeof(es->buf));
8645 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008646 len1 = MIN(len1, es->len);
8647 len2 = es->len - len1; /* remaining data if buffer wraps */
8648
Willy Tarreau9b28e032012-10-12 23:49:43 +02008649 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008650 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008651 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008652
Willy Tarreau4076a152009-04-02 15:18:36 +02008653 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008654 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008655 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008656 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008657
Willy Tarreau4076a152009-04-02 15:18:36 +02008658 es->when = date; // user-visible date
8659 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008660 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008661 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008662 if (objt_conn(sess->origin))
8663 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008664 else
8665 memset(&es->src, 0, sizeof(es->src));
8666
Willy Tarreau078272e2010-12-12 12:46:33 +01008667 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008668 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008669 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008670 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008671 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008672 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008673 es->b_out = chn->buf->o;
8674 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008675 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008676 es->m_clen = msg->chunk_len;
8677 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008678}
Willy Tarreaub2513902006-12-17 14:52:38 +01008679
Willy Tarreau294c4732011-12-16 21:35:50 +01008680/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8681 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8682 * performed over the whole headers. Otherwise it must contain a valid header
8683 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8684 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8685 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8686 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008687 * -1. The value fetch stops at commas, so this function is suited for use with
8688 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008689 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008690 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008691unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008692 struct hdr_idx *idx, int occ,
8693 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008694{
Willy Tarreau294c4732011-12-16 21:35:50 +01008695 struct hdr_ctx local_ctx;
8696 char *ptr_hist[MAX_HDR_HISTORY];
8697 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008698 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008699 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008700
Willy Tarreau294c4732011-12-16 21:35:50 +01008701 if (!ctx) {
8702 local_ctx.idx = 0;
8703 ctx = &local_ctx;
8704 }
8705
Willy Tarreaubce70882009-09-07 11:51:47 +02008706 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008707 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008708 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008709 occ--;
8710 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008711 *vptr = ctx->line + ctx->val;
8712 *vlen = ctx->vlen;
8713 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008714 }
8715 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008716 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008717 }
8718
8719 /* negative occurrence, we scan all the list then walk back */
8720 if (-occ > MAX_HDR_HISTORY)
8721 return 0;
8722
Willy Tarreau294c4732011-12-16 21:35:50 +01008723 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008724 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008725 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8726 len_hist[hist_ptr] = ctx->vlen;
8727 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008728 hist_ptr = 0;
8729 found++;
8730 }
8731 if (-occ > found)
8732 return 0;
8733 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008734 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8735 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8736 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008737 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008738 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008739 if (hist_ptr >= MAX_HDR_HISTORY)
8740 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008741 *vptr = ptr_hist[hist_ptr];
8742 *vlen = len_hist[hist_ptr];
8743 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008744}
8745
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008746/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8747 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8748 * performed over the whole headers. Otherwise it must contain a valid header
8749 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8750 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8751 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8752 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8753 * -1. This function differs from http_get_hdr() in that it only returns full
8754 * line header values and does not stop at commas.
8755 * The return value is 0 if nothing was found, or non-zero otherwise.
8756 */
8757unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8758 struct hdr_idx *idx, int occ,
8759 struct hdr_ctx *ctx, char **vptr, int *vlen)
8760{
8761 struct hdr_ctx local_ctx;
8762 char *ptr_hist[MAX_HDR_HISTORY];
8763 int len_hist[MAX_HDR_HISTORY];
8764 unsigned int hist_ptr;
8765 int found;
8766
8767 if (!ctx) {
8768 local_ctx.idx = 0;
8769 ctx = &local_ctx;
8770 }
8771
8772 if (occ >= 0) {
8773 /* search from the beginning */
8774 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8775 occ--;
8776 if (occ <= 0) {
8777 *vptr = ctx->line + ctx->val;
8778 *vlen = ctx->vlen;
8779 return 1;
8780 }
8781 }
8782 return 0;
8783 }
8784
8785 /* negative occurrence, we scan all the list then walk back */
8786 if (-occ > MAX_HDR_HISTORY)
8787 return 0;
8788
8789 found = hist_ptr = 0;
8790 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8791 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8792 len_hist[hist_ptr] = ctx->vlen;
8793 if (++hist_ptr >= MAX_HDR_HISTORY)
8794 hist_ptr = 0;
8795 found++;
8796 }
8797 if (-occ > found)
8798 return 0;
8799 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8800 * find occurrence -occ, so we have to check [hist_ptr+occ].
8801 */
8802 hist_ptr += occ;
8803 if (hist_ptr >= MAX_HDR_HISTORY)
8804 hist_ptr -= MAX_HDR_HISTORY;
8805 *vptr = ptr_hist[hist_ptr];
8806 *vlen = len_hist[hist_ptr];
8807 return 1;
8808}
8809
Willy Tarreaubaaee002006-06-26 02:48:02 +02008810/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008811 * Print a debug line with a header. Always stop at the first CR or LF char,
8812 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8813 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008814 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008815void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008816{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008817 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008818 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008819
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008820 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008821 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008822 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008823 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 +02008824
8825 for (max = 0; start + max < end; max++)
8826 if (start[max] == '\r' || start[max] == '\n')
8827 break;
8828
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008829 UBOUND(max, trash.size - trash.len - 3);
8830 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8831 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008832 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008833}
8834
Willy Tarreaueee5b512015-04-03 23:46:31 +02008835
8836/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8837 * The hdr_idx is allocated as well. In case of allocation failure, everything
8838 * allocated is freed and NULL is returned. Otherwise the new transaction is
8839 * assigned to the stream and returned.
8840 */
8841struct http_txn *http_alloc_txn(struct stream *s)
8842{
8843 struct http_txn *txn = s->txn;
8844
8845 if (txn)
8846 return txn;
8847
8848 txn = pool_alloc2(pool2_http_txn);
8849 if (!txn)
8850 return txn;
8851
8852 txn->hdr_idx.size = global.tune.max_http_hdr;
8853 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8854 if (!txn->hdr_idx.v) {
8855 pool_free2(pool2_http_txn, txn);
8856 return NULL;
8857 }
8858
8859 s->txn = txn;
8860 return txn;
8861}
8862
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008863void http_txn_reset_req(struct http_txn *txn)
8864{
8865 txn->req.flags = 0;
8866 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8867 txn->req.next = 0;
8868 txn->req.chunk_len = 0LL;
8869 txn->req.body_len = 0LL;
8870 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8871}
8872
8873void http_txn_reset_res(struct http_txn *txn)
8874{
8875 txn->rsp.flags = 0;
8876 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8877 txn->rsp.next = 0;
8878 txn->rsp.chunk_len = 0LL;
8879 txn->rsp.body_len = 0LL;
8880 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8881}
8882
Willy Tarreau0937bc42009-12-22 15:03:09 +01008883/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008884 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008885 * the required fields are properly allocated and that we only need to (re)init
8886 * them. This should be used before processing any new request.
8887 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008888void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008889{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008890 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008891 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008892
8893 txn->flags = 0;
8894 txn->status = -1;
8895
Willy Tarreauf64d1412010-10-07 20:06:11 +02008896 txn->cookie_first_date = 0;
8897 txn->cookie_last_date = 0;
8898
Willy Tarreaueee5b512015-04-03 23:46:31 +02008899 txn->srv_cookie = NULL;
8900 txn->cli_cookie = NULL;
8901 txn->uri = NULL;
8902
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008903 http_txn_reset_req(txn);
8904 http_txn_reset_res(txn);
8905
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008906 txn->req.chn = &s->req;
8907 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008908
8909 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008910
8911 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8912 if (fe->options2 & PR_O2_REQBUG_OK)
8913 txn->req.err_pos = -1; /* let buggy requests pass */
8914
Willy Tarreau0937bc42009-12-22 15:03:09 +01008915 if (txn->hdr_idx.v)
8916 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008917
8918 vars_init(&s->vars_txn, SCOPE_TXN);
8919 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008920}
8921
8922/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008923void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008924{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008925 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008926 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008927
Willy Tarreau75195602014-03-11 15:48:55 +01008928 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008929 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008930 s->comp_algo->end(&s->comp_ctx);
8931 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008932 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008933
Willy Tarreau0937bc42009-12-22 15:03:09 +01008934 /* these ones will have been dynamically allocated */
8935 pool_free2(pool2_requri, txn->uri);
8936 pool_free2(pool2_capture, txn->cli_cookie);
8937 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008938 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008939
William Lallemanda73203e2012-03-12 12:48:57 +01008940 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008941 txn->uri = NULL;
8942 txn->srv_cookie = NULL;
8943 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008944
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008945 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008946 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008947 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008948 pool_free2(h->pool, s->req_cap[h->index]);
8949 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008950 }
8951
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008952 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008953 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008954 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008955 pool_free2(h->pool, s->res_cap[h->index]);
8956 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008957 }
8958
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008959 vars_prune(&s->vars_txn, s);
8960 vars_prune(&s->vars_reqres, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008961}
8962
8963/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008964void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008965{
8966 http_end_txn(s);
8967 http_init_txn(s);
8968
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008969 /* reinitialise the current rule list pointer to NULL. We are sure that
8970 * any rulelist match the NULL pointer.
8971 */
8972 s->current_rule_list = NULL;
8973
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008974 s->be = strm_fe(s);
8975 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008976 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008977 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008978 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008979 /* re-init store persistence */
8980 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008981 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008982
Willy Tarreau0937bc42009-12-22 15:03:09 +01008983 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008984
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008985 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008986
Willy Tarreau739cfba2010-01-25 23:11:14 +01008987 /* We must trim any excess data from the response buffer, because we
8988 * may have blocked an invalid response from a server that we don't
8989 * want to accidentely forward once we disable the analysers, nor do
8990 * we want those data to come along with next response. A typical
8991 * example of such data would be from a buggy server responding to
8992 * a HEAD with some data, or sending more than the advertised
8993 * content-length.
8994 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008995 if (unlikely(s->res.buf->i))
8996 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008997
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008998 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008999 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009000
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009001 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009002 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009003
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009004 s->req.rex = TICK_ETERNITY;
9005 s->req.wex = TICK_ETERNITY;
9006 s->req.analyse_exp = TICK_ETERNITY;
9007 s->res.rex = TICK_ETERNITY;
9008 s->res.wex = TICK_ETERNITY;
9009 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009010}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009011
Sasha Pachev218f0642014-06-16 12:05:59 -06009012void free_http_res_rules(struct list *r)
9013{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009014 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06009015
9016 list_for_each_entry_safe(pr, tr, r, list) {
9017 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009018 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009019 free(pr);
9020 }
9021}
9022
9023void free_http_req_rules(struct list *r)
9024{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009025 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009026
9027 list_for_each_entry_safe(pr, tr, r, list) {
9028 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009029 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009030 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009031
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009032 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009033 free(pr);
9034 }
9035}
9036
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009037/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009038struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009039{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009040 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009041 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009042 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009043 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009044
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009045 rule = (struct act_rule*)calloc(1, sizeof(struct act_rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009046 if (!rule) {
9047 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009048 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009049 }
9050
CJ Ess108b1dd2015-04-07 12:03:37 -04009051 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009052 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009053 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009054 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009055 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009056 int code;
9057 int hc;
9058
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009059 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01009060 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009061 if (strcmp(args[cur_arg], "deny_status") == 0) {
9062 cur_arg++;
9063 if (!args[cur_arg]) {
9064 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9065 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9066 goto out_err;
9067 }
9068
9069 code = atol(args[cur_arg]);
9070 cur_arg++;
9071 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9072 if (http_err_codes[hc] == code) {
9073 rule->deny_status = hc;
9074 break;
9075 }
9076 }
9077
9078 if (hc >= HTTP_ERR_SIZE) {
9079 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9080 file, linenum, code);
9081 }
9082 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009083 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009084 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009085 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009086 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009087 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009088 cur_arg = 1;
9089
9090 while(*args[cur_arg]) {
9091 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009092 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009093 cur_arg+=2;
9094 continue;
9095 } else
9096 break;
9097 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009098 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009099 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009100 cur_arg = 1;
9101
9102 if (!*args[cur_arg] ||
9103 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9104 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9105 file, linenum, args[0]);
9106 goto out_err;
9107 }
9108 rule->arg.nice = atoi(args[cur_arg]);
9109 if (rule->arg.nice < -1024)
9110 rule->arg.nice = -1024;
9111 else if (rule->arg.nice > 1024)
9112 rule->arg.nice = 1024;
9113 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009114 } else if (!strcmp(args[0], "set-tos")) {
9115#ifdef IP_TOS
9116 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009117 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009118 cur_arg = 1;
9119
9120 if (!*args[cur_arg] ||
9121 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9122 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9123 file, linenum, args[0]);
9124 goto out_err;
9125 }
9126
9127 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9128 if (err && *err != '\0') {
9129 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9130 file, linenum, err, args[0]);
9131 goto out_err;
9132 }
9133 cur_arg++;
9134#else
9135 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9136 goto out_err;
9137#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009138 } else if (!strcmp(args[0], "set-mark")) {
9139#ifdef SO_MARK
9140 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009141 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009142 cur_arg = 1;
9143
9144 if (!*args[cur_arg] ||
9145 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9146 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9147 file, linenum, args[0]);
9148 goto out_err;
9149 }
9150
9151 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9152 if (err && *err != '\0') {
9153 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9154 file, linenum, err, args[0]);
9155 goto out_err;
9156 }
9157 cur_arg++;
9158 global.last_checks |= LSTCHK_NETADM;
9159#else
9160 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9161 goto out_err;
9162#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009163 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009164 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009165 cur_arg = 1;
9166
9167 if (!*args[cur_arg] ||
9168 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9169 bad_log_level:
9170 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9171 file, linenum, args[0]);
9172 goto out_err;
9173 }
9174 if (strcmp(args[cur_arg], "silent") == 0)
9175 rule->arg.loglevel = -1;
9176 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9177 goto bad_log_level;
9178 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009179 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009180 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009181 cur_arg = 1;
9182
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009183 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9184 (*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 +01009185 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9186 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009187 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009188 }
9189
9190 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9191 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9192 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009193
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009194 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009195 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009196 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9197 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009198 free(proxy->conf.lfs_file);
9199 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9200 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009201 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009202 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009203 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009204 cur_arg = 1;
9205
9206 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009207 (*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 -06009208 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9209 file, linenum, args[0]);
9210 goto out_err;
9211 }
9212
9213 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9214 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9215 LIST_INIT(&rule->arg.hdr_add.fmt);
9216
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009217 error = NULL;
9218 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9219 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9220 args[cur_arg + 1], error);
9221 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009222 goto out_err;
9223 }
9224
9225 proxy->conf.args.ctx = ARGC_HRQ;
9226 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9227 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9228 file, linenum);
9229
9230 free(proxy->conf.lfs_file);
9231 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9232 proxy->conf.lfs_line = proxy->conf.args.line;
9233 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009234 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009235 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009236 cur_arg = 1;
9237
9238 if (!*args[cur_arg] ||
9239 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9240 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9241 file, linenum, args[0]);
9242 goto out_err;
9243 }
9244
9245 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9246 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9247
9248 proxy->conf.args.ctx = ARGC_HRQ;
9249 free(proxy->conf.lfs_file);
9250 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9251 proxy->conf.lfs_line = proxy->conf.args.line;
9252 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009253 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9254 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009255 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009256 struct sample_expr *expr;
9257 unsigned int where;
9258 char *err = NULL;
9259
9260 cur_arg = 1;
9261 proxy->conf.args.ctx = ARGC_TRK;
9262
9263 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9264 if (!expr) {
9265 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9266 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9267 free(err);
9268 goto out_err;
9269 }
9270
9271 where = 0;
9272 if (proxy->cap & PR_CAP_FE)
9273 where |= SMP_VAL_FE_HRQ_HDR;
9274 if (proxy->cap & PR_CAP_BE)
9275 where |= SMP_VAL_BE_HRQ_HDR;
9276
9277 if (!(expr->fetch->val & where)) {
9278 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9279 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9280 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9281 args[cur_arg-1], sample_src_names(expr->fetch->use));
9282 free(expr);
9283 goto out_err;
9284 }
9285
9286 if (strcmp(args[cur_arg], "table") == 0) {
9287 cur_arg++;
9288 if (!args[cur_arg]) {
9289 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9290 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9291 free(expr);
9292 goto out_err;
9293 }
9294 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009295 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009296 cur_arg++;
9297 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009298 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009299 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009300 } else if (strcmp(args[0], "redirect") == 0) {
9301 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009302 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009303
Willy Tarreaube4653b2015-05-28 15:26:58 +02009304 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009305 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9306 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9307 goto out_err;
9308 }
9309
9310 /* this redirect rule might already contain a parsed condition which
9311 * we'll pass to the http-request rule.
9312 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009313 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009314 rule->arg.redir = redir;
9315 rule->cond = redir->cond;
9316 redir->cond = NULL;
9317 cur_arg = 2;
9318 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009319 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9320 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009321 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009322 /*
9323 * '+ 8' for 'add-acl('
9324 * '- 9' for 'add-acl(' + trailing ')'
9325 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009326 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009327
9328 cur_arg = 1;
9329
9330 if (!*args[cur_arg] ||
9331 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9332 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9333 file, linenum, args[0]);
9334 goto out_err;
9335 }
9336
9337 LIST_INIT(&rule->arg.map.key);
9338 proxy->conf.args.ctx = ARGC_HRQ;
9339 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9340 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9341 file, linenum);
9342 free(proxy->conf.lfs_file);
9343 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9344 proxy->conf.lfs_line = proxy->conf.args.line;
9345 cur_arg += 1;
9346 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9347 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009348 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009349 /*
9350 * '+ 8' for 'del-acl('
9351 * '- 9' for 'del-acl(' + trailing ')'
9352 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009353 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009354
9355 cur_arg = 1;
9356
9357 if (!*args[cur_arg] ||
9358 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9359 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9360 file, linenum, args[0]);
9361 goto out_err;
9362 }
9363
9364 LIST_INIT(&rule->arg.map.key);
9365 proxy->conf.args.ctx = ARGC_HRQ;
9366 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9367 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9368 file, linenum);
9369 free(proxy->conf.lfs_file);
9370 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9371 proxy->conf.lfs_line = proxy->conf.args.line;
9372 cur_arg += 1;
9373 } else if (strncmp(args[0], "del-map", 7) == 0) {
9374 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009375 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009376 /*
9377 * '+ 8' for 'del-map('
9378 * '- 9' for 'del-map(' + trailing ')'
9379 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009380 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009381
9382 cur_arg = 1;
9383
9384 if (!*args[cur_arg] ||
9385 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9386 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9387 file, linenum, args[0]);
9388 goto out_err;
9389 }
9390
9391 LIST_INIT(&rule->arg.map.key);
9392 proxy->conf.args.ctx = ARGC_HRQ;
9393 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9394 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9395 file, linenum);
9396 free(proxy->conf.lfs_file);
9397 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9398 proxy->conf.lfs_line = proxy->conf.args.line;
9399 cur_arg += 1;
9400 } else if (strncmp(args[0], "set-map", 7) == 0) {
9401 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009402 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009403 /*
9404 * '+ 8' for 'set-map('
9405 * '- 9' for 'set-map(' + trailing ')'
9406 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009407 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009408
9409 cur_arg = 1;
9410
9411 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9412 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9413 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9414 file, linenum, args[0]);
9415 goto out_err;
9416 }
9417
9418 LIST_INIT(&rule->arg.map.key);
9419 LIST_INIT(&rule->arg.map.value);
9420 proxy->conf.args.ctx = ARGC_HRQ;
9421
9422 /* key pattern */
9423 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9424 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9425 file, linenum);
9426
9427 /* value pattern */
9428 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9429 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9430 file, linenum);
9431 free(proxy->conf.lfs_file);
9432 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9433 proxy->conf.lfs_line = proxy->conf.args.line;
9434
9435 cur_arg += 2;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009436 } else if (strncmp(args[0], "set-src", 7) == 0) {
9437 struct sample_expr *expr;
9438 unsigned int where;
9439 char *err = NULL;
9440
9441 cur_arg = 1;
9442 proxy->conf.args.ctx = ARGC_HRQ;
9443
9444 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9445 if (!expr) {
9446 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9447 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9448 free(err);
9449 goto out_err;
9450 }
9451
9452 where = 0;
9453 if (proxy->cap & PR_CAP_FE)
9454 where |= SMP_VAL_FE_HRQ_HDR;
9455 if (proxy->cap & PR_CAP_BE)
9456 where |= SMP_VAL_BE_HRQ_HDR;
9457
9458 if (!(expr->fetch->val & where)) {
9459 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9460 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9461 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9462 args[cur_arg-1], sample_src_names(expr->fetch->use));
9463 free(expr);
9464 goto out_err;
9465 }
9466
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02009467 rule->arg.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009468 rule->action = ACT_HTTP_REQ_SET_SRC;
William Lallemand73025dd2014-04-24 14:38:37 +02009469 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9470 char *errmsg = NULL;
9471 cur_arg = 1;
9472 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009473 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009474 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009475 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009476 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9477 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9478 free(errmsg);
9479 goto out_err;
9480 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009481 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009482 action_build_list(&http_req_keywords.list, &trash);
9483 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9484 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9485 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', "
9486 "'set-src'%s%s, but got '%s'%s.\n",
9487 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009488 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009489 }
9490
9491 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9492 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009493 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009494
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009495 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9496 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9497 file, linenum, args[0], errmsg);
9498 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009499 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009500 }
9501 rule->cond = cond;
9502 }
9503 else if (*args[cur_arg]) {
9504 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9505 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9506 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009507 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009508 }
9509
9510 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009511 out_err:
9512 free(rule);
9513 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009514}
9515
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009516/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009517struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009518{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009519 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009520 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009521 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009522 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009523
9524 rule = calloc(1, sizeof(*rule));
9525 if (!rule) {
9526 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9527 goto out_err;
9528 }
9529
9530 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009531 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009532 cur_arg = 1;
9533 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009534 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009535 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009536 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009537 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009538 cur_arg = 1;
9539
9540 if (!*args[cur_arg] ||
9541 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9542 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9543 file, linenum, args[0]);
9544 goto out_err;
9545 }
9546 rule->arg.nice = atoi(args[cur_arg]);
9547 if (rule->arg.nice < -1024)
9548 rule->arg.nice = -1024;
9549 else if (rule->arg.nice > 1024)
9550 rule->arg.nice = 1024;
9551 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009552 } else if (!strcmp(args[0], "set-tos")) {
9553#ifdef IP_TOS
9554 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009555 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009556 cur_arg = 1;
9557
9558 if (!*args[cur_arg] ||
9559 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9560 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9561 file, linenum, args[0]);
9562 goto out_err;
9563 }
9564
9565 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9566 if (err && *err != '\0') {
9567 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9568 file, linenum, err, args[0]);
9569 goto out_err;
9570 }
9571 cur_arg++;
9572#else
9573 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9574 goto out_err;
9575#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009576 } else if (!strcmp(args[0], "set-mark")) {
9577#ifdef SO_MARK
9578 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009579 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009580 cur_arg = 1;
9581
9582 if (!*args[cur_arg] ||
9583 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9584 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9585 file, linenum, args[0]);
9586 goto out_err;
9587 }
9588
9589 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9590 if (err && *err != '\0') {
9591 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9592 file, linenum, err, args[0]);
9593 goto out_err;
9594 }
9595 cur_arg++;
9596 global.last_checks |= LSTCHK_NETADM;
9597#else
9598 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9599 goto out_err;
9600#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009601 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009602 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009603 cur_arg = 1;
9604
9605 if (!*args[cur_arg] ||
9606 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9607 bad_log_level:
9608 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9609 file, linenum, args[0]);
9610 goto out_err;
9611 }
9612 if (strcmp(args[cur_arg], "silent") == 0)
9613 rule->arg.loglevel = -1;
9614 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9615 goto bad_log_level;
9616 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009617 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009618 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009619 cur_arg = 1;
9620
9621 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9622 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9623 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9624 file, linenum, args[0]);
9625 goto out_err;
9626 }
9627
9628 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9629 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9630 LIST_INIT(&rule->arg.hdr_add.fmt);
9631
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009632 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009633 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009634 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9635 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009636 free(proxy->conf.lfs_file);
9637 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9638 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009639 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009640 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009641 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009642 cur_arg = 1;
9643
9644 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009645 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9646 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009647 file, linenum, args[0]);
9648 goto out_err;
9649 }
9650
9651 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9652 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9653 LIST_INIT(&rule->arg.hdr_add.fmt);
9654
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009655 error = NULL;
9656 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9657 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9658 args[cur_arg + 1], error);
9659 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009660 goto out_err;
9661 }
9662
9663 proxy->conf.args.ctx = ARGC_HRQ;
9664 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9665 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9666 file, linenum);
9667
9668 free(proxy->conf.lfs_file);
9669 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9670 proxy->conf.lfs_line = proxy->conf.args.line;
9671 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009672 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009673 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009674 cur_arg = 1;
9675
9676 if (!*args[cur_arg] ||
9677 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9678 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9679 file, linenum, args[0]);
9680 goto out_err;
9681 }
9682
9683 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9684 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9685
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009686 proxy->conf.args.ctx = ARGC_HRS;
9687 free(proxy->conf.lfs_file);
9688 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9689 proxy->conf.lfs_line = proxy->conf.args.line;
9690 cur_arg += 1;
9691 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9692 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009693 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009694 /*
9695 * '+ 8' for 'add-acl('
9696 * '- 9' for 'add-acl(' + trailing ')'
9697 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009698 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009699
9700 cur_arg = 1;
9701
9702 if (!*args[cur_arg] ||
9703 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9704 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9705 file, linenum, args[0]);
9706 goto out_err;
9707 }
9708
9709 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009710 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009711 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9712 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9713 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009714 free(proxy->conf.lfs_file);
9715 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9716 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009717
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009718 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009719 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9720 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009721 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009722 /*
9723 * '+ 8' for 'del-acl('
9724 * '- 9' for 'del-acl(' + trailing ')'
9725 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009726 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009727
9728 cur_arg = 1;
9729
9730 if (!*args[cur_arg] ||
9731 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9732 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9733 file, linenum, args[0]);
9734 goto out_err;
9735 }
9736
9737 LIST_INIT(&rule->arg.map.key);
9738 proxy->conf.args.ctx = ARGC_HRS;
9739 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9740 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9741 file, linenum);
9742 free(proxy->conf.lfs_file);
9743 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9744 proxy->conf.lfs_line = proxy->conf.args.line;
9745 cur_arg += 1;
9746 } else if (strncmp(args[0], "del-map", 7) == 0) {
9747 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009748 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009749 /*
9750 * '+ 8' for 'del-map('
9751 * '- 9' for 'del-map(' + trailing ')'
9752 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009753 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009754
9755 cur_arg = 1;
9756
9757 if (!*args[cur_arg] ||
9758 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9759 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9760 file, linenum, args[0]);
9761 goto out_err;
9762 }
9763
9764 LIST_INIT(&rule->arg.map.key);
9765 proxy->conf.args.ctx = ARGC_HRS;
9766 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9767 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9768 file, linenum);
9769 free(proxy->conf.lfs_file);
9770 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9771 proxy->conf.lfs_line = proxy->conf.args.line;
9772 cur_arg += 1;
9773 } else if (strncmp(args[0], "set-map", 7) == 0) {
9774 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009775 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009776 /*
9777 * '+ 8' for 'set-map('
9778 * '- 9' for 'set-map(' + trailing ')'
9779 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009780 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009781
9782 cur_arg = 1;
9783
9784 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9785 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9786 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9787 file, linenum, args[0]);
9788 goto out_err;
9789 }
9790
9791 LIST_INIT(&rule->arg.map.key);
9792 LIST_INIT(&rule->arg.map.value);
9793
9794 proxy->conf.args.ctx = ARGC_HRS;
9795
9796 /* key pattern */
9797 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9798 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9799 file, linenum);
9800
9801 /* value pattern */
9802 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9803 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9804 file, linenum);
9805
9806 free(proxy->conf.lfs_file);
9807 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9808 proxy->conf.lfs_line = proxy->conf.args.line;
9809
9810 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009811 } else if (strcmp(args[0], "redirect") == 0) {
9812 struct redirect_rule *redir;
9813 char *errmsg = NULL;
9814
9815 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9816 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9817 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9818 goto out_err;
9819 }
9820
9821 /* this redirect rule might already contain a parsed condition which
9822 * we'll pass to the http-request rule.
9823 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009824 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009825 rule->arg.redir = redir;
9826 rule->cond = redir->cond;
9827 redir->cond = NULL;
9828 cur_arg = 2;
9829 return rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009830 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9831 char *errmsg = NULL;
9832 cur_arg = 1;
9833 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009834 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009835 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009836 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009837 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9838 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9839 free(errmsg);
9840 goto out_err;
9841 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009842 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009843 action_build_list(&http_res_keywords.list, &trash);
9844 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9845 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9846 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', "
9847 "'set-src'%s%s, but got '%s'%s.\n",
9848 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009849 goto out_err;
9850 }
9851
9852 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9853 struct acl_cond *cond;
9854 char *errmsg = NULL;
9855
9856 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9857 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9858 file, linenum, args[0], errmsg);
9859 free(errmsg);
9860 goto out_err;
9861 }
9862 rule->cond = cond;
9863 }
9864 else if (*args[cur_arg]) {
9865 Alert("parsing [%s:%d]: 'http-response %s' expects"
9866 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9867 file, linenum, args[0], args[cur_arg]);
9868 goto out_err;
9869 }
9870
9871 return rule;
9872 out_err:
9873 free(rule);
9874 return NULL;
9875}
9876
Willy Tarreau4baae242012-12-27 12:00:31 +01009877/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009878 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009879 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9880 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009881 */
9882struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009883 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009884{
9885 struct redirect_rule *rule;
9886 int cur_arg;
9887 int type = REDIRECT_TYPE_NONE;
9888 int code = 302;
9889 const char *destination = NULL;
9890 const char *cookie = NULL;
9891 int cookie_set = 0;
9892 unsigned int flags = REDIRECT_FLAG_NONE;
9893 struct acl_cond *cond = NULL;
9894
9895 cur_arg = 0;
9896 while (*(args[cur_arg])) {
9897 if (strcmp(args[cur_arg], "location") == 0) {
9898 if (!*args[cur_arg + 1])
9899 goto missing_arg;
9900
9901 type = REDIRECT_TYPE_LOCATION;
9902 cur_arg++;
9903 destination = args[cur_arg];
9904 }
9905 else if (strcmp(args[cur_arg], "prefix") == 0) {
9906 if (!*args[cur_arg + 1])
9907 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009908 type = REDIRECT_TYPE_PREFIX;
9909 cur_arg++;
9910 destination = args[cur_arg];
9911 }
9912 else if (strcmp(args[cur_arg], "scheme") == 0) {
9913 if (!*args[cur_arg + 1])
9914 goto missing_arg;
9915
9916 type = REDIRECT_TYPE_SCHEME;
9917 cur_arg++;
9918 destination = args[cur_arg];
9919 }
9920 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9921 if (!*args[cur_arg + 1])
9922 goto missing_arg;
9923
9924 cur_arg++;
9925 cookie = args[cur_arg];
9926 cookie_set = 1;
9927 }
9928 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9929 if (!*args[cur_arg + 1])
9930 goto missing_arg;
9931
9932 cur_arg++;
9933 cookie = args[cur_arg];
9934 cookie_set = 0;
9935 }
9936 else if (strcmp(args[cur_arg], "code") == 0) {
9937 if (!*args[cur_arg + 1])
9938 goto missing_arg;
9939
9940 cur_arg++;
9941 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009942 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009943 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009944 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009945 args[cur_arg - 1], args[cur_arg]);
9946 return NULL;
9947 }
9948 }
9949 else if (!strcmp(args[cur_arg],"drop-query")) {
9950 flags |= REDIRECT_FLAG_DROP_QS;
9951 }
9952 else if (!strcmp(args[cur_arg],"append-slash")) {
9953 flags |= REDIRECT_FLAG_APPEND_SLASH;
9954 }
9955 else if (strcmp(args[cur_arg], "if") == 0 ||
9956 strcmp(args[cur_arg], "unless") == 0) {
9957 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9958 if (!cond) {
9959 memprintf(errmsg, "error in condition: %s", *errmsg);
9960 return NULL;
9961 }
9962 break;
9963 }
9964 else {
9965 memprintf(errmsg,
9966 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9967 args[cur_arg]);
9968 return NULL;
9969 }
9970 cur_arg++;
9971 }
9972
9973 if (type == REDIRECT_TYPE_NONE) {
9974 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9975 return NULL;
9976 }
9977
Willy Tarreaube4653b2015-05-28 15:26:58 +02009978 if (dir && type != REDIRECT_TYPE_LOCATION) {
9979 memprintf(errmsg, "response only supports redirect type 'location'");
9980 return NULL;
9981 }
9982
Willy Tarreau4baae242012-12-27 12:00:31 +01009983 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9984 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009985 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009986
9987 if (!use_fmt) {
9988 /* old-style static redirect rule */
9989 rule->rdr_str = strdup(destination);
9990 rule->rdr_len = strlen(destination);
9991 }
9992 else {
9993 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009994
9995 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9996 * if prefix == "/", we don't want to add anything, otherwise it
9997 * makes it hard for the user to configure a self-redirection.
9998 */
Godbachd9722032014-12-18 15:44:58 +08009999 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010000 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +010010001 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +020010002 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
10003 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +010010004 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010005 free(curproxy->conf.lfs_file);
10006 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10007 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010008 }
10009 }
10010
Willy Tarreau4baae242012-12-27 12:00:31 +010010011 if (cookie) {
10012 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10013 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10014 */
10015 rule->cookie_len = strlen(cookie);
10016 if (cookie_set) {
10017 rule->cookie_str = malloc(rule->cookie_len + 10);
10018 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10019 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10020 rule->cookie_len += 9;
10021 } else {
10022 rule->cookie_str = malloc(rule->cookie_len + 21);
10023 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10024 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10025 rule->cookie_len += 20;
10026 }
10027 }
10028 rule->type = type;
10029 rule->code = code;
10030 rule->flags = flags;
10031 LIST_INIT(&rule->list);
10032 return rule;
10033
10034 missing_arg:
10035 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10036 return NULL;
10037}
10038
Willy Tarreau8797c062007-05-07 00:55:35 +020010039/************************************************************************/
10040/* The code below is dedicated to ACL parsing and matching */
10041/************************************************************************/
10042
10043
Willy Tarreau14174bc2012-04-16 14:34:04 +020010044/* This function ensures that the prerequisites for an L7 fetch are ready,
10045 * which means that a request or response is ready. If some data is missing,
10046 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010047 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10048 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010049 *
10050 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010051 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10052 * decide whether or not an HTTP message is present ;
10053 * 0 if the requested data cannot be fetched or if it is certain that
10054 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010055 * 1 if an HTTP message is ready
10056 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010057int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010058 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010059{
Willy Tarreau192252e2015-04-04 01:47:55 +020010060 struct http_txn *txn;
10061 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010062
Willy Tarreaueee5b512015-04-03 23:46:31 +020010063 /* Note: this function may only be used from places where
10064 * http_init_txn() has already been done, and implies that <s>,
10065 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
10066 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010067 */
10068
Willy Tarreau192252e2015-04-04 01:47:55 +020010069 if (!s)
10070 return 0;
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010071 if (!s->txn) {
10072 if (unlikely(!http_alloc_txn(s)))
10073 return 0; /* not enough memory */
10074 http_init_txn(s);
10075 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010076 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010077 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010078
10079 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010080 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010081
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010082 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010083 /* If the buffer does not leave enough free space at the end,
10084 * we must first realign it.
10085 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010086 if (s->req.buf->p > s->req.buf->data &&
10087 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10088 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010089
Willy Tarreau14174bc2012-04-16 14:34:04 +020010090 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010091 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010092 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010093
10094 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010095 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010096 http_msg_analyzer(msg, &txn->hdr_idx);
10097
10098 /* Still no valid request ? */
10099 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010100 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010101 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010102 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010103 }
10104 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010105 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010106 return 0;
10107 }
10108
10109 /* OK we just got a valid HTTP request. We have some minor
10110 * preparation to perform so that further checks can rely
10111 * on HTTP tests.
10112 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010113
10114 /* If the request was parsed but was too large, we must absolutely
10115 * return an error so that it is not processed. At the moment this
10116 * cannot happen, but if the parsers are to change in the future,
10117 * we want this check to be maintained.
10118 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010119 if (unlikely(s->req.buf->i + s->req.buf->p >
10120 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010121 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010122 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010123 return 1;
10124 }
10125
Willy Tarreau9b28e032012-10-12 23:49:43 +020010126 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010127 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010128 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010129
Willy Tarreau506d0502013-07-06 13:29:24 +020010130 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10131 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010132 }
10133
Willy Tarreau506d0502013-07-06 13:29:24 +020010134 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010135 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010136 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010137
10138 /* otherwise everything's ready for the request */
10139 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010140 else {
10141 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010142 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10143 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010144 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010145 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010146 }
10147
10148 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010149 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010150 return 1;
10151}
Willy Tarreau8797c062007-05-07 00:55:35 +020010152
Willy Tarreau8797c062007-05-07 00:55:35 +020010153/* 1. Check on METHOD
10154 * We use the pre-parsed method if it is known, and store its number as an
10155 * integer. If it is unknown, we use the pointer and the length.
10156 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010157static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010158{
10159 int len, meth;
10160
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010161 len = strlen(text);
10162 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010163
10164 pattern->val.i = meth;
10165 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010166 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010167 pattern->len = len;
10168 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010169 else {
10170 pattern->ptr.str = NULL;
10171 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010172 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010173 return 1;
10174}
10175
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010176/* This function fetches the method of current HTTP request and stores
10177 * it in the global pattern struct as a chunk. There are two possibilities :
10178 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10179 * in <len> and <ptr> is NULL ;
10180 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10181 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010182 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010183 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010184static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010185smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010186{
10187 int meth;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010188 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010189
Willy Tarreau24e32d82012-04-23 23:55:44 +020010190 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010191
Willy Tarreau8797c062007-05-07 00:55:35 +020010192 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010193 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010194 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010195 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010196 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10197 /* ensure the indexes are not affected */
10198 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010199 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010200 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10201 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010202 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010203 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010204 return 1;
10205}
10206
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010207/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010208static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010209{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010210 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010211 struct pattern_list *lst;
10212 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010213
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010214 list_for_each_entry(lst, &expr->patterns, list) {
10215 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010216
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010217 /* well-known method */
10218 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010219 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010220 return pattern;
10221 else
10222 continue;
10223 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010224
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010225 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010226 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010227 continue;
10228
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010229 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010230 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10231 (!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 +010010232 return pattern;
10233 }
10234 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010235}
10236
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010237static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010238smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010239{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010240 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010241 char *ptr;
10242 int len;
10243
Willy Tarreauc0239e02012-04-16 14:42:55 +020010244 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010245
Willy Tarreau8797c062007-05-07 00:55:35 +020010246 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010247 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010248
10249 while ((len-- > 0) && (*ptr++ != '/'));
10250 if (len <= 0)
10251 return 0;
10252
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010253 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010254 smp->data.u.str.str = ptr;
10255 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010256
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010257 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010258 return 1;
10259}
10260
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010261static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010262smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010263{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010264 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010265 char *ptr;
10266 int len;
10267
Willy Tarreauc0239e02012-04-16 14:42:55 +020010268 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010269
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010270 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010271 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10272 return 0;
10273
Willy Tarreau8797c062007-05-07 00:55:35 +020010274 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010275 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010276
10277 while ((len-- > 0) && (*ptr++ != '/'));
10278 if (len <= 0)
10279 return 0;
10280
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010281 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010282 smp->data.u.str.str = ptr;
10283 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010284
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010285 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010286 return 1;
10287}
10288
10289/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010290static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010291smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010292{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010293 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010294 char *ptr;
10295 int len;
10296
Willy Tarreauc0239e02012-04-16 14:42:55 +020010297 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010298
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010299 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010300 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10301 return 0;
10302
Willy Tarreau8797c062007-05-07 00:55:35 +020010303 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010304 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010305
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010306 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010307 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010308 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010309 return 1;
10310}
10311
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010312/* returns the longest available part of the body. This requires that the body
10313 * has been waited for using http-buffer-request.
10314 */
10315static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010316smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010317{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010318 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010319 struct http_msg *msg;
10320 unsigned long len;
10321 unsigned long block1;
10322 char *body;
10323 struct chunk *temp;
10324
10325 CHECK_HTTP_MESSAGE_FIRST();
10326
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010327 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010328 msg = &txn->req;
10329 else
10330 msg = &txn->rsp;
10331
10332 len = http_body_bytes(msg);
10333 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10334
10335 block1 = len;
10336 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10337 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10338
10339 if (block1 == len) {
10340 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010341 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010342 smp->data.u.str.str = body;
10343 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010344 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10345 }
10346 else {
10347 /* buffer is wrapped, we need to defragment it */
10348 temp = get_trash_chunk();
10349 memcpy(temp->str, body, block1);
10350 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010351 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010352 smp->data.u.str.str = temp->str;
10353 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010354 smp->flags = SMP_F_VOL_TEST;
10355 }
10356 return 1;
10357}
10358
10359
10360/* returns the available length of the body. This requires that the body
10361 * has been waited for using http-buffer-request.
10362 */
10363static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010364smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010365{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010366 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010367 struct http_msg *msg;
10368
10369 CHECK_HTTP_MESSAGE_FIRST();
10370
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010371 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010372 msg = &txn->req;
10373 else
10374 msg = &txn->rsp;
10375
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010376 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010377 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010378
10379 smp->flags = SMP_F_VOL_TEST;
10380 return 1;
10381}
10382
10383
10384/* returns the advertised length of the body, or the advertised size of the
10385 * chunks available in the buffer. This requires that the body has been waited
10386 * for using http-buffer-request.
10387 */
10388static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010389smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010390{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010391 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010392 struct http_msg *msg;
10393
10394 CHECK_HTTP_MESSAGE_FIRST();
10395
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010396 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010397 msg = &txn->req;
10398 else
10399 msg = &txn->rsp;
10400
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010401 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010402 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010403
10404 smp->flags = SMP_F_VOL_TEST;
10405 return 1;
10406}
10407
10408
Willy Tarreau8797c062007-05-07 00:55:35 +020010409/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010410static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010411smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010412{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010413 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010414
Willy Tarreauc0239e02012-04-16 14:42:55 +020010415 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010416
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010417 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010418 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010419 smp->data.u.str.len = txn->req.sl.rq.u_l;
10420 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010421 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010422 return 1;
10423}
10424
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010425static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010426smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010427{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010428 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010429 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010430
Willy Tarreauc0239e02012-04-16 14:42:55 +020010431 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010432
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010433 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010434 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010435 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010436 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010437
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010438 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010439 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010440 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010441 return 1;
10442}
10443
10444static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010445smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010446{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010447 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010448 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010449
Willy Tarreauc0239e02012-04-16 14:42:55 +020010450 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010451
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010452 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010453 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 +020010454 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10455 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010456
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010457 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010458 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010459 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010460 return 1;
10461}
10462
Willy Tarreau185b5c42012-04-26 15:11:51 +020010463/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10464 * Accepts an optional argument of type string containing the header field name,
10465 * and an optional argument of type signed or unsigned integer to request an
10466 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010467 * headers are considered from the first one. It does not stop on commas and
10468 * returns full lines instead (useful for User-Agent or Date for example).
10469 */
10470static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010471smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010472{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010473 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010474 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010475 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010476 int occ = 0;
10477 const char *name_str = NULL;
10478 int name_len = 0;
10479
10480 if (!ctx) {
10481 /* first call */
10482 ctx = &static_hdr_ctx;
10483 ctx->idx = 0;
10484 smp->ctx.a[0] = ctx;
10485 }
10486
10487 if (args) {
10488 if (args[0].type != ARGT_STR)
10489 return 0;
10490 name_str = args[0].data.str.str;
10491 name_len = args[0].data.str.len;
10492
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010493 if (args[1].type == ARGT_SINT)
10494 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010495 }
10496
10497 CHECK_HTTP_MESSAGE_FIRST();
10498
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010499 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010500 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 +020010501
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010502 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10503 /* search for header from the beginning */
10504 ctx->idx = 0;
10505
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010506 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010507 /* no explicit occurrence and single fetch => last header by default */
10508 occ = -1;
10509
10510 if (!occ)
10511 /* prepare to report multiple occurrences for ACL fetches */
10512 smp->flags |= SMP_F_NOT_LAST;
10513
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010514 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010515 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010516 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 +020010517 return 1;
10518
10519 smp->flags &= ~SMP_F_NOT_LAST;
10520 return 0;
10521}
10522
10523/* 6. Check on HTTP header count. The number of occurrences is returned.
10524 * Accepts exactly 1 argument of type string. It does not stop on commas and
10525 * returns full lines instead (useful for User-Agent or Date for example).
10526 */
10527static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010528smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010529{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010530 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010531 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010532 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010533 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010534 const char *name = NULL;
10535 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010536
Willy Tarreau601a4d12015-04-01 19:16:09 +020010537 if (args && args->type == ARGT_STR) {
10538 name = args->data.str.str;
10539 len = args->data.str.len;
10540 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010541
10542 CHECK_HTTP_MESSAGE_FIRST();
10543
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010544 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010545 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 +020010546
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010547 ctx.idx = 0;
10548 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010549 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010550 cnt++;
10551
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010552 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010553 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010554 smp->flags = SMP_F_VOL_HDR;
10555 return 1;
10556}
10557
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010558static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010559smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010560{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010561 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010562 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010563 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010564 struct chunk *temp;
10565 char del = ',';
10566
10567 if (args && args->type == ARGT_STR)
10568 del = *args[0].data.str.str;
10569
10570 CHECK_HTTP_MESSAGE_FIRST();
10571
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010572 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010573 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 +020010574
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010575 temp = get_trash_chunk();
10576
10577 ctx.idx = 0;
10578 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10579 if (temp->len)
10580 temp->str[temp->len++] = del;
10581 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10582 temp->len += ctx.del;
10583 }
10584
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010585 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010586 smp->data.u.str.str = temp->str;
10587 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010588 smp->flags = SMP_F_VOL_HDR;
10589 return 1;
10590}
10591
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010592/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10593 * Accepts an optional argument of type string containing the header field name,
10594 * and an optional argument of type signed or unsigned integer to request an
10595 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010596 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010597 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010598static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010599smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010600{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010601 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010602 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010603 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010604 int occ = 0;
10605 const char *name_str = NULL;
10606 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010607
Willy Tarreaua890d072013-04-02 12:01:06 +020010608 if (!ctx) {
10609 /* first call */
10610 ctx = &static_hdr_ctx;
10611 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010612 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010613 }
10614
Willy Tarreau185b5c42012-04-26 15:11:51 +020010615 if (args) {
10616 if (args[0].type != ARGT_STR)
10617 return 0;
10618 name_str = args[0].data.str.str;
10619 name_len = args[0].data.str.len;
10620
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010621 if (args[1].type == ARGT_SINT)
10622 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010623 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010624
Willy Tarreaue333ec92012-04-16 16:26:40 +020010625 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010626
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010627 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010628 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 +020010629
Willy Tarreau185b5c42012-04-26 15:11:51 +020010630 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010631 /* search for header from the beginning */
10632 ctx->idx = 0;
10633
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010634 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010635 /* no explicit occurrence and single fetch => last header by default */
10636 occ = -1;
10637
10638 if (!occ)
10639 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010640 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010641
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010642 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010643 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010644 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 +020010645 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010646
Willy Tarreau37406352012-04-23 16:16:37 +020010647 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010648 return 0;
10649}
10650
Willy Tarreauc11416f2007-06-17 16:58:38 +020010651/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010652 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010653 */
10654static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010655smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010656{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010657 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010658 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010659 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010660 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010661 const char *name = NULL;
10662 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010663
Willy Tarreau601a4d12015-04-01 19:16:09 +020010664 if (args && args->type == ARGT_STR) {
10665 name = args->data.str.str;
10666 len = args->data.str.len;
10667 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010668
Willy Tarreaue333ec92012-04-16 16:26:40 +020010669 CHECK_HTTP_MESSAGE_FIRST();
10670
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010671 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010672 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 +020010673
Willy Tarreau33a7e692007-06-10 19:45:56 +020010674 ctx.idx = 0;
10675 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010676 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010677 cnt++;
10678
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010679 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010680 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010681 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010682 return 1;
10683}
10684
Willy Tarreau185b5c42012-04-26 15:11:51 +020010685/* Fetch an HTTP header's integer value. The integer value is returned. It
10686 * takes a mandatory argument of type string and an optional one of type int
10687 * to designate a specific occurrence. It returns an unsigned integer, which
10688 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010689 */
10690static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010691smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010692{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010693 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010694
Willy Tarreauf853c462012-04-23 18:53:56 +020010695 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010696 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010697 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010698 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010699
Willy Tarreaud53e2422012-04-16 17:21:11 +020010700 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010701}
10702
Cyril Bonté69fa9922012-10-25 00:01:06 +020010703/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10704 * and an optional one of type int to designate a specific occurrence.
10705 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010706 */
10707static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010708smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010709{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010710 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010711
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010712 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010713 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010714 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010715 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010716 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010717 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010718 if (smp->data.u.str.len < temp->size - 1) {
10719 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10720 temp->str[smp->data.u.str.len] = '\0';
10721 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010722 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010723 break;
10724 }
10725 }
10726 }
10727
Willy Tarreaud53e2422012-04-16 17:21:11 +020010728 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010729 if (!(smp->flags & SMP_F_NOT_LAST))
10730 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010731 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010732 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010733}
10734
Willy Tarreau737b0c12007-06-10 21:28:46 +020010735/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10736 * the first '/' after the possible hostname, and ends before the possible '?'.
10737 */
10738static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010739smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010740{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010741 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010742 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010743
Willy Tarreauc0239e02012-04-16 14:42:55 +020010744 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010745
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010746 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010747 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010748 ptr = http_get_path(txn);
10749 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010750 return 0;
10751
10752 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010753 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010754 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010755
10756 while (ptr < end && *ptr != '?')
10757 ptr++;
10758
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010759 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010760 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010761 return 1;
10762}
10763
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010764/* This produces a concatenation of the first occurrence of the Host header
10765 * followed by the path component if it begins with a slash ('/'). This means
10766 * that '*' will not be added, resulting in exactly the first Host entry.
10767 * If no Host header is found, then the path is returned as-is. The returned
10768 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010769 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010770 */
10771static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010772smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010773{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010774 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010775 char *ptr, *end, *beg;
10776 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010777 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010778
10779 CHECK_HTTP_MESSAGE_FIRST();
10780
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010781 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010782 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010783 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010784 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010785
10786 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010787 temp = get_trash_chunk();
10788 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010789 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010790 smp->data.u.str.str = temp->str;
10791 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010792
10793 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010794 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010795 beg = http_get_path(txn);
10796 if (!beg)
10797 beg = end;
10798
10799 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10800
10801 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010802 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10803 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010804 }
10805
10806 smp->flags = SMP_F_VOL_1ST;
10807 return 1;
10808}
10809
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010810/* This produces a 32-bit hash of the concatenation of the first occurrence of
10811 * the Host header followed by the path component if it begins with a slash ('/').
10812 * This means that '*' will not be added, resulting in exactly the first Host
10813 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010814 * is hashed using the path hash followed by a full avalanche hash and provides a
10815 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010816 * high-traffic sites without having to store whole paths.
10817 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010818int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010819smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010820{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010821 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010822 struct hdr_ctx ctx;
10823 unsigned int hash = 0;
10824 char *ptr, *beg, *end;
10825 int len;
10826
10827 CHECK_HTTP_MESSAGE_FIRST();
10828
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010829 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010830 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010831 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010832 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10833 ptr = ctx.line + ctx.val;
10834 len = ctx.vlen;
10835 while (len--)
10836 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10837 }
10838
10839 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010840 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010841 beg = http_get_path(txn);
10842 if (!beg)
10843 beg = end;
10844
10845 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10846
10847 if (beg < ptr && *beg == '/') {
10848 while (beg < ptr)
10849 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10850 }
10851 hash = full_hash(hash);
10852
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010853 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010854 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010855 smp->flags = SMP_F_VOL_1ST;
10856 return 1;
10857}
10858
Willy Tarreau4a550602012-12-09 14:53:32 +010010859/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010860 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10861 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10862 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010863 * that in environments where IPv6 is insignificant, truncating the output to
10864 * 8 bytes would still work.
10865 */
10866static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010867smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010868{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010869 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010870 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010871
10872 if (!cli_conn)
10873 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010874
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010875 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010876 return 0;
10877
Willy Tarreau47ca5452012-12-23 20:22:19 +010010878 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010879 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010880 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010881
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010882 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010883 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010884 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010885 temp->len += 4;
10886 break;
10887 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010888 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010889 temp->len += 16;
10890 break;
10891 default:
10892 return 0;
10893 }
10894
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010895 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010896 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010897 return 1;
10898}
10899
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010900/* Extracts the query string, which comes after the question mark '?'. If no
10901 * question mark is found, nothing is returned. Otherwise it returns a sample
10902 * of type string carrying the whole query string.
10903 */
10904static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010905smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010906{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010907 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010908 char *ptr, *end;
10909
10910 CHECK_HTTP_MESSAGE_FIRST();
10911
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010912 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010913 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10914 end = ptr + txn->req.sl.rq.u_l;
10915
10916 /* look up the '?' */
10917 do {
10918 if (ptr == end)
10919 return 0;
10920 } while (*ptr++ != '?');
10921
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010922 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010923 smp->data.u.str.str = ptr;
10924 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010925 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10926 return 1;
10927}
10928
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010929static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010930smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010931{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010932 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10933 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10934 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010935
Willy Tarreau24e32d82012-04-23 23:55:44 +020010936 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010937
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010938 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010939 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010940 return 1;
10941}
10942
Willy Tarreau7f18e522010-10-22 20:04:13 +020010943/* return a valid test if the current request is the first one on the connection */
10944static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010945smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010946{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010947 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010948 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010949 return 1;
10950}
10951
Willy Tarreau34db1082012-04-19 17:16:54 +020010952/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010953static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010954smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010955{
10956
Willy Tarreau24e32d82012-04-23 23:55:44 +020010957 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010958 return 0;
10959
Willy Tarreauc0239e02012-04-16 14:42:55 +020010960 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010961
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010962 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010963 return 0;
10964
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010965 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010966 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010967 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010968 return 1;
10969}
Willy Tarreau8797c062007-05-07 00:55:35 +020010970
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010971/* Accepts exactly 1 argument of type userlist */
10972static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010973smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010974{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010975 if (!args || args->type != ARGT_USR)
10976 return 0;
10977
10978 CHECK_HTTP_MESSAGE_FIRST();
10979
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010980 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010981 return 0;
10982
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010983 /* if the user does not belong to the userlist or has a wrong password,
10984 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010985 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010986 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010987 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10988 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010989 return 0;
10990
10991 /* pat_match_auth() will need the user list */
10992 smp->ctx.a[0] = args->data.usr;
10993
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010994 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010995 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010996 smp->data.u.str.str = smp->strm->txn->auth.user;
10997 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010998
10999 return 1;
11000}
11001
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011002/* Try to find the next occurrence of a cookie name in a cookie header value.
11003 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11004 * the cookie value is returned into *value and *value_l, and the function
11005 * returns a pointer to the next pointer to search from if the value was found.
11006 * Otherwise if the cookie was not found, NULL is returned and neither value
11007 * nor value_l are touched. The input <hdr> string should first point to the
11008 * header's value, and the <hdr_end> pointer must point to the first character
11009 * not part of the value. <list> must be non-zero if value may represent a list
11010 * of values (cookie headers). This makes it faster to abort parsing when no
11011 * list is expected.
11012 */
David Carlier4686f792015-09-25 14:10:50 +010011013char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011014extract_cookie_value(char *hdr, const char *hdr_end,
11015 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011016 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011017{
11018 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11019 char *next;
11020
11021 /* we search at least a cookie name followed by an equal, and more
11022 * generally something like this :
11023 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11024 */
11025 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11026 /* Iterate through all cookies on this line */
11027
11028 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
11029 att_beg++;
11030
11031 /* find att_end : this is the first character after the last non
11032 * space before the equal. It may be equal to hdr_end.
11033 */
11034 equal = att_end = att_beg;
11035
11036 while (equal < hdr_end) {
11037 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11038 break;
11039 if (http_is_spht[(unsigned char)*equal++])
11040 continue;
11041 att_end = equal;
11042 }
11043
11044 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11045 * is between <att_beg> and <equal>, both may be identical.
11046 */
11047
11048 /* look for end of cookie if there is an equal sign */
11049 if (equal < hdr_end && *equal == '=') {
11050 /* look for the beginning of the value */
11051 val_beg = equal + 1;
11052 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
11053 val_beg++;
11054
11055 /* find the end of the value, respecting quotes */
11056 next = find_cookie_value_end(val_beg, hdr_end);
11057
11058 /* make val_end point to the first white space or delimitor after the value */
11059 val_end = next;
11060 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
11061 val_end--;
11062 } else {
11063 val_beg = val_end = next = equal;
11064 }
11065
11066 /* We have nothing to do with attributes beginning with '$'. However,
11067 * they will automatically be removed if a header before them is removed,
11068 * since they're supposed to be linked together.
11069 */
11070 if (*att_beg == '$')
11071 continue;
11072
11073 /* Ignore cookies with no equal sign */
11074 if (equal == next)
11075 continue;
11076
11077 /* Now we have the cookie name between att_beg and att_end, and
11078 * its value between val_beg and val_end.
11079 */
11080
11081 if (att_end - att_beg == cookie_name_l &&
11082 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11083 /* let's return this value and indicate where to go on from */
11084 *value = val_beg;
11085 *value_l = val_end - val_beg;
11086 return next + 1;
11087 }
11088
11089 /* Set-Cookie headers only have the name in the first attr=value part */
11090 if (!list)
11091 break;
11092 }
11093
11094 return NULL;
11095}
11096
William Lallemanda43ba4e2014-01-28 18:14:25 +010011097/* Fetch a captured HTTP request header. The index is the position of
11098 * the "capture" option in the configuration file
11099 */
11100static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011101smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011102{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011103 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011104 int idx;
11105
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011106 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011107 return 0;
11108
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011109 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011110
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011111 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 +010011112 return 0;
11113
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011114 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011115 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011116 smp->data.u.str.str = smp->strm->req_cap[idx];
11117 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011118
11119 return 1;
11120}
11121
11122/* Fetch a captured HTTP response header. The index is the position of
11123 * the "capture" option in the configuration file
11124 */
11125static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011126smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011127{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011128 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011129 int idx;
11130
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011131 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011132 return 0;
11133
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011134 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011135
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011136 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 +010011137 return 0;
11138
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011139 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011140 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011141 smp->data.u.str.str = smp->strm->res_cap[idx];
11142 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011143
11144 return 1;
11145}
11146
William Lallemand65ad6e12014-01-31 15:08:02 +010011147/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11148static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011149smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011150{
11151 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011152 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011153 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011154
Willy Tarreau15e91e12015-04-04 00:52:09 +020011155 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011156 return 0;
11157
William Lallemand96a77852014-02-05 00:30:02 +010011158 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011159
William Lallemand96a77852014-02-05 00:30:02 +010011160 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11161 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011162
William Lallemand96a77852014-02-05 00:30:02 +010011163 temp = get_trash_chunk();
11164 temp->str = txn->uri;
11165 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011166 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011167 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011168 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011169
11170 return 1;
11171
11172}
11173
11174/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11175static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011176smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011177{
11178 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011179 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011180 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011181
Willy Tarreau15e91e12015-04-04 00:52:09 +020011182 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011183 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011184
William Lallemand65ad6e12014-01-31 15:08:02 +010011185 ptr = txn->uri;
11186
11187 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11188 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011189
William Lallemand65ad6e12014-01-31 15:08:02 +010011190 if (!*ptr)
11191 return 0;
11192
11193 ptr++; /* skip the space */
11194
11195 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011196 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011197 if (!ptr)
11198 return 0;
11199 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11200 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011201
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011202 smp->data.u.str = *temp;
11203 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011204 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011205 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011206
11207 return 1;
11208}
11209
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011210/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11211 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11212 */
11213static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011214smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011215{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011216 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011217
Willy Tarreau15e91e12015-04-04 00:52:09 +020011218 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011219 return 0;
11220
11221 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011222 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011223 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011224 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011225
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011226 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011227 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011228 smp->flags = SMP_F_CONST;
11229 return 1;
11230
11231}
11232
11233/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11234 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11235 */
11236static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011237smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011238{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011239 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011240
Willy Tarreau15e91e12015-04-04 00:52:09 +020011241 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011242 return 0;
11243
11244 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011245 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011246 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011247 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011248
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011249 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011250 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011251 smp->flags = SMP_F_CONST;
11252 return 1;
11253
11254}
11255
William Lallemand65ad6e12014-01-31 15:08:02 +010011256
Willy Tarreaue333ec92012-04-16 16:26:40 +020011257/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011258 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011259 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011260 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011261 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011262 * Accepts exactly 1 argument of type string. If the input options indicate
11263 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011264 * The returned sample is of type CSTR. Can be used to parse cookies in other
11265 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011266 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011267int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011268{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011269 struct http_txn *txn;
11270 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011271 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011272 const struct http_msg *msg;
11273 const char *hdr_name;
11274 int hdr_name_len;
11275 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011276 int occ = 0;
11277 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011278
Willy Tarreau24e32d82012-04-23 23:55:44 +020011279 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011280 return 0;
11281
Willy Tarreaua890d072013-04-02 12:01:06 +020011282 if (!ctx) {
11283 /* first call */
11284 ctx = &static_hdr_ctx;
11285 ctx->idx = 0;
11286 smp->ctx.a[2] = ctx;
11287 }
11288
Willy Tarreaue333ec92012-04-16 16:26:40 +020011289 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011290
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011291 txn = smp->strm->txn;
11292 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011293
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011294 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011295 msg = &txn->req;
11296 hdr_name = "Cookie";
11297 hdr_name_len = 6;
11298 } else {
11299 msg = &txn->rsp;
11300 hdr_name = "Set-Cookie";
11301 hdr_name_len = 10;
11302 }
11303
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011304 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011305 /* no explicit occurrence and single fetch => last cookie by default */
11306 occ = -1;
11307
11308 /* OK so basically here, either we want only one value and it's the
11309 * last one, or we want to iterate over all of them and we fetch the
11310 * next one.
11311 */
11312
Willy Tarreau9b28e032012-10-12 23:49:43 +020011313 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011314 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011315 /* search for the header from the beginning, we must first initialize
11316 * the search parameters.
11317 */
Willy Tarreau37406352012-04-23 16:16:37 +020011318 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011319 ctx->idx = 0;
11320 }
11321
Willy Tarreau28376d62012-04-26 21:26:10 +020011322 smp->flags |= SMP_F_VOL_HDR;
11323
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011324 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011325 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11326 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011327 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11328 goto out;
11329
Willy Tarreau24e32d82012-04-23 23:55:44 +020011330 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011331 continue;
11332
Willy Tarreau37406352012-04-23 16:16:37 +020011333 smp->ctx.a[0] = ctx->line + ctx->val;
11334 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011335 }
11336
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011337 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011338 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011339 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011340 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011341 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011342 &smp->data.u.str.str,
11343 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011344 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011345 found = 1;
11346 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011347 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011348 smp->flags |= SMP_F_NOT_LAST;
11349 return 1;
11350 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011351 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011352 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011353 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011354 /* all cookie headers and values were scanned. If we're looking for the
11355 * last occurrence, we may return it now.
11356 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011357 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011358 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011359 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011360}
11361
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011362/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011363 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011364 * multiple cookies may be parsed on the same line. The returned sample is of
11365 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011366 */
11367static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011368smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011369{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011370 struct http_txn *txn;
11371 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011372 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011373 const struct http_msg *msg;
11374 const char *hdr_name;
11375 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011376 int cnt;
11377 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011378 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011379
Willy Tarreau24e32d82012-04-23 23:55:44 +020011380 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011381 return 0;
11382
Willy Tarreaue333ec92012-04-16 16:26:40 +020011383 CHECK_HTTP_MESSAGE_FIRST();
11384
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011385 txn = smp->strm->txn;
11386 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011387
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011388 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011389 msg = &txn->req;
11390 hdr_name = "Cookie";
11391 hdr_name_len = 6;
11392 } else {
11393 msg = &txn->rsp;
11394 hdr_name = "Set-Cookie";
11395 hdr_name_len = 10;
11396 }
11397
Willy Tarreau9b28e032012-10-12 23:49:43 +020011398 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011399 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011400 ctx.idx = 0;
11401 cnt = 0;
11402
11403 while (1) {
11404 /* Note: val_beg == NULL every time we need to fetch a new header */
11405 if (!val_beg) {
11406 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11407 break;
11408
Willy Tarreau24e32d82012-04-23 23:55:44 +020011409 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011410 continue;
11411
11412 val_beg = ctx.line + ctx.val;
11413 val_end = val_beg + ctx.vlen;
11414 }
11415
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011416 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011417 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011418 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011419 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011420 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011421 &smp->data.u.str.str,
11422 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011423 cnt++;
11424 }
11425 }
11426
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011427 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011428 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011429 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011430 return 1;
11431}
11432
Willy Tarreau51539362012-05-08 12:46:28 +020011433/* Fetch an cookie's integer value. The integer value is returned. It
11434 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11435 */
11436static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011437smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011438{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011439 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011440
11441 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011442 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011443 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011444 }
11445
11446 return ret;
11447}
11448
Willy Tarreau8797c062007-05-07 00:55:35 +020011449/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011450/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011451/************************************************************************/
11452
David Cournapeau16023ee2010-12-23 20:55:41 +090011453/*
11454 * Given a path string and its length, find the position of beginning of the
11455 * query string. Returns NULL if no query string is found in the path.
11456 *
11457 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11458 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011459 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011460 */
bedis4c75cca2012-10-05 08:38:24 +020011461static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011462{
11463 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011464
bedis4c75cca2012-10-05 08:38:24 +020011465 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011466 return p ? p + 1 : NULL;
11467}
11468
bedis4c75cca2012-10-05 08:38:24 +020011469static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011470{
bedis4c75cca2012-10-05 08:38:24 +020011471 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011472}
11473
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011474/* after increasing a pointer value, it can exceed the first buffer
11475 * size. This function transform the value of <ptr> according with
11476 * the expected position. <chunks> is an array of the one or two
11477 * avalaible chunks. The first value is the start of the first chunk,
11478 * the second value if the end+1 of the first chunks. The third value
11479 * is NULL or the start of the second chunk and the fourth value is
11480 * the end+1 of the second chunk. The function returns 1 if does a
11481 * wrap, else returns 0.
11482 */
11483static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11484{
11485 if (*ptr < chunks[1])
11486 return 0;
11487 if (!chunks[2])
11488 return 0;
11489 *ptr = chunks[2] + ( *ptr - chunks[1] );
11490 return 1;
11491}
11492
David Cournapeau16023ee2010-12-23 20:55:41 +090011493/*
11494 * Given a url parameter, find the starting position of the first occurence,
11495 * or NULL if the parameter is not found.
11496 *
11497 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11498 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011499 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011500 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011501 * or the second chunk. The caller must be check the position before using the
11502 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011503 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011504static const char *
11505find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011506 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011507 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011508{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011509 const char *pos, *last, *equal;
11510 const char **bufs = chunks;
11511 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011512
David Cournapeau16023ee2010-12-23 20:55:41 +090011513
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011514 pos = bufs[0];
11515 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011516 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011517 /* Check the equal. */
11518 equal = pos + url_param_name_l;
11519 if (fix_pointer_if_wrap(chunks, &equal)) {
11520 if (equal >= chunks[3])
11521 return NULL;
11522 } else {
11523 if (equal >= chunks[1])
11524 return NULL;
11525 }
11526 if (*equal == '=') {
11527 if (pos + url_param_name_l > last) {
11528 /* process wrap case, we detect a wrap. In this case, the
11529 * comparison is performed in two parts.
11530 */
11531
11532 /* This is the end, we dont have any other chunk. */
11533 if (bufs != chunks || !bufs[2])
11534 return NULL;
11535
11536 /* Compute the length of each part of the comparison. */
11537 l1 = last - pos;
11538 l2 = url_param_name_l - l1;
11539
11540 /* The second buffer is too short to contain the compared string. */
11541 if (bufs[2] + l2 > bufs[3])
11542 return NULL;
11543
11544 if (memcmp(pos, url_param_name, l1) == 0 &&
11545 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11546 return pos;
11547
11548 /* Perform wrapping and jump the string who fail the comparison. */
11549 bufs += 2;
11550 pos = bufs[0] + l2;
11551 last = bufs[1];
11552
11553 } else {
11554 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011555 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11556 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011557 pos += url_param_name_l + 1;
11558 if (fix_pointer_if_wrap(chunks, &pos))
11559 last = bufs[2];
11560 }
11561 }
11562
11563 while (1) {
11564 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011565 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011566 pos++;
11567 if (pos < last)
11568 break;
11569 /* process buffer wrapping. */
11570 if (bufs != chunks || !bufs[2])
11571 return NULL;
11572 bufs += 2;
11573 pos = bufs[0];
11574 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011575 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011576 pos++;
11577 }
11578 return NULL;
11579}
11580
11581/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011582 * Given a url parameter name and a query string, find the next value.
11583 * An empty url_param_name matches the first available parameter.
11584 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11585 * respectively provide a pointer to the value and its end.
11586 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011587 */
11588static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011589find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011590 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011591 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011592{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011593 const char *arg_start, *qs_end;
11594 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011595
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011596 arg_start = chunks[0];
11597 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011598 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011599 /* Looks for an argument name. */
11600 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011601 url_param_name, url_param_name_l,
11602 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011603 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011604 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011605 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011606 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011607 if (!arg_start)
11608 return 0;
11609
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011610 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011611 while (1) {
11612 /* looks for the first argument. */
11613 value_start = memchr(arg_start, '=', qs_end - arg_start);
11614 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011615 /* Check for wrapping. */
11616 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011617 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011618 chunks[2]) {
11619 arg_start = chunks[2];
11620 qs_end = chunks[3];
11621 continue;
11622 }
11623 return 0;
11624 }
11625 break;
11626 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011627 value_start++;
11628 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011629 else {
11630 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011631 value_start = arg_start + url_param_name_l + 1;
11632
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011633 /* Check for pointer wrapping. */
11634 if (fix_pointer_if_wrap(chunks, &value_start)) {
11635 /* Update the end pointer. */
11636 qs_end = chunks[3];
11637
11638 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011639 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011640 return 0;
11641 }
11642 }
11643
David Cournapeau16023ee2010-12-23 20:55:41 +090011644 value_end = value_start;
11645
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011646 while (1) {
11647 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11648 value_end++;
11649 if (value_end < qs_end)
11650 break;
11651 /* process buffer wrapping. */
11652 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011653 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011654 chunks[2]) {
11655 value_end = chunks[2];
11656 qs_end = chunks[3];
11657 continue;
11658 }
11659 break;
11660 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011661
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011662 *vstart = value_start;
11663 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011664 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011665}
11666
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011667/* This scans a URL-encoded query string. It takes an optionally wrapping
11668 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11669 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11670 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011671 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011672static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011673smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011674{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011675 const char *vstart, *vend;
11676 struct chunk *temp;
11677 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011678
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011679 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011680 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011681 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011682 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011683 return 0;
11684
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011685 /* Create sample. If the value is contiguous, return the pointer as CONST,
11686 * if the value is wrapped, copy-it in a buffer.
11687 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011688 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011689 if (chunks[2] &&
11690 vstart >= chunks[0] && vstart <= chunks[1] &&
11691 vend >= chunks[2] && vend <= chunks[3]) {
11692 /* Wrapped case. */
11693 temp = get_trash_chunk();
11694 memcpy(temp->str, vstart, chunks[1] - vstart);
11695 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011696 smp->data.u.str.str = temp->str;
11697 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011698 } else {
11699 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011700 smp->data.u.str.str = (char *)vstart;
11701 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011702 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11703 }
11704
11705 /* Update context, check wrapping. */
11706 chunks[0] = vend;
11707 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11708 chunks[1] = chunks[3];
11709 chunks[2] = NULL;
11710 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011711
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011712 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011713 smp->flags |= SMP_F_NOT_LAST;
11714
David Cournapeau16023ee2010-12-23 20:55:41 +090011715 return 1;
11716}
11717
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011718/* This function iterates over each parameter of the query string. It uses
11719 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011720 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11721 * An optional parameter name is passed in args[0], otherwise any parameter is
11722 * considered. It supports an optional delimiter argument for the beginning of
11723 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011724 */
11725static int
11726smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11727{
11728 struct http_msg *msg;
11729 char delim = '?';
11730 const char *name;
11731 int name_len;
11732
Dragan Dosen26f77e52015-05-25 10:02:11 +020011733 if (!args ||
11734 (args[0].type && args[0].type != ARGT_STR) ||
11735 (args[1].type && args[1].type != ARGT_STR))
11736 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011737
Dragan Dosen26f77e52015-05-25 10:02:11 +020011738 name = "";
11739 name_len = 0;
11740 if (args->type == ARGT_STR) {
11741 name = args->data.str.str;
11742 name_len = args->data.str.len;
11743 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011744
Dragan Dosen26f77e52015-05-25 10:02:11 +020011745 if (args[1].type)
11746 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011747
Dragan Dosen26f77e52015-05-25 10:02:11 +020011748 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011749 CHECK_HTTP_MESSAGE_FIRST();
11750
11751 msg = &smp->strm->txn->req;
11752
11753 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11754 msg->sl.rq.u_l, delim);
11755 if (!smp->ctx.a[0])
11756 return 0;
11757
11758 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011759
11760 /* Assume that the context is filled with NULL pointer
11761 * before the first call.
11762 * smp->ctx.a[2] = NULL;
11763 * smp->ctx.a[3] = NULL;
11764 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011765 }
11766
11767 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11768}
11769
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011770/* This function iterates over each parameter of the body. This requires
11771 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011772 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11773 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11774 * optional second part if the body wraps at the end of the buffer. An optional
11775 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011776 */
11777static int
11778smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11779{
11780 struct http_txn *txn = smp->strm->txn;
11781 struct http_msg *msg;
11782 unsigned long len;
11783 unsigned long block1;
11784 char *body;
11785 const char *name;
11786 int name_len;
11787
11788 if (!args || (args[0].type && args[0].type != ARGT_STR))
11789 return 0;
11790
11791 name = "";
11792 name_len = 0;
11793 if (args[0].type == ARGT_STR) {
11794 name = args[0].data.str.str;
11795 name_len = args[0].data.str.len;
11796 }
11797
11798 if (!smp->ctx.a[0]) { // first call, find the query string
11799 CHECK_HTTP_MESSAGE_FIRST();
11800
11801 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
11802 msg = &txn->req;
11803 else
11804 msg = &txn->rsp;
11805
11806 len = http_body_bytes(msg);
11807 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11808
11809 block1 = len;
11810 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11811 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11812
11813 if (block1 == len) {
11814 /* buffer is not wrapped (or empty) */
11815 smp->ctx.a[0] = body;
11816 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011817
11818 /* Assume that the context is filled with NULL pointer
11819 * before the first call.
11820 * smp->ctx.a[2] = NULL;
11821 * smp->ctx.a[3] = NULL;
11822 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011823 }
11824 else {
11825 /* buffer is wrapped, we need to defragment it */
11826 smp->ctx.a[0] = body;
11827 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011828 smp->ctx.a[2] = msg->chn->buf->data;
11829 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011830 }
11831 }
11832 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11833}
11834
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011835/* Return the signed integer value for the specified url parameter (see url_param
11836 * above).
11837 */
11838static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011839smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011840{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011841 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011842
11843 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011844 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011845 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011846 }
11847
11848 return ret;
11849}
11850
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011851/* This produces a 32-bit hash of the concatenation of the first occurrence of
11852 * the Host header followed by the path component if it begins with a slash ('/').
11853 * This means that '*' will not be added, resulting in exactly the first Host
11854 * entry. If no Host header is found, then the path is used. The resulting value
11855 * is hashed using the url hash followed by a full avalanche hash and provides a
11856 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11857 * high-traffic sites without having to store whole paths.
11858 * this differs from the base32 functions in that it includes the url parameters
11859 * as well as the path
11860 */
11861static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011862smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011863{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011864 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011865 struct hdr_ctx ctx;
11866 unsigned int hash = 0;
11867 char *ptr, *beg, *end;
11868 int len;
11869
11870 CHECK_HTTP_MESSAGE_FIRST();
11871
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011872 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011873 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011874 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011875 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11876 ptr = ctx.line + ctx.val;
11877 len = ctx.vlen;
11878 while (len--)
11879 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11880 }
11881
11882 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011883 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011884 beg = http_get_path(txn);
11885 if (!beg)
11886 beg = end;
11887
11888 for (ptr = beg; ptr < end ; ptr++);
11889
11890 if (beg < ptr && *beg == '/') {
11891 while (beg < ptr)
11892 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11893 }
11894 hash = full_hash(hash);
11895
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011896 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011897 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011898 smp->flags = SMP_F_VOL_1ST;
11899 return 1;
11900}
11901
11902/* This concatenates the source address with the 32-bit hash of the Host and
11903 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11904 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11905 * on the source address length. The URL hash is stored before the address so
11906 * that in environments where IPv6 is insignificant, truncating the output to
11907 * 8 bytes would still work.
11908 */
11909static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011910smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011911{
11912 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011913 struct connection *cli_conn = objt_conn(smp->sess->origin);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011914 unsigned int hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011915
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011916 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011917 return 0;
11918
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011919 /* The returned hash is a 32 bytes integer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011920 hash = smp->data.u.sint;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011921
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011922 temp = get_trash_chunk();
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011923 memcpy(temp->str + temp->len, &hash, sizeof(hash));
11924 temp->len += sizeof(hash);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011925
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011926 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011927 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011928 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011929 temp->len += 4;
11930 break;
11931 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011932 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011933 temp->len += 16;
11934 break;
11935 default:
11936 return 0;
11937 }
11938
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011939 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011940 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011941 return 1;
11942}
11943
Willy Tarreau185b5c42012-04-26 15:11:51 +020011944/* This function is used to validate the arguments passed to any "hdr" fetch
11945 * keyword. These keywords support an optional positive or negative occurrence
11946 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11947 * is assumed that the types are already the correct ones. Returns 0 on error,
11948 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11949 * error message in case of error, that the caller is responsible for freeing.
11950 * The initial location must either be freeable or NULL.
11951 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011952int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011953{
11954 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011955 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011956 return 0;
11957 }
11958 return 1;
11959}
11960
Willy Tarreau276fae92013-07-25 14:36:01 +020011961/* takes an UINT value on input supposed to represent the time since EPOCH,
11962 * adds an optional offset found in args[0] and emits a string representing
11963 * the date in RFC-1123/5322 format.
11964 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011965static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011966{
11967 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11968 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11969 struct chunk *temp;
11970 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011971 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011972 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011973
11974 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011975 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011976 curr_date += args[0].data.sint;
11977
11978 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011979 if (!tm)
11980 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011981
11982 temp = get_trash_chunk();
11983 temp->len = snprintf(temp->str, temp->size - temp->len,
11984 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11985 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11986 tm->tm_hour, tm->tm_min, tm->tm_sec);
11987
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011988 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011989 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011990 return 1;
11991}
11992
Thierry FOURNIERad903512014-04-11 17:51:01 +020011993/* Match language range with language tag. RFC2616 14.4:
11994 *
11995 * A language-range matches a language-tag if it exactly equals
11996 * the tag, or if it exactly equals a prefix of the tag such
11997 * that the first tag character following the prefix is "-".
11998 *
11999 * Return 1 if the strings match, else return 0.
12000 */
12001static inline int language_range_match(const char *range, int range_len,
12002 const char *tag, int tag_len)
12003{
12004 const char *end = range + range_len;
12005 const char *tend = tag + tag_len;
12006 while (range < end) {
12007 if (*range == '-' && tag == tend)
12008 return 1;
12009 if (*range != *tag || tag == tend)
12010 return 0;
12011 range++;
12012 tag++;
12013 }
12014 /* Return true only if the last char of the tag is matched. */
12015 return tag == tend;
12016}
12017
12018/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012019static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012020{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012021 const char *al = smp->data.u.str.str;
12022 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012023 const char *token;
12024 int toklen;
12025 int qvalue;
12026 const char *str;
12027 const char *w;
12028 int best_q = 0;
12029
12030 /* Set the constant to the sample, because the output of the
12031 * function will be peek in the constant configuration string.
12032 */
12033 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012034 smp->data.u.str.size = 0;
12035 smp->data.u.str.str = "";
12036 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012037
12038 /* Parse the accept language */
12039 while (1) {
12040
12041 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012042 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012043 al++;
12044 if (al >= end)
12045 break;
12046
12047 /* Start of the fisrt word. */
12048 token = al;
12049
12050 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012051 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012052 al++;
12053 if (al == token)
12054 goto expect_comma;
12055
12056 /* Length of the token. */
12057 toklen = al - token;
12058 qvalue = 1000;
12059
12060 /* Check if the token exists in the list. If the token not exists,
12061 * jump to the next token.
12062 */
12063 str = args[0].data.str.str;
12064 w = str;
12065 while (1) {
12066 if (*str == ';' || *str == '\0') {
12067 if (language_range_match(token, toklen, w, str-w))
12068 goto look_for_q;
12069 if (*str == '\0')
12070 goto expect_comma;
12071 w = str + 1;
12072 }
12073 str++;
12074 }
12075 goto expect_comma;
12076
12077look_for_q:
12078
12079 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012080 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012081 al++;
12082 if (al >= end)
12083 goto process_value;
12084
12085 /* If ',' is found, process the result */
12086 if (*al == ',')
12087 goto process_value;
12088
12089 /* If the character is different from ';', look
12090 * for the end of the header part in best effort.
12091 */
12092 if (*al != ';')
12093 goto expect_comma;
12094
12095 /* Assumes that the char is ';', now expect "q=". */
12096 al++;
12097
12098 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012099 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012100 al++;
12101 if (al >= end)
12102 goto process_value;
12103
12104 /* Expect 'q'. If no 'q', continue in best effort */
12105 if (*al != 'q')
12106 goto process_value;
12107 al++;
12108
12109 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012110 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012111 al++;
12112 if (al >= end)
12113 goto process_value;
12114
12115 /* Expect '='. If no '=', continue in best effort */
12116 if (*al != '=')
12117 goto process_value;
12118 al++;
12119
12120 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012121 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012122 al++;
12123 if (al >= end)
12124 goto process_value;
12125
12126 /* Parse the q value. */
12127 qvalue = parse_qvalue(al, &al);
12128
12129process_value:
12130
12131 /* If the new q value is the best q value, then store the associated
12132 * language in the response. If qvalue is the biggest value (1000),
12133 * break the process.
12134 */
12135 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012136 smp->data.u.str.str = (char *)w;
12137 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012138 if (qvalue >= 1000)
12139 break;
12140 best_q = qvalue;
12141 }
12142
12143expect_comma:
12144
12145 /* Expect comma or end. If the end is detected, quit the loop. */
12146 while (al < end && *al != ',')
12147 al++;
12148 if (al >= end)
12149 break;
12150
12151 /* Comma is found, jump it and restart the analyzer. */
12152 al++;
12153 }
12154
12155 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012156 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12157 smp->data.u.str.str = args[1].data.str.str;
12158 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012159 }
12160
12161 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012162 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012163}
12164
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012165/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012166static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012167{
12168 /* If the constant flag is set or if not size is avalaible at
12169 * the end of the buffer, copy the string in other buffer
12170 * before decoding.
12171 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012172 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012173 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012174 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12175 smp->data.u.str.str = str->str;
12176 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012177 smp->flags &= ~SMP_F_CONST;
12178 }
12179
12180 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012181 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12182 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012183 return 1;
12184}
12185
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012186static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12187{
12188 struct proxy *fe = strm_fe(smp->strm);
12189 int idx, i;
12190 struct cap_hdr *hdr;
12191 int len;
12192
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012193 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012194 return 0;
12195
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012196 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012197
12198 /* Check the availibity of the capture id. */
12199 if (idx > fe->nb_req_cap - 1)
12200 return 0;
12201
12202 /* Look for the original configuration. */
12203 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12204 hdr != NULL && i != idx ;
12205 i--, hdr = hdr->next);
12206 if (!hdr)
12207 return 0;
12208
12209 /* check for the memory allocation */
12210 if (smp->strm->req_cap[hdr->index] == NULL)
12211 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12212 if (smp->strm->req_cap[hdr->index] == NULL)
12213 return 0;
12214
12215 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012216 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012217 if (len > hdr->len)
12218 len = hdr->len;
12219
12220 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012221 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012222 smp->strm->req_cap[idx][len] = '\0';
12223
12224 return 1;
12225}
12226
12227static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12228{
12229 struct proxy *fe = strm_fe(smp->strm);
12230 int idx, i;
12231 struct cap_hdr *hdr;
12232 int len;
12233
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012234 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012235 return 0;
12236
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012237 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012238
12239 /* Check the availibity of the capture id. */
12240 if (idx > fe->nb_rsp_cap - 1)
12241 return 0;
12242
12243 /* Look for the original configuration. */
12244 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12245 hdr != NULL && i != idx ;
12246 i--, hdr = hdr->next);
12247 if (!hdr)
12248 return 0;
12249
12250 /* check for the memory allocation */
12251 if (smp->strm->res_cap[hdr->index] == NULL)
12252 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12253 if (smp->strm->res_cap[hdr->index] == NULL)
12254 return 0;
12255
12256 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012257 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012258 if (len > hdr->len)
12259 len = hdr->len;
12260
12261 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012262 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012263 smp->strm->res_cap[idx][len] = '\0';
12264
12265 return 1;
12266}
12267
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012268/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012269 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012270 * the relevant part of the request line accordingly. Then it updates various
12271 * pointers to the next elements which were moved, and the total buffer length.
12272 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012273 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12274 * error, though this can be revisited when this code is finally exploited.
12275 *
12276 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12277 * query string and 3 to replace uri.
12278 *
12279 * In query string case, the mark question '?' must be set at the start of the
12280 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012281 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012282int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012283 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012284{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012285 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012286 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012287 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012288 int delta;
12289
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012290 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012291 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012292 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012293 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12294
12295 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012296 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012297 txn->req.sl.rq.m_l += delta;
12298 txn->req.sl.rq.u += delta;
12299 txn->req.sl.rq.v += delta;
12300 break;
12301
12302 case 1: // path
12303 cur_ptr = http_get_path(txn);
12304 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012305 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012306
12307 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012308 while (cur_end < s->req.buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012309 cur_end++;
12310
12311 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012312 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012313 txn->req.sl.rq.u_l += delta;
12314 txn->req.sl.rq.v += delta;
12315 break;
12316
12317 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012318 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012319 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012320 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12321 while (cur_ptr < cur_end && *cur_ptr != '?')
12322 cur_ptr++;
12323
12324 /* skip the question mark or indicate that we must insert it
12325 * (but only if the format string is not empty then).
12326 */
12327 if (cur_ptr < cur_end)
12328 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012329 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012330 offset = 0;
12331
12332 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012333 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012334 txn->req.sl.rq.u_l += delta;
12335 txn->req.sl.rq.v += delta;
12336 break;
12337
12338 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012339 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012340 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12341
12342 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012343 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012344 txn->req.sl.rq.u_l += delta;
12345 txn->req.sl.rq.v += delta;
12346 break;
12347
12348 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012349 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012350 }
12351
12352 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012353 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012354 txn->req.sl.rq.l += delta;
12355 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012356 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012357 return 0;
12358}
12359
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012360/* This function replace the HTTP status code and the associated message. The
12361 * variable <status> contains the new status code. This function never fails.
12362 */
12363void http_set_status(unsigned int status, struct stream *s)
12364{
12365 struct http_txn *txn = s->txn;
12366 char *cur_ptr, *cur_end;
12367 int delta;
12368 char *res;
12369 int c_l;
12370 const char *msg;
12371 int msg_len;
12372
12373 chunk_reset(&trash);
12374
12375 res = ultoa_o(status, trash.str, trash.size);
12376 c_l = res - trash.str;
12377
12378 trash.str[c_l] = ' ';
12379 trash.len = c_l + 1;
12380
12381 msg = get_reason(status);
12382 msg_len = strlen(msg);
12383
12384 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12385 trash.len += msg_len;
12386
12387 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12388 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12389
12390 /* commit changes and adjust message */
12391 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12392
12393 /* adjust res line offsets and lengths */
12394 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12395 txn->rsp.sl.st.c_l = c_l;
12396 txn->rsp.sl.st.r_l = msg_len;
12397
12398 delta = trash.len - (cur_end - cur_ptr);
12399 txn->rsp.sl.st.l += delta;
12400 txn->hdr_idx.v[0].len += delta;
12401 http_msg_move_end(&txn->rsp, delta);
12402}
12403
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012404/* This function executes one of the set-{method,path,query,uri} actions. It
12405 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012406 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012407 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012408 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12409 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012410 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012411enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012412 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012413{
12414 chunk_reset(&trash);
12415
12416 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012417 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012418 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012419 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012420
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012421 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012422 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012423}
12424
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012425/* This function is just a compliant action wrapper for "set-status". */
12426enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012427 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012428{
12429 http_set_status(rule->arg.status.code, s);
12430 return ACT_RET_CONT;
12431}
12432
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012433/* parse an http-request action among :
12434 * set-method
12435 * set-path
12436 * set-query
12437 * set-uri
12438 *
12439 * All of them accept a single argument of type string representing a log-format.
12440 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12441 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012442 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012443 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012444enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12445 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012446{
12447 int cur_arg = *orig_arg;
12448
Thierry FOURNIER42148732015-09-02 17:17:33 +020012449 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012450
12451 switch (args[0][4]) {
12452 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012453 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012454 rule->action_ptr = http_action_set_req_line;
12455 break;
12456 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012457 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012458 rule->action_ptr = http_action_set_req_line;
12459 break;
12460 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012461 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012462 rule->action_ptr = http_action_set_req_line;
12463 break;
12464 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012465 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012466 rule->action_ptr = http_action_set_req_line;
12467 break;
12468 default:
12469 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012470 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012471 }
12472
12473 if (!*args[cur_arg] ||
12474 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12475 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012476 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012477 }
12478
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012479 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012480 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012481 parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012482 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12483 proxy->conf.args.file, proxy->conf.args.line);
12484
12485 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012486 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012487}
12488
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012489/* parse set-status action:
12490 * This action accepts a single argument of type int representing
12491 * an http status code. It returns ACT_RET_PRS_OK on success,
12492 * ACT_RET_PRS_ERR on error.
12493 */
12494enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12495 struct act_rule *rule, char **err)
12496{
12497 char *error;
12498
Thierry FOURNIER42148732015-09-02 17:17:33 +020012499 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012500 rule->action_ptr = action_http_set_status;
12501
12502 /* Check if an argument is available */
12503 if (!*args[*orig_arg]) {
12504 memprintf(err, "expects exactly 1 argument <status>");
12505 return ACT_RET_PRS_ERR;
12506 }
12507
12508 /* convert status code as integer */
12509 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12510 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12511 memprintf(err, "expects an integer status code between 100 and 999");
12512 return ACT_RET_PRS_ERR;
12513 }
12514
12515 (*orig_arg)++;
12516 return ACT_RET_PRS_OK;
12517}
12518
Willy Tarreaua9083d02015-05-08 15:27:59 +020012519/* This function executes the "capture" action. It executes a fetch expression,
12520 * turns the result into a string and puts it in a capture slot. It always
12521 * returns 1. If an error occurs the action is cancelled, but the rule
12522 * processing continues.
12523 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012524enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012525 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012526{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012527 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012528 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012529 char **cap = s->req_cap;
12530 int len;
12531
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012532 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012533 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012534 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012535
12536 if (cap[h->index] == NULL)
12537 cap[h->index] = pool_alloc2(h->pool);
12538
12539 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012540 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012541
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012542 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012543 if (len > h->len)
12544 len = h->len;
12545
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012546 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012547 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012548 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012549}
12550
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012551/* This function executes the "capture" action and store the result in a
12552 * capture slot if exists. It executes a fetch expression, turns the result
12553 * into a string and puts it in a capture slot. It always returns 1. If an
12554 * error occurs the action is cancelled, but the rule processing continues.
12555 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012556enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012557 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012558{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012559 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012560 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012561 char **cap = s->req_cap;
12562 struct proxy *fe = strm_fe(s);
12563 int len;
12564 int i;
12565
12566 /* Look for the original configuration. */
12567 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012568 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012569 i--, h = h->next);
12570 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012571 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012572
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012573 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012574 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012575 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012576
12577 if (cap[h->index] == NULL)
12578 cap[h->index] = pool_alloc2(h->pool);
12579
12580 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012581 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012582
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012583 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012584 if (len > h->len)
12585 len = h->len;
12586
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012587 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012588 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012589 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012590}
12591
Willy Tarreaua9083d02015-05-08 15:27:59 +020012592/* parse an "http-request capture" action. It takes a single argument which is
12593 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012594 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012595 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012596 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012597enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12598 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012599{
12600 struct sample_expr *expr;
12601 struct cap_hdr *hdr;
12602 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012603 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012604
12605 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12606 if (strcmp(args[cur_arg], "if") == 0 ||
12607 strcmp(args[cur_arg], "unless") == 0)
12608 break;
12609
12610 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012611 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012612 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012613 }
12614
Willy Tarreaua9083d02015-05-08 15:27:59 +020012615 cur_arg = *orig_arg;
12616 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12617 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012618 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012619
12620 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12621 memprintf(err,
12622 "fetch method '%s' extracts information from '%s', none of which is available here",
12623 args[cur_arg-1], sample_src_names(expr->fetch->use));
12624 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012625 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012626 }
12627
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012628 if (!args[cur_arg] || !*args[cur_arg]) {
12629 memprintf(err, "expects 'len or 'id'");
12630 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012631 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012632 }
12633
Willy Tarreaua9083d02015-05-08 15:27:59 +020012634 if (strcmp(args[cur_arg], "len") == 0) {
12635 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012636
12637 if (!(px->cap & PR_CAP_FE)) {
12638 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012639 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012640 }
12641
12642 proxy->conf.args.ctx = ARGC_CAP;
12643
Willy Tarreaua9083d02015-05-08 15:27:59 +020012644 if (!args[cur_arg]) {
12645 memprintf(err, "missing length value");
12646 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012647 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012648 }
12649 /* we copy the table name for now, it will be resolved later */
12650 len = atoi(args[cur_arg]);
12651 if (len <= 0) {
12652 memprintf(err, "length must be > 0");
12653 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012654 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012655 }
12656 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012657
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012658 if (!len) {
12659 memprintf(err, "a positive 'len' argument is mandatory");
12660 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012661 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012662 }
12663
12664 hdr = calloc(sizeof(struct cap_hdr), 1);
12665 hdr->next = px->req_cap;
12666 hdr->name = NULL; /* not a header capture */
12667 hdr->namelen = 0;
12668 hdr->len = len;
12669 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12670 hdr->index = px->nb_req_cap++;
12671
12672 px->req_cap = hdr;
12673 px->to_log |= LW_REQHDR;
12674
Thierry FOURNIER42148732015-09-02 17:17:33 +020012675 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012676 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012677 rule->arg.cap.expr = expr;
12678 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012679 }
12680
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012681 else if (strcmp(args[cur_arg], "id") == 0) {
12682 int id;
12683 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012684
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012685 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012686
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012687 if (!args[cur_arg]) {
12688 memprintf(err, "missing id value");
12689 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012690 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012691 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012692
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012693 id = strtol(args[cur_arg], &error, 10);
12694 if (*error != '\0') {
12695 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12696 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012697 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012698 }
12699 cur_arg++;
12700
12701 proxy->conf.args.ctx = ARGC_CAP;
12702
Thierry FOURNIER42148732015-09-02 17:17:33 +020012703 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012704 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012705 rule->arg.capid.expr = expr;
12706 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012707 }
12708
12709 else {
12710 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12711 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012712 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012713 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012714
12715 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012716 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012717}
12718
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012719/* This function executes the "capture" action and store the result in a
12720 * capture slot if exists. It executes a fetch expression, turns the result
12721 * into a string and puts it in a capture slot. It always returns 1. If an
12722 * error occurs the action is cancelled, but the rule processing continues.
12723 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012724enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012725 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012726{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012727 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012728 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012729 char **cap = s->res_cap;
12730 struct proxy *fe = strm_fe(s);
12731 int len;
12732 int i;
12733
12734 /* Look for the original configuration. */
12735 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012736 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012737 i--, h = h->next);
12738 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012739 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012740
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012741 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012742 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012743 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012744
12745 if (cap[h->index] == NULL)
12746 cap[h->index] = pool_alloc2(h->pool);
12747
12748 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012749 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012750
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012751 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012752 if (len > h->len)
12753 len = h->len;
12754
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012755 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012756 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012757 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012758}
12759
12760/* parse an "http-response capture" action. It takes a single argument which is
12761 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12762 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012763 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012764 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012765enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12766 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012767{
12768 struct sample_expr *expr;
12769 int cur_arg;
12770 int id;
12771 char *error;
12772
12773 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12774 if (strcmp(args[cur_arg], "if") == 0 ||
12775 strcmp(args[cur_arg], "unless") == 0)
12776 break;
12777
12778 if (cur_arg < *orig_arg + 3) {
12779 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012780 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012781 }
12782
12783 cur_arg = *orig_arg;
12784 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12785 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012786 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012787
12788 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12789 memprintf(err,
12790 "fetch method '%s' extracts information from '%s', none of which is available here",
12791 args[cur_arg-1], sample_src_names(expr->fetch->use));
12792 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012793 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012794 }
12795
12796 if (!args[cur_arg] || !*args[cur_arg]) {
12797 memprintf(err, "expects 'len or 'id'");
12798 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012799 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012800 }
12801
12802 if (strcmp(args[cur_arg], "id") != 0) {
12803 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12804 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012805 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012806 }
12807
12808 cur_arg++;
12809
12810 if (!args[cur_arg]) {
12811 memprintf(err, "missing id value");
12812 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012813 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012814 }
12815
12816 id = strtol(args[cur_arg], &error, 10);
12817 if (*error != '\0') {
12818 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12819 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012820 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012821 }
12822 cur_arg++;
12823
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012824 proxy->conf.args.ctx = ARGC_CAP;
12825
Thierry FOURNIER42148732015-09-02 17:17:33 +020012826 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012827 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012828 rule->arg.capid.expr = expr;
12829 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012830
12831 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012832 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012833}
12834
William Lallemand73025dd2014-04-24 14:38:37 +020012835/*
12836 * Return the struct http_req_action_kw associated to a keyword.
12837 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012838struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012839{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012840 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012841}
12842
12843/*
12844 * Return the struct http_res_action_kw associated to a keyword.
12845 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012846struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012847{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012848 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012849}
12850
Willy Tarreau4a568972010-05-12 08:08:50 +020012851/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012852/* All supported ACL keywords must be declared here. */
12853/************************************************************************/
12854
12855/* Note: must not be declared <const> as its list will be overwritten.
12856 * Please take care of keeping this list alphabetically sorted.
12857 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012858static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012859 { "base", "base", PAT_MATCH_STR },
12860 { "base_beg", "base", PAT_MATCH_BEG },
12861 { "base_dir", "base", PAT_MATCH_DIR },
12862 { "base_dom", "base", PAT_MATCH_DOM },
12863 { "base_end", "base", PAT_MATCH_END },
12864 { "base_len", "base", PAT_MATCH_LEN },
12865 { "base_reg", "base", PAT_MATCH_REG },
12866 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012867
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012868 { "cook", "req.cook", PAT_MATCH_STR },
12869 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12870 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12871 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12872 { "cook_end", "req.cook", PAT_MATCH_END },
12873 { "cook_len", "req.cook", PAT_MATCH_LEN },
12874 { "cook_reg", "req.cook", PAT_MATCH_REG },
12875 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012876
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012877 { "hdr", "req.hdr", PAT_MATCH_STR },
12878 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12879 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12880 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12881 { "hdr_end", "req.hdr", PAT_MATCH_END },
12882 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12883 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12884 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012885
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012886 /* these two declarations uses strings with list storage (in place
12887 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12888 * and delete functions are relative to the list management. The parse
12889 * and match method are related to the corresponding fetch methods. This
12890 * is very particular ACL declaration mode.
12891 */
12892 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12893 { "method", NULL, PAT_MATCH_STR, pat_parse_meth, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012894
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012895 { "path", "path", PAT_MATCH_STR },
12896 { "path_beg", "path", PAT_MATCH_BEG },
12897 { "path_dir", "path", PAT_MATCH_DIR },
12898 { "path_dom", "path", PAT_MATCH_DOM },
12899 { "path_end", "path", PAT_MATCH_END },
12900 { "path_len", "path", PAT_MATCH_LEN },
12901 { "path_reg", "path", PAT_MATCH_REG },
12902 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012903
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012904 { "req_ver", "req.ver", PAT_MATCH_STR },
12905 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012906
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012907 { "scook", "res.cook", PAT_MATCH_STR },
12908 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12909 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12910 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12911 { "scook_end", "res.cook", PAT_MATCH_END },
12912 { "scook_len", "res.cook", PAT_MATCH_LEN },
12913 { "scook_reg", "res.cook", PAT_MATCH_REG },
12914 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012915
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012916 { "shdr", "res.hdr", PAT_MATCH_STR },
12917 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12918 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12919 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12920 { "shdr_end", "res.hdr", PAT_MATCH_END },
12921 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12922 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12923 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012924
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012925 { "url", "url", PAT_MATCH_STR },
12926 { "url_beg", "url", PAT_MATCH_BEG },
12927 { "url_dir", "url", PAT_MATCH_DIR },
12928 { "url_dom", "url", PAT_MATCH_DOM },
12929 { "url_end", "url", PAT_MATCH_END },
12930 { "url_len", "url", PAT_MATCH_LEN },
12931 { "url_reg", "url", PAT_MATCH_REG },
12932 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012933
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012934 { "urlp", "urlp", PAT_MATCH_STR },
12935 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12936 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12937 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12938 { "urlp_end", "urlp", PAT_MATCH_END },
12939 { "urlp_len", "urlp", PAT_MATCH_LEN },
12940 { "urlp_reg", "urlp", PAT_MATCH_REG },
12941 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012942
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012943 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012944}};
12945
12946/************************************************************************/
12947/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012948/************************************************************************/
12949/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012950static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012951 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012952 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012953 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12954
Willy Tarreau87b09662015-04-03 00:22:06 +020012955 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012956 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012957
12958 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012959 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12960 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12961 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012962
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012963 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12964 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012965
Willy Tarreau409bcde2013-01-08 00:31:00 +010012966 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12967 * are only here to match the ACL's name, are request-only and are used
12968 * for ACL compatibility only.
12969 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012970 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12971 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012972 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12973 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012974
12975 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12976 * only here to match the ACL's name, are request-only and are used for
12977 * ACL compatibility only.
12978 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012979 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012980 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012981 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012982 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012983
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012984 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012985 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012986 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012987 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012988 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012989 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012990
12991 /* HTTP protocol on the request path */
12992 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012993 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012994
12995 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012996 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12997 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012998
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012999 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013000 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13001 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013002 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013003
Willy Tarreau18ed2562013-01-14 15:56:36 +010013004 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013005 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13006 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013007
Willy Tarreau18ed2562013-01-14 15:56:36 +010013008 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013009 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013010 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13011 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013012
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013013 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013014 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013015 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013016 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013017 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
Willy Tarreaueb27ec72015-02-20 13:55:29 +010013018 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013019 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013020
13021 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013022 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013023 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13024 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013025
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013026 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013027 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013028 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013029 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013030 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
Willy Tarreaueb27ec72015-02-20 13:55:29 +010013031 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013032 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013033
Willy Tarreau409bcde2013-01-08 00:31:00 +010013034 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013035 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013036 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13037 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013038 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013039
13040 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013041 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013042 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013043 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013044 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013045
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013046 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013047 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013048 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013049 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013050 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013051 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013052 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13053 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013054 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013055 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013056}};
13057
Willy Tarreau8797c062007-05-07 00:55:35 +020013058
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013059/************************************************************************/
13060/* All supported converter keywords must be declared here. */
13061/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013062/* Note: must not be declared <const> as its list will be overwritten */
13063static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013064 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013065 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013066 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13067 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013068 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013069 { NULL, NULL, 0, 0, 0 },
13070}};
13071
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013072
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013073/************************************************************************/
13074/* All supported http-request action keywords must be declared here. */
13075/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013076struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013077 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013078 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013079 { "set-method", parse_set_req_line },
13080 { "set-path", parse_set_req_line },
13081 { "set-query", parse_set_req_line },
13082 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013083 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013084 }
13085};
13086
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013087struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013088 .kw = {
13089 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013090 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013091 { NULL, NULL }
13092 }
13093};
13094
Willy Tarreau8797c062007-05-07 00:55:35 +020013095__attribute__((constructor))
13096static void __http_protocol_init(void)
13097{
13098 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013099 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013100 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013101 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013102 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020013103}
13104
13105
Willy Tarreau58f10d72006-12-04 02:26:12 +010013106/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013107 * Local variables:
13108 * c-indent-level: 8
13109 * c-basic-offset: 8
13110 * End:
13111 */