blob: e362a96ffbd20172e8ef541517c7af71cedb7bb7 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreau8797c062007-05-07 00:55:35 +020044#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020045#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020064#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020068#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020069
Willy Tarreau522d6c02009-12-06 18:49:18 +010070const char HTTP_100[] =
71 "HTTP/1.1 100 Continue\r\n\r\n";
72
73const struct chunk http_100_chunk = {
74 .str = (char *)&HTTP_100,
75 .len = sizeof(HTTP_100)-1
76};
77
Willy Tarreaua9679ac2010-01-03 17:32:57 +010078/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020079const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010081 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020082 "Location: "; /* not terminated since it will be concatenated with the URL */
83
Willy Tarreau0f772532006-12-23 20:51:41 +010084const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010088 "Location: "; /* not terminated since it will be concatenated with the URL */
89
90/* same as 302 except that the browser MUST retry with the GET method */
91const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010094 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010095 "Location: "; /* not terminated since it will be concatenated with the URL */
96
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040097
98/* same as 302 except that the browser MUST retry with the same method */
99const char *HTTP_307 =
100 "HTTP/1.1 307 Temporary Redirect\r\n"
101 "Cache-Control: no-cache\r\n"
102 "Content-length: 0\r\n"
103 "Location: "; /* not terminated since it will be concatenated with the URL */
104
105/* same as 301 except that the browser MUST retry with the same method */
106const char *HTTP_308 =
107 "HTTP/1.1 308 Permanent Redirect\r\n"
108 "Content-length: 0\r\n"
109 "Location: "; /* not terminated since it will be concatenated with the URL */
110
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
112const char *HTTP_401_fmt =
113 "HTTP/1.0 401 Unauthorized\r\n"
114 "Cache-Control: no-cache\r\n"
115 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200116 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
118 "\r\n"
119 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
120
Willy Tarreau844a7e72010-01-31 21:46:18 +0100121const char *HTTP_407_fmt =
122 "HTTP/1.0 407 Unauthorized\r\n"
123 "Cache-Control: no-cache\r\n"
124 "Connection: close\r\n"
125 "Content-Type: text/html\r\n"
126 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
127 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800128 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100129
Willy Tarreau0f772532006-12-23 20:51:41 +0100130
131const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200132 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100133 [HTTP_ERR_400] = 400,
134 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400135 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100136 [HTTP_ERR_408] = 408,
CJ Ess108b1dd2015-04-07 12:03:37 -0400137 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 [HTTP_ERR_500] = 500,
139 [HTTP_ERR_502] = 502,
140 [HTTP_ERR_503] = 503,
141 [HTTP_ERR_504] = 504,
142};
143
Willy Tarreau80587432006-12-24 17:47:20 +0100144static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200145 [HTTP_ERR_200] =
146 "HTTP/1.0 200 OK\r\n"
147 "Cache-Control: no-cache\r\n"
148 "Connection: close\r\n"
149 "Content-Type: text/html\r\n"
150 "\r\n"
151 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
152
Willy Tarreau0f772532006-12-23 20:51:41 +0100153 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100154 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100155 "Cache-Control: no-cache\r\n"
156 "Connection: close\r\n"
157 "Content-Type: text/html\r\n"
158 "\r\n"
159 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
160
161 [HTTP_ERR_403] =
162 "HTTP/1.0 403 Forbidden\r\n"
163 "Cache-Control: no-cache\r\n"
164 "Connection: close\r\n"
165 "Content-Type: text/html\r\n"
166 "\r\n"
167 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
168
CJ Ess108b1dd2015-04-07 12:03:37 -0400169 [HTTP_ERR_405] =
170 "HTTP/1.0 405 Method Not Allowed\r\n"
171 "Cache-Control: no-cache\r\n"
172 "Connection: close\r\n"
173 "Content-Type: text/html\r\n"
174 "\r\n"
175 "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n",
176
Willy Tarreau0f772532006-12-23 20:51:41 +0100177 [HTTP_ERR_408] =
178 "HTTP/1.0 408 Request Time-out\r\n"
179 "Cache-Control: no-cache\r\n"
180 "Connection: close\r\n"
181 "Content-Type: text/html\r\n"
182 "\r\n"
183 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
184
CJ Ess108b1dd2015-04-07 12:03:37 -0400185 [HTTP_ERR_429] =
186 "HTTP/1.0 429 Too Many Requests\r\n"
187 "Cache-Control: no-cache\r\n"
188 "Connection: close\r\n"
189 "Content-Type: text/html\r\n"
190 "\r\n"
191 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
192
Willy Tarreau0f772532006-12-23 20:51:41 +0100193 [HTTP_ERR_500] =
194 "HTTP/1.0 500 Server Error\r\n"
195 "Cache-Control: no-cache\r\n"
196 "Connection: close\r\n"
197 "Content-Type: text/html\r\n"
198 "\r\n"
199 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
200
201 [HTTP_ERR_502] =
202 "HTTP/1.0 502 Bad Gateway\r\n"
203 "Cache-Control: no-cache\r\n"
204 "Connection: close\r\n"
205 "Content-Type: text/html\r\n"
206 "\r\n"
207 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
208
209 [HTTP_ERR_503] =
210 "HTTP/1.0 503 Service Unavailable\r\n"
211 "Cache-Control: no-cache\r\n"
212 "Connection: close\r\n"
213 "Content-Type: text/html\r\n"
214 "\r\n"
215 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
216
217 [HTTP_ERR_504] =
218 "HTTP/1.0 504 Gateway Time-out\r\n"
219 "Cache-Control: no-cache\r\n"
220 "Connection: close\r\n"
221 "Content-Type: text/html\r\n"
222 "\r\n"
223 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
224
225};
226
Cyril Bonté19979e12012-04-04 12:57:21 +0200227/* status codes available for the stats admin page (strictly 4 chars length) */
228const char *stat_status_codes[STAT_STATUS_SIZE] = {
229 [STAT_STATUS_DENY] = "DENY",
230 [STAT_STATUS_DONE] = "DONE",
231 [STAT_STATUS_ERRP] = "ERRP",
232 [STAT_STATUS_EXCD] = "EXCD",
233 [STAT_STATUS_NONE] = "NONE",
234 [STAT_STATUS_PART] = "PART",
235 [STAT_STATUS_UNKN] = "UNKN",
236};
237
238
William Lallemand73025dd2014-04-24 14:38:37 +0200239/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200240struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200241 .list = LIST_HEAD_INIT(http_req_keywords.list)
242};
243
244/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200245struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200246 .list = LIST_HEAD_INIT(http_res_keywords.list)
247};
248
Willy Tarreau80587432006-12-24 17:47:20 +0100249/* We must put the messages here since GCC cannot initialize consts depending
250 * on strlen().
251 */
252struct chunk http_err_chunks[HTTP_ERR_SIZE];
253
Willy Tarreaua890d072013-04-02 12:01:06 +0200254/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
255static struct hdr_ctx static_hdr_ctx;
256
Willy Tarreau42250582007-04-01 01:30:43 +0200257#define FD_SETS_ARE_BITFIELDS
258#ifdef FD_SETS_ARE_BITFIELDS
259/*
260 * This map is used with all the FD_* macros to check whether a particular bit
261 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
262 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
263 * byte should be encoded. Be careful to always pass bytes from 0 to 255
264 * exclusively to the macros.
265 */
266fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
267fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100268fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200269
270#else
271#error "Check if your OS uses bitfields for fd_sets"
272#endif
273
Willy Tarreau87b09662015-04-03 00:22:06 +0200274static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200275
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200276/* This function returns a reason associated with the HTTP status.
277 * This function never fails, a message is always returned.
278 */
279const char *get_reason(unsigned int status)
280{
281 switch (status) {
282 case 100: return "Continue";
283 case 101: return "Switching Protocols";
284 case 102: return "Processing";
285 case 200: return "OK";
286 case 201: return "Created";
287 case 202: return "Accepted";
288 case 203: return "Non-Authoritative Information";
289 case 204: return "No Content";
290 case 205: return "Reset Content";
291 case 206: return "Partial Content";
292 case 207: return "Multi-Status";
293 case 210: return "Content Different";
294 case 226: return "IM Used";
295 case 300: return "Multiple Choices";
296 case 301: return "Moved Permanently";
297 case 302: return "Moved Temporarily";
298 case 303: return "See Other";
299 case 304: return "Not Modified";
300 case 305: return "Use Proxy";
301 case 307: return "Temporary Redirect";
302 case 308: return "Permanent Redirect";
303 case 310: return "Too many Redirects";
304 case 400: return "Bad Request";
305 case 401: return "Unauthorized";
306 case 402: return "Payment Required";
307 case 403: return "Forbidden";
308 case 404: return "Not Found";
309 case 405: return "Method Not Allowed";
310 case 406: return "Not Acceptable";
311 case 407: return "Proxy Authentication Required";
312 case 408: return "Request Time-out";
313 case 409: return "Conflict";
314 case 410: return "Gone";
315 case 411: return "Length Required";
316 case 412: return "Precondition Failed";
317 case 413: return "Request Entity Too Large";
318 case 414: return "Request-URI Too Long";
319 case 415: return "Unsupported Media Type";
320 case 416: return "Requested range unsatisfiable";
321 case 417: return "Expectation failed";
322 case 418: return "I'm a teapot";
323 case 422: return "Unprocessable entity";
324 case 423: return "Locked";
325 case 424: return "Method failure";
326 case 425: return "Unordered Collection";
327 case 426: return "Upgrade Required";
328 case 428: return "Precondition Required";
329 case 429: return "Too Many Requests";
330 case 431: return "Request Header Fields Too Large";
331 case 449: return "Retry With";
332 case 450: return "Blocked by Windows Parental Controls";
333 case 451: return "Unavailable For Legal Reasons";
334 case 456: return "Unrecoverable Error";
335 case 499: return "client has closed connection";
336 case 500: return "Internal Server Error";
337 case 501: return "Not Implemented";
338 case 502: return "Bad Gateway ou Proxy Error";
339 case 503: return "Service Unavailable";
340 case 504: return "Gateway Time-out";
341 case 505: return "HTTP Version not supported";
342 case 506: return "Variant also negociate";
343 case 507: return "Insufficient storage";
344 case 508: return "Loop detected";
345 case 509: return "Bandwidth Limit Exceeded";
346 case 510: return "Not extended";
347 case 511: return "Network authentication required";
348 case 520: return "Web server is returning an unknown error";
349 default:
350 switch (status) {
351 case 100 ... 199: return "Informational";
352 case 200 ... 299: return "Success";
353 case 300 ... 399: return "Redirection";
354 case 400 ... 499: return "Client Error";
355 case 500 ... 599: return "Server Error";
356 default: return "Other";
357 }
358 }
359}
360
Willy Tarreau80587432006-12-24 17:47:20 +0100361void init_proto_http()
362{
Willy Tarreau42250582007-04-01 01:30:43 +0200363 int i;
364 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100365 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200366
Willy Tarreau80587432006-12-24 17:47:20 +0100367 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
368 if (!http_err_msgs[msg]) {
369 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
370 abort();
371 }
372
373 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
374 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
375 }
Willy Tarreau42250582007-04-01 01:30:43 +0200376
377 /* initialize the log header encoding map : '{|}"#' should be encoded with
378 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
379 * URL encoding only requires '"', '#' to be encoded as well as non-
380 * printable characters above.
381 */
382 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
383 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100384 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200385 for (i = 0; i < 32; i++) {
386 FD_SET(i, hdr_encode_map);
387 FD_SET(i, url_encode_map);
388 }
389 for (i = 127; i < 256; i++) {
390 FD_SET(i, hdr_encode_map);
391 FD_SET(i, url_encode_map);
392 }
393
394 tmp = "\"#{|}";
395 while (*tmp) {
396 FD_SET(*tmp, hdr_encode_map);
397 tmp++;
398 }
399
400 tmp = "\"#";
401 while (*tmp) {
402 FD_SET(*tmp, url_encode_map);
403 tmp++;
404 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200405
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100406 /* initialize the http header encoding map. The draft httpbis define the
407 * header content as:
408 *
409 * HTTP-message = start-line
410 * *( header-field CRLF )
411 * CRLF
412 * [ message-body ]
413 * header-field = field-name ":" OWS field-value OWS
414 * field-value = *( field-content / obs-fold )
415 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
416 * obs-fold = CRLF 1*( SP / HTAB )
417 * field-vchar = VCHAR / obs-text
418 * VCHAR = %x21-7E
419 * obs-text = %x80-FF
420 *
421 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
422 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
423 * "obs-fold" is volontary forgotten because haproxy remove this.
424 */
425 memset(http_encode_map, 0, sizeof(http_encode_map));
426 for (i = 0x00; i <= 0x08; i++)
427 FD_SET(i, http_encode_map);
428 for (i = 0x0a; i <= 0x1f; i++)
429 FD_SET(i, http_encode_map);
430 FD_SET(0x7f, http_encode_map);
431
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200432 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200433 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200434 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100435 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100436}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200437
Willy Tarreau53b6c742006-12-17 13:37:46 +0100438/*
439 * We have 26 list of methods (1 per first letter), each of which can have
440 * up to 3 entries (2 valid, 1 null).
441 */
442struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100443 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100444 int len;
445 const char text[8];
446};
447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100448const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100449 ['C' - 'A'] = {
450 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
451 },
452 ['D' - 'A'] = {
453 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
454 },
455 ['G' - 'A'] = {
456 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
457 },
458 ['H' - 'A'] = {
459 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
460 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200461 ['O' - 'A'] = {
462 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
463 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100464 ['P' - 'A'] = {
465 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
466 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
467 },
468 ['T' - 'A'] = {
469 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
470 },
471 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200472 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100473 */
474};
475
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100476const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100477 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
478 [HTTP_METH_GET] = { "GET", 3 },
479 [HTTP_METH_HEAD] = { "HEAD", 4 },
480 [HTTP_METH_POST] = { "POST", 4 },
481 [HTTP_METH_PUT] = { "PUT", 3 },
482 [HTTP_METH_DELETE] = { "DELETE", 6 },
483 [HTTP_METH_TRACE] = { "TRACE", 5 },
484 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
485};
486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100487/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200488 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100489 * RFC2616 for those chars.
490 */
491
492const char http_is_spht[256] = {
493 [' '] = 1, ['\t'] = 1,
494};
495
496const char http_is_crlf[256] = {
497 ['\r'] = 1, ['\n'] = 1,
498};
499
500const char http_is_lws[256] = {
501 [' '] = 1, ['\t'] = 1,
502 ['\r'] = 1, ['\n'] = 1,
503};
504
505const char http_is_sep[256] = {
506 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
507 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
508 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
509 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
510 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
511};
512
513const char http_is_ctl[256] = {
514 [0 ... 31] = 1,
515 [127] = 1,
516};
517
518/*
519 * A token is any ASCII char that is neither a separator nor a CTL char.
520 * Do not overwrite values in assignment since gcc-2.95 will not handle
521 * them correctly. Instead, define every non-CTL char's status.
522 */
523const char http_is_token[256] = {
524 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
525 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
526 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
527 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
528 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
529 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
530 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
531 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
532 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
533 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
534 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
535 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
536 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
537 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
538 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
539 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
540 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
541 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
542 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
543 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
544 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
545 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
546 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
547 ['|'] = 1, ['}'] = 0, ['~'] = 1,
548};
549
550
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100551/*
552 * An http ver_token is any ASCII which can be found in an HTTP version,
553 * which includes 'H', 'T', 'P', '/', '.' and any digit.
554 */
555const char http_is_ver_token[256] = {
556 ['.'] = 1, ['/'] = 1,
557 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
558 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
Thierry FOURNIER63d692c2015-02-28 19:03:56 +0100559 ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1,
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100560};
561
562
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100563/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100564 * Adds a header and its CRLF at the tail of the message's buffer, just before
565 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100566 * The header is also automatically added to the index <hdr_idx>, and the end
567 * of headers is automatically adjusted. The number of bytes added is returned
568 * on success, otherwise <0 is returned indicating an error.
569 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100570int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100571{
572 int bytes, len;
573
574 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200575 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100576 if (!bytes)
577 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100578 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100579 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
580}
581
582/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100583 * Adds a header and its CRLF at the tail of the message's buffer, just before
584 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100585 * the buffer is only opened and the space reserved, but nothing is copied.
586 * The header is also automatically added to the index <hdr_idx>, and the end
587 * of headers is automatically adjusted. The number of bytes added is returned
588 * on success, otherwise <0 is returned indicating an error.
589 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100590int http_header_add_tail2(struct http_msg *msg,
591 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100592{
593 int bytes;
594
Willy Tarreau9b28e032012-10-12 23:49:43 +0200595 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100596 if (!bytes)
597 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100598 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100599 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
600}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200601
602/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100603 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
604 * If so, returns the position of the first non-space character relative to
605 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
606 * to return a pointer to the place after the first space. Returns 0 if the
607 * header name does not match. Checks are case-insensitive.
608 */
609int http_header_match2(const char *hdr, const char *end,
610 const char *name, int len)
611{
612 const char *val;
613
614 if (hdr + len >= end)
615 return 0;
616 if (hdr[len] != ':')
617 return 0;
618 if (strncasecmp(hdr, name, len) != 0)
619 return 0;
620 val = hdr + len + 1;
621 while (val < end && HTTP_IS_SPHT(*val))
622 val++;
623 if ((val >= end) && (len + 2 <= end - hdr))
624 return len + 2; /* we may replace starting from second space */
625 return val - hdr;
626}
627
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200628/* Find the first or next occurrence of header <name> in message buffer <sol>
629 * using headers index <idx>, and return it in the <ctx> structure. This
630 * structure holds everything necessary to use the header and find next
631 * occurrence. If its <idx> member is 0, the header is searched from the
632 * beginning. Otherwise, the next occurrence is returned. The function returns
633 * 1 when it finds a value, and 0 when there is no more. It is very similar to
634 * http_find_header2() except that it is designed to work with full-line headers
635 * whose comma is not a delimiter but is part of the syntax. As a special case,
636 * if ctx->val is NULL when searching for a new values of a header, the current
637 * header is rescanned. This allows rescanning after a header deletion.
638 */
639int http_find_full_header2(const char *name, int len,
640 char *sol, struct hdr_idx *idx,
641 struct hdr_ctx *ctx)
642{
643 char *eol, *sov;
644 int cur_idx, old_idx;
645
646 cur_idx = ctx->idx;
647 if (cur_idx) {
648 /* We have previously returned a header, let's search another one */
649 sol = ctx->line;
650 eol = sol + idx->v[cur_idx].len;
651 goto next_hdr;
652 }
653
654 /* first request for this header */
655 sol += hdr_idx_first_pos(idx);
656 old_idx = 0;
657 cur_idx = hdr_idx_first_idx(idx);
658 while (cur_idx) {
659 eol = sol + idx->v[cur_idx].len;
660
661 if (len == 0) {
662 /* No argument was passed, we want any header.
663 * To achieve this, we simply build a fake request. */
664 while (sol + len < eol && sol[len] != ':')
665 len++;
666 name = sol;
667 }
668
669 if ((len < eol - sol) &&
670 (sol[len] == ':') &&
671 (strncasecmp(sol, name, len) == 0)) {
672 ctx->del = len;
673 sov = sol + len + 1;
674 while (sov < eol && http_is_lws[(unsigned char)*sov])
675 sov++;
676
677 ctx->line = sol;
678 ctx->prev = old_idx;
679 ctx->idx = cur_idx;
680 ctx->val = sov - sol;
681 ctx->tws = 0;
682 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
683 eol--;
684 ctx->tws++;
685 }
686 ctx->vlen = eol - sov;
687 return 1;
688 }
689 next_hdr:
690 sol = eol + idx->v[cur_idx].cr + 1;
691 old_idx = cur_idx;
692 cur_idx = idx->v[cur_idx].next;
693 }
694 return 0;
695}
696
Willy Tarreauc90dc232015-02-20 13:51:36 +0100697/* Find the first or next header field in message buffer <sol> using headers
698 * index <idx>, and return it in the <ctx> structure. This structure holds
699 * everything necessary to use the header and find next occurrence. If its
700 * <idx> member is 0, the first header is retrieved. Otherwise, the next
701 * occurrence is returned. The function returns 1 when it finds a value, and
702 * 0 when there is no more. It is equivalent to http_find_full_header2() with
703 * no header name.
704 */
705int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
706{
707 char *eol, *sov;
708 int cur_idx, old_idx;
709 int len;
710
711 cur_idx = ctx->idx;
712 if (cur_idx) {
713 /* We have previously returned a header, let's search another one */
714 sol = ctx->line;
715 eol = sol + idx->v[cur_idx].len;
716 goto next_hdr;
717 }
718
719 /* first request for this header */
720 sol += hdr_idx_first_pos(idx);
721 old_idx = 0;
722 cur_idx = hdr_idx_first_idx(idx);
723 while (cur_idx) {
724 eol = sol + idx->v[cur_idx].len;
725
726 len = 0;
727 while (1) {
728 if (len >= eol - sol)
729 goto next_hdr;
730 if (sol[len] == ':')
731 break;
732 len++;
733 }
734
735 ctx->del = len;
736 sov = sol + len + 1;
737 while (sov < eol && http_is_lws[(unsigned char)*sov])
738 sov++;
739
740 ctx->line = sol;
741 ctx->prev = old_idx;
742 ctx->idx = cur_idx;
743 ctx->val = sov - sol;
744 ctx->tws = 0;
745
746 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
747 eol--;
748 ctx->tws++;
749 }
750 ctx->vlen = eol - sov;
751 return 1;
752
753 next_hdr:
754 sol = eol + idx->v[cur_idx].cr + 1;
755 old_idx = cur_idx;
756 cur_idx = idx->v[cur_idx].next;
757 }
758 return 0;
759}
760
Willy Tarreau68085d82010-01-18 14:54:04 +0100761/* Find the end of the header value contained between <s> and <e>. See RFC2616,
762 * par 2.2 for more information. Note that it requires a valid header to return
763 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200764 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100765char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200766{
767 int quoted, qdpair;
768
769 quoted = qdpair = 0;
770 for (; s < e; s++) {
771 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200772 else if (quoted) {
773 if (*s == '\\') qdpair = 1;
774 else if (*s == '"') quoted = 0;
775 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200776 else if (*s == '"') quoted = 1;
777 else if (*s == ',') return s;
778 }
779 return s;
780}
781
782/* Find the first or next occurrence of header <name> in message buffer <sol>
783 * using headers index <idx>, and return it in the <ctx> structure. This
784 * structure holds everything necessary to use the header and find next
785 * occurrence. If its <idx> member is 0, the header is searched from the
786 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100787 * 1 when it finds a value, and 0 when there is no more. It is designed to work
788 * with headers defined as comma-separated lists. As a special case, if ctx->val
789 * is NULL when searching for a new values of a header, the current header is
790 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200791 */
792int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100793 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200794 struct hdr_ctx *ctx)
795{
Willy Tarreau68085d82010-01-18 14:54:04 +0100796 char *eol, *sov;
797 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200798
Willy Tarreau68085d82010-01-18 14:54:04 +0100799 cur_idx = ctx->idx;
800 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200801 /* We have previously returned a value, let's search
802 * another one on the same line.
803 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200804 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200805 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100806 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200807 eol = sol + idx->v[cur_idx].len;
808
809 if (sov >= eol)
810 /* no more values in this header */
811 goto next_hdr;
812
Willy Tarreau68085d82010-01-18 14:54:04 +0100813 /* values remaining for this header, skip the comma but save it
814 * for later use (eg: for header deletion).
815 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200816 sov++;
817 while (sov < eol && http_is_lws[(unsigned char)*sov])
818 sov++;
819
820 goto return_hdr;
821 }
822
823 /* first request for this header */
824 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100825 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200826 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200827 while (cur_idx) {
828 eol = sol + idx->v[cur_idx].len;
829
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200830 if (len == 0) {
831 /* No argument was passed, we want any header.
832 * To achieve this, we simply build a fake request. */
833 while (sol + len < eol && sol[len] != ':')
834 len++;
835 name = sol;
836 }
837
Willy Tarreau33a7e692007-06-10 19:45:56 +0200838 if ((len < eol - sol) &&
839 (sol[len] == ':') &&
840 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100841 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200842 sov = sol + len + 1;
843 while (sov < eol && http_is_lws[(unsigned char)*sov])
844 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100845
Willy Tarreau33a7e692007-06-10 19:45:56 +0200846 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100847 ctx->prev = old_idx;
848 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200849 ctx->idx = cur_idx;
850 ctx->val = sov - sol;
851
852 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200853 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200854 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200855 eol--;
856 ctx->tws++;
857 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200858 ctx->vlen = eol - sov;
859 return 1;
860 }
861 next_hdr:
862 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100863 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200864 cur_idx = idx->v[cur_idx].next;
865 }
866 return 0;
867}
868
869int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100870 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200871 struct hdr_ctx *ctx)
872{
873 return http_find_header2(name, strlen(name), sol, idx, ctx);
874}
875
Willy Tarreau68085d82010-01-18 14:54:04 +0100876/* Remove one value of a header. This only works on a <ctx> returned by one of
877 * the http_find_header functions. The value is removed, as well as surrounding
878 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100879 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100880 * message <msg>. The new index is returned. If it is zero, it means there is
881 * no more header, so any processing may stop. The ctx is always left in a form
882 * that can be handled by http_find_header2() to find next occurrence.
883 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100884int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100885{
886 int cur_idx = ctx->idx;
887 char *sol = ctx->line;
888 struct hdr_idx_elem *hdr;
889 int delta, skip_comma;
890
891 if (!cur_idx)
892 return 0;
893
894 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200895 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100896 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200897 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100898 http_msg_move_end(msg, delta);
899 idx->used--;
900 hdr->len = 0; /* unused entry */
901 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100902 if (idx->tail == ctx->idx)
903 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100904 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100905 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100906 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200907 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100908 return ctx->idx;
909 }
910
911 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200912 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
913 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100914 */
915
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200916 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200917 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200918 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100919 NULL, 0);
920 hdr->len += delta;
921 http_msg_move_end(msg, delta);
922 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200923 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100924 return ctx->idx;
925}
926
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100927/* This function handles a server error at the stream interface level. The
928 * stream interface is assumed to be already in a closed state. An optional
929 * message is copied into the input buffer, and an HTTP status code stored.
930 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100931 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200933static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100934 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200935{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100936 channel_auto_read(si_oc(si));
937 channel_abort(si_oc(si));
938 channel_auto_close(si_oc(si));
939 channel_erase(si_oc(si));
940 channel_auto_close(si_ic(si));
941 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100942 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200943 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100944 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200945 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200946 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200947 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200948 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200949 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200950}
951
Willy Tarreau87b09662015-04-03 00:22:06 +0200952/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100953 * and message.
954 */
955
Willy Tarreau87b09662015-04-03 00:22:06 +0200956struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100957{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200958 if (s->be->errmsg[msgnum].str)
959 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200960 else if (strm_fe(s)->errmsg[msgnum].str)
961 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100962 else
963 return &http_err_chunks[msgnum];
964}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200965
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100966void
967http_reply_and_close(struct stream *s, short status, struct chunk *msg)
968{
969 stream_int_retnclose(&s->si[0], msg);
970}
971
Willy Tarreau53b6c742006-12-17 13:37:46 +0100972/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200973 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
974 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100975 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100976enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100977{
978 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100979 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100980
981 m = ((unsigned)*str - 'A');
982
983 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100984 for (h = http_methods[m]; h->len > 0; h++) {
985 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100986 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100987 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100988 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100989 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100990 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200991 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100992}
993
Willy Tarreau21d2af32008-02-14 20:25:24 +0100994/* Parse the URI from the given transaction (which is assumed to be in request
995 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
996 * It is returned otherwise.
997 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +0200998char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100999{
1000 char *ptr, *end;
1001
Willy Tarreau9b28e032012-10-12 23:49:43 +02001002 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +01001003 end = ptr + txn->req.sl.rq.u_l;
1004
1005 if (ptr >= end)
1006 return NULL;
1007
1008 /* RFC2616, par. 5.1.2 :
1009 * Request-URI = "*" | absuri | abspath | authority
1010 */
1011
1012 if (*ptr == '*')
1013 return NULL;
1014
1015 if (isalpha((unsigned char)*ptr)) {
1016 /* this is a scheme as described by RFC3986, par. 3.1 */
1017 ptr++;
1018 while (ptr < end &&
1019 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1020 ptr++;
1021 /* skip '://' */
1022 if (ptr == end || *ptr++ != ':')
1023 return NULL;
1024 if (ptr == end || *ptr++ != '/')
1025 return NULL;
1026 if (ptr == end || *ptr++ != '/')
1027 return NULL;
1028 }
1029 /* skip [user[:passwd]@]host[:[port]] */
1030
1031 while (ptr < end && *ptr != '/')
1032 ptr++;
1033
1034 if (ptr == end)
1035 return NULL;
1036
1037 /* OK, we got the '/' ! */
1038 return ptr;
1039}
1040
William Lallemand65ad6e12014-01-31 15:08:02 +01001041/* Parse the URI from the given string and look for the "/" beginning the PATH.
1042 * If not found, return NULL. It is returned otherwise.
1043 */
1044static char *
1045http_get_path_from_string(char *str)
1046{
1047 char *ptr = str;
1048
1049 /* RFC2616, par. 5.1.2 :
1050 * Request-URI = "*" | absuri | abspath | authority
1051 */
1052
1053 if (*ptr == '*')
1054 return NULL;
1055
1056 if (isalpha((unsigned char)*ptr)) {
1057 /* this is a scheme as described by RFC3986, par. 3.1 */
1058 ptr++;
1059 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1060 ptr++;
1061 /* skip '://' */
1062 if (*ptr == '\0' || *ptr++ != ':')
1063 return NULL;
1064 if (*ptr == '\0' || *ptr++ != '/')
1065 return NULL;
1066 if (*ptr == '\0' || *ptr++ != '/')
1067 return NULL;
1068 }
1069 /* skip [user[:passwd]@]host[:[port]] */
1070
1071 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1072 ptr++;
1073
1074 if (*ptr == '\0' || *ptr == ' ')
1075 return NULL;
1076
1077 /* OK, we got the '/' ! */
1078 return ptr;
1079}
1080
Willy Tarreau71241ab2012-12-27 11:30:54 +01001081/* Returns a 302 for a redirectable request that reaches a server working in
1082 * in redirect mode. This may only be called just after the stream interface
1083 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1084 * follow normal proxy processing. NOTE: this function is designed to support
1085 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001086 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001087void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001088{
1089 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001090 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001091 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001092 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001093
1094 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001095 trash.len = strlen(HTTP_302);
1096 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001097
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001098 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001099
Willy Tarreauefb453c2008-10-26 20:49:47 +01001100 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001101 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001102 return;
1103
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001104 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001105 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001106 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1107 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001108 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001109
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001110 /* 3: add the request URI. Since it was already forwarded, we need
1111 * to temporarily rewind the buffer.
1112 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001113 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001114 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001115
Willy Tarreauefb453c2008-10-26 20:49:47 +01001116 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001117 len = buffer_count(s->req.buf, path, b_ptr(s->req.buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001118
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001119 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001120
Willy Tarreauefb453c2008-10-26 20:49:47 +01001121 if (!path)
1122 return;
1123
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001124 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001125 return;
1126
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001127 memcpy(trash.str + trash.len, path, len);
1128 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001129
1130 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001131 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1132 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001133 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001134 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1135 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001136 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001137
1138 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001139 si_shutr(si);
1140 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001141 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001142 si->state = SI_ST_CLO;
1143
1144 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001145 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001146
1147 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001148 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001149 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001150}
1151
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001152/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001153 * that the server side is closed. Note that err_type is actually a
1154 * bitmask, where almost only aborts may be cumulated with other
1155 * values. We consider that aborted operations are more important
1156 * than timeouts or errors due to the fact that nobody else in the
1157 * logs might explain incomplete retries. All others should avoid
1158 * being cumulated. It should normally not be possible to have multiple
1159 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001160 * Note that connection errors appearing on the second request of a keep-alive
1161 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001162 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001163void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001164{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001165 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001166
1167 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001168 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001169 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001170 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001171 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001172 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001173 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001174 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001175 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001176 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001177 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001178 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001179 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001180 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001181 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001182 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001183 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001184 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001185 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1186 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001187 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001188 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001189 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001190 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001191 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001192 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001193 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001194 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001195}
1196
Willy Tarreau42250582007-04-01 01:30:43 +02001197extern const char sess_term_cond[8];
1198extern const char sess_fin_state[8];
1199extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001200struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001201struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001202struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001203struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001204
Willy Tarreau117f59e2007-03-04 18:17:17 +01001205/*
1206 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001207 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001208 */
1209void capture_headers(char *som, struct hdr_idx *idx,
1210 char **cap, struct cap_hdr *cap_hdr)
1211{
1212 char *eol, *sol, *col, *sov;
1213 int cur_idx;
1214 struct cap_hdr *h;
1215 int len;
1216
1217 sol = som + hdr_idx_first_pos(idx);
1218 cur_idx = hdr_idx_first_idx(idx);
1219
1220 while (cur_idx) {
1221 eol = sol + idx->v[cur_idx].len;
1222
1223 col = sol;
1224 while (col < eol && *col != ':')
1225 col++;
1226
1227 sov = col + 1;
1228 while (sov < eol && http_is_lws[(unsigned char)*sov])
1229 sov++;
1230
1231 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001232 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001233 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1234 if (cap[h->index] == NULL)
1235 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001236 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001237
1238 if (cap[h->index] == NULL) {
1239 Alert("HTTP capture : out of memory.\n");
1240 continue;
1241 }
1242
1243 len = eol - sov;
1244 if (len > h->len)
1245 len = h->len;
1246
1247 memcpy(cap[h->index], sov, len);
1248 cap[h->index][len]=0;
1249 }
1250 }
1251 sol = eol + idx->v[cur_idx].cr + 1;
1252 cur_idx = idx->v[cur_idx].next;
1253 }
1254}
1255
1256
Willy Tarreau42250582007-04-01 01:30:43 +02001257/* either we find an LF at <ptr> or we jump to <bad>.
1258 */
1259#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1260
1261/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1262 * otherwise to <http_msg_ood> with <state> set to <st>.
1263 */
1264#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1265 ptr++; \
1266 if (likely(ptr < end)) \
1267 goto good; \
1268 else { \
1269 state = (st); \
1270 goto http_msg_ood; \
1271 } \
1272 } while (0)
1273
1274
Willy Tarreaubaaee002006-06-26 02:48:02 +02001275/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001276 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001277 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1278 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1279 * will give undefined results.
1280 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1281 * and that msg->sol points to the beginning of the response.
1282 * If a complete line is found (which implies that at least one CR or LF is
1283 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1284 * returned indicating an incomplete line (which does not mean that parts have
1285 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1286 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1287 * upon next call.
1288 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001289 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001290 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1291 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001292 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001293 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001294const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001295 enum ht_state state, const char *ptr, const char *end,
1296 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001297{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001298 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001299
Willy Tarreau8973c702007-01-21 23:58:29 +01001300 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001301 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001302 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001303 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001304 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1305
1306 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001307 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001308 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1309 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001310 state = HTTP_MSG_ERROR;
1311 break;
1312
Willy Tarreau8973c702007-01-21 23:58:29 +01001313 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001314 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001315 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001316 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001317 goto http_msg_rpcode;
1318 }
1319 if (likely(HTTP_IS_SPHT(*ptr)))
1320 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1321 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001322 state = HTTP_MSG_ERROR;
1323 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001324
Willy Tarreau8973c702007-01-21 23:58:29 +01001325 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001326 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001327 if (likely(!HTTP_IS_LWS(*ptr)))
1328 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1329
1330 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001331 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001332 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1333 }
1334
1335 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001336 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001337 http_msg_rsp_reason:
1338 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001339 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001340 msg->sl.st.r_l = 0;
1341 goto http_msg_rpline_eol;
1342
Willy Tarreau8973c702007-01-21 23:58:29 +01001343 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001344 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001345 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001346 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001347 goto http_msg_rpreason;
1348 }
1349 if (likely(HTTP_IS_SPHT(*ptr)))
1350 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1351 /* so it's a CR/LF, so there is no reason phrase */
1352 goto http_msg_rsp_reason;
1353
Willy Tarreau8973c702007-01-21 23:58:29 +01001354 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001355 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001356 if (likely(!HTTP_IS_CRLF(*ptr)))
1357 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001358 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001359 http_msg_rpline_eol:
1360 /* We have seen the end of line. Note that we do not
1361 * necessarily have the \n yet, but at least we know that we
1362 * have EITHER \r OR \n, otherwise the response would not be
1363 * complete. We can then record the response length and return
1364 * to the caller which will be able to register it.
1365 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001366 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001367 return ptr;
1368
Willy Tarreau8973c702007-01-21 23:58:29 +01001369 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001370#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001371 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1372 exit(1);
1373#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001374 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001375 }
1376
1377 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001378 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001379 if (ret_state)
1380 *ret_state = state;
1381 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001382 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001383 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001384}
1385
Willy Tarreau8973c702007-01-21 23:58:29 +01001386/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 * This function parses a request line between <ptr> and <end>, starting with
1388 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1389 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1390 * will give undefined results.
1391 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1392 * and that msg->sol points to the beginning of the request.
1393 * If a complete line is found (which implies that at least one CR or LF is
1394 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1395 * returned indicating an incomplete line (which does not mean that parts have
1396 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1397 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1398 * upon next call.
1399 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001400 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1402 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001403 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001404 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001405const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001406 enum ht_state state, const char *ptr, const char *end,
1407 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001408{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001409 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001410
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001413 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 if (likely(HTTP_IS_TOKEN(*ptr)))
1415 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001416
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001418 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1420 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001421
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 if (likely(HTTP_IS_CRLF(*ptr))) {
1423 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001424 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001426 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001428 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001430 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 msg->sl.rq.v_l = 0;
1432 goto http_msg_rqline_eol;
1433 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001434 state = HTTP_MSG_ERROR;
1435 break;
1436
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001438 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001440 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 goto http_msg_rquri;
1442 }
1443 if (likely(HTTP_IS_SPHT(*ptr)))
1444 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1445 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1446 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001449 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001450 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001452
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001454 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1456 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001457
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001458 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001459 /* non-ASCII chars are forbidden unless option
1460 * accept-invalid-http-request is enabled in the frontend.
1461 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001462 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001463 if (msg->err_pos < -1)
1464 goto invalid_char;
1465 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001466 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001467 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1468 }
1469
1470 if (likely(HTTP_IS_CRLF(*ptr))) {
1471 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1472 goto http_msg_req09_uri_e;
1473 }
1474
1475 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001476 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001477 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001478 state = HTTP_MSG_ERROR;
1479 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001482 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001484 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001485 goto http_msg_rqver;
1486 }
1487 if (likely(HTTP_IS_SPHT(*ptr)))
1488 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1489 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1490 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001491
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001493 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001494 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001495 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001496
1497 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001498 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001499 http_msg_rqline_eol:
1500 /* We have seen the end of line. Note that we do not
1501 * necessarily have the \n yet, but at least we know that we
1502 * have EITHER \r OR \n, otherwise the request would not be
1503 * complete. We can then record the request length and return
1504 * to the caller which will be able to register it.
1505 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001506 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001507 return ptr;
1508 }
1509
1510 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001511 state = HTTP_MSG_ERROR;
1512 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001514 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001515#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1517 exit(1);
1518#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001519 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001521
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001523 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 if (ret_state)
1525 *ret_state = state;
1526 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001527 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001530
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001531/*
1532 * Returns the data from Authorization header. Function may be called more
1533 * than once so data is stored in txn->auth_data. When no header is found
1534 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001535 * searching again for something we are unable to find anyway. However, if
1536 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001537 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001538 */
1539
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001540/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1541 * set according to global.tune.bufsize.
1542 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001543char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001544
1545int
Willy Tarreau87b09662015-04-03 00:22:06 +02001546get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001547{
1548
Willy Tarreaueee5b512015-04-03 23:46:31 +02001549 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001550 struct chunk auth_method;
1551 struct hdr_ctx ctx;
1552 char *h, *p;
1553 int len;
1554
1555#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001556 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001557#endif
1558
1559 if (txn->auth.method == HTTP_AUTH_WRONG)
1560 return 0;
1561
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001562 txn->auth.method = HTTP_AUTH_WRONG;
1563
1564 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001565
1566 if (txn->flags & TX_USE_PX_CONN) {
1567 h = "Proxy-Authorization";
1568 len = strlen(h);
1569 } else {
1570 h = "Authorization";
1571 len = strlen(h);
1572 }
1573
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001574 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001575 return 0;
1576
1577 h = ctx.line + ctx.val;
1578
1579 p = memchr(h, ' ', ctx.vlen);
1580 if (!p || p == h)
1581 return 0;
1582
1583 chunk_initlen(&auth_method, h, 0, p-h);
1584 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1585
1586 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1587
1588 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001589 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001590
1591 if (len < 0)
1592 return 0;
1593
1594
1595 get_http_auth_buff[len] = '\0';
1596
1597 p = strchr(get_http_auth_buff, ':');
1598
1599 if (!p)
1600 return 0;
1601
1602 txn->auth.user = get_http_auth_buff;
1603 *p = '\0';
1604 txn->auth.pass = p+1;
1605
1606 txn->auth.method = HTTP_AUTH_BASIC;
1607 return 1;
1608 }
1609
1610 return 0;
1611}
1612
Willy Tarreau58f10d72006-12-04 02:26:12 +01001613
Willy Tarreau8973c702007-01-21 23:58:29 +01001614/*
1615 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001616 * depending on the initial msg->msg_state. The caller is responsible for
1617 * ensuring that the message does not wrap. The function can be preempted
1618 * everywhere when data are missing and recalled at the exact same location
1619 * with no information loss. The message may even be realigned between two
1620 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001621 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001622 * fields. Note that msg->sol will be initialized after completing the first
1623 * state, so that none of the msg pointers has to be initialized prior to the
1624 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001625 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001626void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001627{
Willy Tarreau3770f232013-12-07 00:01:53 +01001628 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001630 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001631
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001632 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001633 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001634 ptr = buf->p + msg->next;
1635 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001636
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637 if (unlikely(ptr >= end))
1638 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001641 /*
1642 * First, states that are specific to the response only.
1643 * We check them first so that request and headers are
1644 * closer to each other (accessed more often).
1645 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001646 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001647 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001648 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001649 /* we have a start of message, but we have to check
1650 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001651 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001652 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001653 if (unlikely(ptr != buf->p)) {
1654 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001655 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001656 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001657 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001658 }
Willy Tarreau26927362012-05-18 23:22:52 +02001659 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001660 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001661 hdr_idx_init(idx);
1662 state = HTTP_MSG_RPVER;
1663 goto http_msg_rpver;
1664 }
1665
1666 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1667 goto http_msg_invalid;
1668
1669 if (unlikely(*ptr == '\n'))
1670 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1671 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1672 /* stop here */
1673
Willy Tarreau8973c702007-01-21 23:58:29 +01001674 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001675 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001676 EXPECT_LF_HERE(ptr, http_msg_invalid);
1677 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1678 /* stop here */
1679
Willy Tarreau8973c702007-01-21 23:58:29 +01001680 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001681 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001682 case HTTP_MSG_RPVER_SP:
1683 case HTTP_MSG_RPCODE:
1684 case HTTP_MSG_RPCODE_SP:
1685 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001686 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001687 state, ptr, end,
1688 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001689 if (unlikely(!ptr))
1690 return;
1691
1692 /* we have a full response and we know that we have either a CR
1693 * or an LF at <ptr>.
1694 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001695 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1696
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001697 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001698 if (likely(*ptr == '\r'))
1699 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1700 goto http_msg_rpline_end;
1701
Willy Tarreau8973c702007-01-21 23:58:29 +01001702 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001703 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001704 /* msg->sol must point to the first of CR or LF. */
1705 EXPECT_LF_HERE(ptr, http_msg_invalid);
1706 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1707 /* stop here */
1708
1709 /*
1710 * Second, states that are specific to the request only
1711 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001712 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001713 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001714 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001715 /* we have a start of message, but we have to check
1716 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001717 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001718 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001719 if (likely(ptr != buf->p)) {
1720 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001721 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001722 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001723 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 }
Willy Tarreau26927362012-05-18 23:22:52 +02001725 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001726 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001727 state = HTTP_MSG_RQMETH;
1728 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001730
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001731 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1732 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001733
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 if (unlikely(*ptr == '\n'))
1735 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1736 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001737 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001738
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001740 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001741 EXPECT_LF_HERE(ptr, http_msg_invalid);
1742 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001743 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001744
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001745 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001746 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001747 case HTTP_MSG_RQMETH_SP:
1748 case HTTP_MSG_RQURI:
1749 case HTTP_MSG_RQURI_SP:
1750 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001751 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001752 state, ptr, end,
1753 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 if (unlikely(!ptr))
1755 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001756
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 /* we have a full request and we know that we have either a CR
1758 * or an LF at <ptr>.
1759 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001760 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001761
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001762 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001763 if (likely(*ptr == '\r'))
1764 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001765 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001766
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001767 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001768 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001769 /* check for HTTP/0.9 request : no version information available.
1770 * msg->sol must point to the first of CR or LF.
1771 */
1772 if (unlikely(msg->sl.rq.v_l == 0))
1773 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001774
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001775 EXPECT_LF_HERE(ptr, http_msg_invalid);
1776 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001777 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001778
Willy Tarreau8973c702007-01-21 23:58:29 +01001779 /*
1780 * Common states below
1781 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001782 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001783 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001784 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001785 if (likely(!HTTP_IS_CRLF(*ptr))) {
1786 goto http_msg_hdr_name;
1787 }
1788
1789 if (likely(*ptr == '\r'))
1790 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1791 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001792
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001793 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001794 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001795 /* assumes msg->sol points to the first char */
1796 if (likely(HTTP_IS_TOKEN(*ptr)))
1797 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001798
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001799 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001800 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001801
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001802 if (likely(msg->err_pos < -1) || *ptr == '\n')
1803 goto http_msg_invalid;
1804
1805 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001806 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001807
1808 /* and we still accept this non-token character */
1809 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001810
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001811 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001812 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001813 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001814 if (likely(HTTP_IS_SPHT(*ptr)))
1815 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001816
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001818 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001819
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001820 if (likely(!HTTP_IS_CRLF(*ptr))) {
1821 goto http_msg_hdr_val;
1822 }
1823
1824 if (likely(*ptr == '\r'))
1825 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1826 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001827
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001828 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001829 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001830 EXPECT_LF_HERE(ptr, http_msg_invalid);
1831 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001832
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001833 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001834 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001835 if (likely(HTTP_IS_SPHT(*ptr))) {
1836 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001837 for (; buf->p + msg->sov < ptr; msg->sov++)
1838 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001839 goto http_msg_hdr_l1_sp;
1840 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001841 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001842 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001843 goto http_msg_complete_header;
1844
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001845 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001846 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001847 /* assumes msg->sol points to the first char, and msg->sov
1848 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001849 */
1850 if (likely(!HTTP_IS_CRLF(*ptr)))
1851 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001852
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001853 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001854 /* Note: we could also copy eol into ->eoh so that we have the
1855 * real header end in case it ends with lots of LWS, but is this
1856 * really needed ?
1857 */
1858 if (likely(*ptr == '\r'))
1859 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1860 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001861
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001862 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001863 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001864 EXPECT_LF_HERE(ptr, http_msg_invalid);
1865 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001866
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001868 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001869 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1870 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001871 for (; buf->p + msg->eol < ptr; msg->eol++)
1872 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001873 goto http_msg_hdr_val;
1874 }
1875 http_msg_complete_header:
1876 /*
1877 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001878 * Assumes msg->sol points to the first char, msg->sov points
1879 * to the first character of the value and msg->eol to the
1880 * first CR or LF so we know how the line ends. We insert last
1881 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001882 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001883 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001884 idx, idx->tail) < 0))
1885 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001886
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001887 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001888 if (likely(!HTTP_IS_CRLF(*ptr))) {
1889 goto http_msg_hdr_name;
1890 }
1891
1892 if (likely(*ptr == '\r'))
1893 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1894 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001895
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001897 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001898 /* Assumes msg->sol points to the first of either CR or LF.
1899 * Sets ->sov and ->next to the total header length, ->eoh to
1900 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1901 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001902 EXPECT_LF_HERE(ptr, http_msg_invalid);
1903 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001904 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001905 msg->eoh = msg->sol;
1906 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001907 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001908 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001909 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001910
1911 case HTTP_MSG_ERROR:
1912 /* this may only happen if we call http_msg_analyser() twice with an error */
1913 break;
1914
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001915 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001916#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001917 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1918 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001919#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001920 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001921 }
1922 http_msg_ood:
1923 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001924 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001925 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001926 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001927
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001928 http_msg_invalid:
1929 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001930 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001931 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001932 return;
1933}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001934
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001935/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1936 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1937 * nothing is done and 1 is returned.
1938 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001939static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001940{
1941 int delta;
1942 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001943 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001944
1945 if (msg->sl.rq.v_l != 0)
1946 return 1;
1947
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001948 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1949 if (txn->meth != HTTP_METH_GET)
1950 return 0;
1951
Willy Tarreau9b28e032012-10-12 23:49:43 +02001952 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001953 delta = 0;
1954
1955 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001956 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1957 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001958 }
1959 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001960 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001961 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001962 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001963 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001964 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001965 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001966 NULL, NULL);
1967 if (unlikely(!cur_end))
1968 return 0;
1969
1970 /* we have a full HTTP/1.0 request now and we know that
1971 * we have either a CR or an LF at <ptr>.
1972 */
1973 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1974 return 1;
1975}
1976
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001977/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001978 * and "keep-alive" values. If we already know that some headers may safely
1979 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001980 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1981 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001982 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001983 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1984 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1985 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001986 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001987 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001988void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001989{
Willy Tarreau5b154472009-12-21 20:11:07 +01001990 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001991 const char *hdr_val = "Connection";
1992 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001993
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001994 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001995 return;
1996
Willy Tarreau88d349d2010-01-25 12:15:43 +01001997 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1998 hdr_val = "Proxy-Connection";
1999 hdr_len = 16;
2000 }
2001
Willy Tarreau5b154472009-12-21 20:11:07 +01002002 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002003 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002004 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002005 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2006 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002007 if (to_del & 2)
2008 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002009 else
2010 txn->flags |= TX_CON_KAL_SET;
2011 }
2012 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2013 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002014 if (to_del & 1)
2015 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002016 else
2017 txn->flags |= TX_CON_CLO_SET;
2018 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002019 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2020 txn->flags |= TX_HDR_CONN_UPG;
2021 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002022 }
2023
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002024 txn->flags |= TX_HDR_CONN_PRS;
2025 return;
2026}
Willy Tarreau5b154472009-12-21 20:11:07 +01002027
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002028/* Apply desired changes on the Connection: header. Values may be removed and/or
2029 * added depending on the <wanted> flags, which are exclusively composed of
2030 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2031 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2032 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002033void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002034{
2035 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002036 const char *hdr_val = "Connection";
2037 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002038
2039 ctx.idx = 0;
2040
Willy Tarreau88d349d2010-01-25 12:15:43 +01002041
2042 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2043 hdr_val = "Proxy-Connection";
2044 hdr_len = 16;
2045 }
2046
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002047 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002048 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002049 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2050 if (wanted & TX_CON_KAL_SET)
2051 txn->flags |= TX_CON_KAL_SET;
2052 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002053 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002054 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002055 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2056 if (wanted & TX_CON_CLO_SET)
2057 txn->flags |= TX_CON_CLO_SET;
2058 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002059 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002060 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002061 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002062
2063 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2064 return;
2065
2066 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2067 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002068 hdr_val = "Connection: close";
2069 hdr_len = 17;
2070 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2071 hdr_val = "Proxy-Connection: close";
2072 hdr_len = 23;
2073 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002074 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002075 }
2076
2077 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2078 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002079 hdr_val = "Connection: keep-alive";
2080 hdr_len = 22;
2081 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2082 hdr_val = "Proxy-Connection: keep-alive";
2083 hdr_len = 28;
2084 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002085 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002086 }
2087 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002088}
2089
Willy Tarreauc24715e2014-04-17 15:21:20 +02002090/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
2091 * the first byte of data after the chunk size, so that we know we can forward
2092 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
2093 * the chunk size. That way it is always possible to differentiate between the
2094 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01002095 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002096 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002097 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002098static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002099{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002100 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002101 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002102 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002103 const char *end = buf->data + buf->size;
2104 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002105 unsigned int chunk = 0;
2106
2107 /* The chunk size is in the following form, though we are only
2108 * interested in the size and CRLF :
2109 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2110 */
2111 while (1) {
2112 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002113 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002114 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002115 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002116 if (c < 0) /* not a hex digit anymore */
2117 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002118 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002119 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002120 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002121 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002122 chunk = (chunk << 4) + c;
2123 }
2124
Willy Tarreaud98cf932009-12-27 22:54:55 +01002125 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002126 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002127 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002128
2129 while (http_is_spht[(unsigned char)*ptr]) {
2130 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002131 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002132 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002133 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002134 }
2135
Willy Tarreaud98cf932009-12-27 22:54:55 +01002136 /* Up to there, we know that at least one byte is present at *ptr. Check
2137 * for the end of chunk size.
2138 */
2139 while (1) {
2140 if (likely(HTTP_IS_CRLF(*ptr))) {
2141 /* we now have a CR or an LF at ptr */
2142 if (likely(*ptr == '\r')) {
2143 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002144 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002145 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002146 return 0;
2147 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002148
Willy Tarreaud98cf932009-12-27 22:54:55 +01002149 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002150 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002151 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002152 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002153 /* done */
2154 break;
2155 }
2156 else if (*ptr == ';') {
2157 /* chunk extension, ends at next CRLF */
2158 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002159 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002160 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002161 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002162
2163 while (!HTTP_IS_CRLF(*ptr)) {
2164 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002165 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002166 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002167 return 0;
2168 }
2169 /* we have a CRLF now, loop above */
2170 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002171 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002172 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002173 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002174 }
2175
Willy Tarreaud98cf932009-12-27 22:54:55 +01002176 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002177 * which may or may not be present. We save that into ->next,
2178 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002179 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002180 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002181 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002182 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002183 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002184 msg->chunk_len = chunk;
2185 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002186 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002187 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002188 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002189 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002190 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002191}
2192
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002193/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002194 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002195 * the trailers is found, it is automatically scheduled to be forwarded,
2196 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2197 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002198 * except maybe msg->next if it could parse some lines, and returns zero.
2199 * If a parse error is encountered, the function returns < 0 and does not
2200 * change anything except maybe msg->next. Note that the message must
2201 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002202 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002203 * forwarding, and that msg->next exactly matches the length of trailers
2204 * already parsed and not forwarded. It is also important to note that this
2205 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002206 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002207static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002208{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002209 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002210
Willy Tarreaua458b672012-03-05 11:17:50 +01002211 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002212 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002213 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002214 const char *ptr = b_ptr(buf, msg->next);
2215 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002216 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002217
2218 /* scan current line and stop at LF or CRLF */
2219 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002220 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002221 return 0;
2222
2223 if (*ptr == '\n') {
2224 if (!p1)
2225 p1 = ptr;
2226 p2 = ptr;
2227 break;
2228 }
2229
2230 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002231 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002232 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002233 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002234 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002235 p1 = ptr;
2236 }
2237
2238 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002239 if (ptr >= buf->data + buf->size)
2240 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002241 }
2242
2243 /* after LF; point to beginning of next line */
2244 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002245 if (p2 >= buf->data + buf->size)
2246 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002247
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002248 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002249 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002250 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002251
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002252 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002253 /* LF/CRLF at beginning of line => end of trailers at p2.
2254 * Everything was scheduled for forwarding, there's nothing
2255 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002256 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002257 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002258 msg->msg_state = HTTP_MSG_DONE;
2259 return 1;
2260 }
2261 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002262 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002263 }
2264}
2265
Willy Tarreauc24715e2014-04-17 15:21:20 +02002266/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2267 * or a possible LF alone at the end of a chunk. It automatically adjusts
2268 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002269 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002270 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2271 * not enough data are available, the function does not change anything and
2272 * returns zero. If a parse error is encountered, the function returns < 0 and
2273 * does not change anything. Note: this function is designed to parse wrapped
2274 * CRLF at the end of the buffer.
2275 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002276static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002277{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002278 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002279 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002280 int bytes;
2281
2282 /* NB: we'll check data availabilty at the end. It's not a
2283 * problem because whatever we match first will be checked
2284 * against the correct length.
2285 */
2286 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002287 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002288 if (*ptr == '\r') {
2289 bytes++;
2290 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002291 if (ptr >= buf->data + buf->size)
2292 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002293 }
2294
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002295 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002296 return 0;
2297
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002298 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002299 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002300 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002301 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002302
2303 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002304 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002305 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002306 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002307 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002308 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2309 return 1;
2310}
Willy Tarreau5b154472009-12-21 20:11:07 +01002311
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002312/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2313 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2314 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2315 * Unparsable qvalues return 1000 as "q=1.000".
2316 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002317int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002318{
2319 int q = 1000;
2320
Willy Tarreau506c69a2014-07-08 00:59:48 +02002321 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002322 goto out;
2323 q = (*qvalue++ - '0') * 1000;
2324
2325 if (*qvalue++ != '.')
2326 goto out;
2327
Willy Tarreau506c69a2014-07-08 00:59:48 +02002328 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002329 goto out;
2330 q += (*qvalue++ - '0') * 100;
2331
Willy Tarreau506c69a2014-07-08 00:59:48 +02002332 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002333 goto out;
2334 q += (*qvalue++ - '0') * 10;
2335
Willy Tarreau506c69a2014-07-08 00:59:48 +02002336 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002337 goto out;
2338 q += (*qvalue++ - '0') * 1;
2339 out:
2340 if (q > 1000)
2341 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002342 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002343 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002344 return q;
2345}
William Lallemand82fe75c2012-10-23 10:25:10 +02002346
2347/*
2348 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002349 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002350int select_compression_request_header(struct stream *s, struct buffer *req)
William Lallemand82fe75c2012-10-23 10:25:10 +02002351{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002352 struct http_txn *txn = s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002353 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002354 struct hdr_ctx ctx;
2355 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002356 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002357
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002358 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2359 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002360 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2361 */
2362 ctx.idx = 0;
2363 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2364 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002365 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2366 (ctx.vlen < 31 ||
2367 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2368 ctx.line[ctx.val + 30] < '6' ||
2369 (ctx.line[ctx.val + 30] == '6' &&
2370 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2371 s->comp_algo = NULL;
2372 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002373 }
2374
William Lallemand82fe75c2012-10-23 10:25:10 +02002375 /* search for the algo in the backend in priority or the frontend */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002376 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002377 int best_q = 0;
2378
William Lallemand82fe75c2012-10-23 10:25:10 +02002379 ctx.idx = 0;
2380 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002381 const char *qval;
2382 int q;
2383 int toklen;
2384
2385 /* try to isolate the token from the optional q-value */
2386 toklen = 0;
2387 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2388 toklen++;
2389
2390 qval = ctx.line + ctx.val + toklen;
2391 while (1) {
2392 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2393 qval++;
2394
2395 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2396 qval = NULL;
2397 break;
2398 }
2399 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002400
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002401 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2402 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002403
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002404 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2405 qval = NULL;
2406 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002407 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002408 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2409 break;
2410
2411 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2412 qval++;
2413 }
2414
2415 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002416 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002417
2418 if (q <= best_q)
2419 continue;
2420
2421 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2422 if (*(ctx.line + ctx.val) == '*' ||
Willy Tarreau615105e2015-03-28 16:40:46 +01002423 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002424 s->comp_algo = comp_algo;
2425 best_q = q;
2426 break;
2427 }
2428 }
2429 }
2430 }
2431
2432 /* remove all occurrences of the header when "compression offload" is set */
2433 if (s->comp_algo) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002434 if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002435 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2436 ctx.idx = 0;
2437 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2438 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002439 }
2440 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002441 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002442 }
2443
2444 /* identity is implicit does not require headers */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002445 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002446 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
Willy Tarreau615105e2015-03-28 16:40:46 +01002447 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002448 s->comp_algo = comp_algo;
2449 return 1;
2450 }
2451 }
2452 }
2453
2454 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002455 return 0;
2456}
2457
2458/*
2459 * Selects a comression algorithm depending of the server response.
2460 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002461int select_compression_response_header(struct stream *s, struct buffer *res)
William Lallemand82fe75c2012-10-23 10:25:10 +02002462{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002463 struct http_txn *txn = s->txn;
William Lallemand82fe75c2012-10-23 10:25:10 +02002464 struct http_msg *msg = &txn->rsp;
2465 struct hdr_ctx ctx;
2466 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002467
2468 /* no common compression algorithm was found in request header */
2469 if (s->comp_algo == NULL)
2470 goto fail;
2471
2472 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002473 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002474 goto fail;
2475
Jesse Hathaway2468d4e2015-03-06 20:16:15 +00002476 /* compress 200,201,202,203 responses only */
2477 if ((txn->status != 200) &&
2478 (txn->status != 201) &&
2479 (txn->status != 202) &&
2480 (txn->status != 203))
William Lallemandd3002612012-11-26 14:34:47 +01002481 goto fail;
2482
William Lallemand82fe75c2012-10-23 10:25:10 +02002483 /* Content-Length is null */
2484 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2485 goto fail;
2486
2487 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002488 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002489 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2490 goto fail;
2491
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002492 /* no compression when Cache-Control: no-transform is present in the message */
2493 ctx.idx = 0;
2494 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2495 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2496 goto fail;
2497 }
2498
William Lallemand82fe75c2012-10-23 10:25:10 +02002499 comp_type = NULL;
2500
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002501 /* we don't want to compress multipart content-types, nor content-types that are
2502 * not listed in the "compression type" directive if any. If no content-type was
2503 * found but configuration requires one, we don't compress either. Backend has
2504 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002505 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002506 ctx.idx = 0;
2507 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2508 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2509 goto fail;
2510
2511 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002512 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002513 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002514 if (ctx.vlen >= comp_type->name_len &&
2515 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002516 /* this Content-Type should be compressed */
2517 break;
2518 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002519 /* this Content-Type should not be compressed */
2520 if (comp_type == NULL)
2521 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002522 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002523 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002524 else { /* no content-type header */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002525 if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->comp->types))
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002526 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002527 }
2528
William Lallemandd85f9172012-11-09 17:05:39 +01002529 /* limit compression rate */
2530 if (global.comp_rate_lim > 0)
2531 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2532 goto fail;
2533
William Lallemand072a2bf2012-11-20 17:01:01 +01002534 /* limit cpu usage */
2535 if (idle_pct < compress_min_idle)
2536 goto fail;
2537
William Lallemand4c49fae2012-11-07 15:00:23 +01002538 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002539 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002540 goto fail;
2541
Willy Tarreaue7dff022015-04-03 01:14:29 +02002542 s->flags |= SF_COMP_READY;
William Lallemandec3e3892012-11-12 17:02:18 +01002543
William Lallemand82fe75c2012-10-23 10:25:10 +02002544 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002545 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002546 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2547 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2548
2549 /* add Transfer-Encoding header */
2550 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2551 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2552
2553 /*
2554 * Add Content-Encoding header when it's not identity encoding.
2555 * RFC 2616 : Identity encoding: This content-coding is used only in the
2556 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2557 * header.
2558 */
Willy Tarreau615105e2015-03-28 16:40:46 +01002559 if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002560 trash.len = 18;
2561 memcpy(trash.str, "Content-Encoding: ", trash.len);
Willy Tarreau615105e2015-03-28 16:40:46 +01002562 memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len);
2563 trash.len += s->comp_algo->ua_name_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002564 trash.str[trash.len] = '\0';
2565 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002566 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002567 return 1;
2568
2569fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002570 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002571 return 0;
2572}
2573
Willy Tarreau87b09662015-04-03 00:22:06 +02002574void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002575{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002576 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002577 int tmp = TX_CON_WANT_KAL;
2578
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002579 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2580 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002581 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2582 tmp = TX_CON_WANT_TUN;
2583
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002584 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002585 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2586 tmp = TX_CON_WANT_TUN;
2587 }
2588
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002589 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002590 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2591 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002592 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002593 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2594 tmp = TX_CON_WANT_CLO;
2595 else
2596 tmp = TX_CON_WANT_SCL;
2597 }
2598
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002599 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002600 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2601 tmp = TX_CON_WANT_CLO;
2602
2603 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2604 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2605
2606 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2607 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2608 /* parse the Connection header and possibly clean it */
2609 int to_del = 0;
2610 if ((msg->flags & HTTP_MSGF_VER_11) ||
2611 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002612 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002613 to_del |= 2; /* remove "keep-alive" */
2614 if (!(msg->flags & HTTP_MSGF_VER_11))
2615 to_del |= 1; /* remove "close" */
2616 http_parse_connection_header(txn, msg, to_del);
2617 }
2618
2619 /* check if client or config asks for explicit close in KAL/SCL */
2620 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2621 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2622 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2623 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2624 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002625 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002626 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2627}
William Lallemand82fe75c2012-10-23 10:25:10 +02002628
Willy Tarreaud787e662009-07-07 10:14:51 +02002629/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2630 * processing can continue on next analysers, or zero if it either needs more
2631 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002632 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002633 * when it has nothing left to do, and may remove any analyser when it wants to
2634 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002635 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002636int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002637{
Willy Tarreau59234e92008-11-30 23:51:27 +01002638 /*
2639 * We will parse the partial (or complete) lines.
2640 * We will check the request syntax, and also join multi-line
2641 * headers. An index of all the lines will be elaborated while
2642 * parsing.
2643 *
2644 * For the parsing, we use a 28 states FSM.
2645 *
2646 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002647 * req->buf->p = beginning of request
2648 * req->buf->p + msg->eoh = end of processed headers / start of current one
2649 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002650 * msg->eol = end of current header or line (LF or CRLF)
2651 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002652 *
2653 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002654 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002655 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2656 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002658
Willy Tarreau59234e92008-11-30 23:51:27 +01002659 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002660 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002661 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002662 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002663 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002664
Willy Tarreau87b09662015-04-03 00:22:06 +02002665 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002666 now_ms, __FUNCTION__,
2667 s,
2668 req,
2669 req->rex, req->wex,
2670 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002671 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002672 req->analysers);
2673
Willy Tarreau52a0c602009-08-16 22:45:38 +02002674 /* we're speaking HTTP here, so let's speak HTTP to the client */
2675 s->srv_error = http_return_srv_error;
2676
Willy Tarreau83e3af02009-12-28 17:39:57 +01002677 /* There's a protected area at the end of the buffer for rewriting
2678 * purposes. We don't want to start to parse the request if the
2679 * protected area is affected, because we may have to move processed
2680 * data later, which is much more complicated.
2681 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002682 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002683 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002684 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002685 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002686 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002687 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002688 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002689 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002690 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002691 return 0;
2692 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002693 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2694 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2695 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002696 }
2697
Willy Tarreau065e8332010-01-08 00:30:20 +01002698 /* Note that we have the same problem with the response ; we
2699 * may want to send a redirect, error or anything which requires
2700 * some spare space. So we'll ensure that we have at least
2701 * maxrewrite bytes available in the response buffer before
2702 * processing that one. This will only affect pipelined
2703 * keep-alive requests.
2704 */
2705 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002706 unlikely(!channel_is_rewritable(&s->res) ||
2707 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2708 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2709 if (s->res.buf->o) {
2710 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002711 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002712 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002713 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002714 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2715 s->res.flags |= CF_WAKE_WRITE;
2716 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002717 return 0;
2718 }
2719 }
2720
Willy Tarreau9b28e032012-10-12 23:49:43 +02002721 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002722 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002723 }
2724
Willy Tarreau59234e92008-11-30 23:51:27 +01002725 /* 1: we might have to print this header in debug mode */
2726 if (unlikely((global.mode & MODE_DEBUG) &&
2727 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002728 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002729 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002730
Willy Tarreau9b28e032012-10-12 23:49:43 +02002731 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002732 /* this is a bit complex : in case of error on the request line,
2733 * we know that rq.l is still zero, so we display only the part
2734 * up to the end of the line (truncated by debug_hdr).
2735 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002736 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002737 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002738
Willy Tarreau59234e92008-11-30 23:51:27 +01002739 sol += hdr_idx_first_pos(&txn->hdr_idx);
2740 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002741
Willy Tarreau59234e92008-11-30 23:51:27 +01002742 while (cur_idx) {
2743 eol = sol + txn->hdr_idx.v[cur_idx].len;
2744 debug_hdr("clihdr", s, sol, eol);
2745 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2746 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002747 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002748 }
2749
Willy Tarreau58f10d72006-12-04 02:26:12 +01002750
Willy Tarreau59234e92008-11-30 23:51:27 +01002751 /*
2752 * Now we quickly check if we have found a full valid request.
2753 * If not so, we check the FD and buffer states before leaving.
2754 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002755 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002756 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002757 * on a keep-alive stream, if we encounter and error, close, t/o,
2758 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002759 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002760 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002761 * Last, we may increase some tracked counters' http request errors on
2762 * the cases that are deliberately the client's fault. For instance,
2763 * a timeout or connection reset is not counted as an error. However
2764 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002765 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002766
Willy Tarreau655dce92009-11-08 13:10:58 +01002767 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002768 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002769 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002770 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002771 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002772 stream_inc_http_req_ctr(s);
2773 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002774 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002775 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002776 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002777
Willy Tarreau59234e92008-11-30 23:51:27 +01002778 /* 1: Since we are in header mode, if there's no space
2779 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002780 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002781 * must terminate it now.
2782 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002783 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002784 /* FIXME: check if URI is set and return Status
2785 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002786 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002787 stream_inc_http_req_ctr(s);
2788 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002789 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002790 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002791 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002792 goto return_bad_req;
2793 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002794
Willy Tarreau59234e92008-11-30 23:51:27 +01002795 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002796 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002797 if (!(s->flags & SF_ERR_MASK))
2798 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002799
Willy Tarreaufcffa692010-01-10 14:21:19 +01002800 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002801 goto failed_keep_alive;
2802
Willy Tarreau0f228a02015-05-01 15:37:53 +02002803 if (sess->fe->options & PR_O_IGNORE_PRB)
2804 goto failed_keep_alive;
2805
Willy Tarreau59234e92008-11-30 23:51:27 +01002806 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002807 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002808 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002809 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002810 }
2811
Willy Tarreaudc979f22012-12-04 10:39:01 +01002812 txn->status = 400;
Willy Tarreau59234e92008-11-30 23:51:27 +01002813 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002814 http_reply_and_close(s, txn->status, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002815 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002816
Willy Tarreau87b09662015-04-03 00:22:06 +02002817 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002818 proxy_inc_fe_req_ctr(sess->fe);
2819 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002820 if (sess->listener->counters)
2821 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002822
Willy Tarreaue7dff022015-04-03 01:14:29 +02002823 if (!(s->flags & SF_FINST_MASK))
2824 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002825 return 0;
2826 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002827
Willy Tarreau59234e92008-11-30 23:51:27 +01002828 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002829 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002830 if (!(s->flags & SF_ERR_MASK))
2831 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002832
Willy Tarreaufcffa692010-01-10 14:21:19 +01002833 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002834 goto failed_keep_alive;
2835
Willy Tarreau0f228a02015-05-01 15:37:53 +02002836 if (sess->fe->options & PR_O_IGNORE_PRB)
2837 goto failed_keep_alive;
2838
Willy Tarreau59234e92008-11-30 23:51:27 +01002839 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002840 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002841 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002842 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002843 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002844 txn->status = 408;
Willy Tarreau59234e92008-11-30 23:51:27 +01002845 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002846 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002847 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002848
Willy Tarreau87b09662015-04-03 00:22:06 +02002849 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002850 proxy_inc_fe_req_ctr(sess->fe);
2851 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002852 if (sess->listener->counters)
2853 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002854
Willy Tarreaue7dff022015-04-03 01:14:29 +02002855 if (!(s->flags & SF_FINST_MASK))
2856 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002857 return 0;
2858 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002859
Willy Tarreau59234e92008-11-30 23:51:27 +01002860 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002861 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002862 if (!(s->flags & SF_ERR_MASK))
2863 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002864
Willy Tarreaufcffa692010-01-10 14:21:19 +01002865 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002866 goto failed_keep_alive;
2867
Willy Tarreau0f228a02015-05-01 15:37:53 +02002868 if (sess->fe->options & PR_O_IGNORE_PRB)
2869 goto failed_keep_alive;
2870
Willy Tarreau4076a152009-04-02 15:18:36 +02002871 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002872 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002873 txn->status = 400;
Willy Tarreau59234e92008-11-30 23:51:27 +01002874 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002875 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002876 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002877
Willy Tarreau87b09662015-04-03 00:22:06 +02002878 stream_inc_http_err_ctr(s);
2879 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002880 proxy_inc_fe_req_ctr(sess->fe);
2881 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002882 if (sess->listener->counters)
2883 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002884
Willy Tarreaue7dff022015-04-03 01:14:29 +02002885 if (!(s->flags & SF_FINST_MASK))
2886 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002887 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002888 }
2889
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002890 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002891 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002892 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002893#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002894 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2895 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002896 /* We need more data, we have to re-enable quick-ack in case we
2897 * previously disabled it, otherwise we might cause the client
2898 * to delay next data.
2899 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002900 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002901 }
2902#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002903
Willy Tarreaufcffa692010-01-10 14:21:19 +01002904 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2905 /* If the client starts to talk, let's fall back to
2906 * request timeout processing.
2907 */
2908 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002909 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002910 }
2911
Willy Tarreau59234e92008-11-30 23:51:27 +01002912 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002913 if (!tick_isset(req->analyse_exp)) {
2914 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2915 (txn->flags & TX_WAIT_NEXT_RQ) &&
2916 tick_isset(s->be->timeout.httpka))
2917 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2918 else
2919 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2920 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002921
Willy Tarreau59234e92008-11-30 23:51:27 +01002922 /* we're not ready yet */
2923 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002924
2925 failed_keep_alive:
2926 /* Here we process low-level errors for keep-alive requests. In
2927 * short, if the request is not the first one and it experiences
2928 * a timeout, read error or shutdown, we just silently close so
2929 * that the client can try again.
2930 */
2931 txn->status = 0;
2932 msg->msg_state = HTTP_MSG_RQBEFORE;
2933 req->analysers = 0;
2934 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002935 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002936 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002937 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002938 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002939 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002940
Willy Tarreaud787e662009-07-07 10:14:51 +02002941 /* OK now we have a complete HTTP request with indexed headers. Let's
2942 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002943 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002944 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002945 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002946 * byte after the last LF. msg->sov points to the first byte of data.
2947 * msg->eol cannot be trusted because it may have been left uninitialized
2948 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002949 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002950
Willy Tarreau87b09662015-04-03 00:22:06 +02002951 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002952 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002953
Willy Tarreaub16a5742010-01-10 14:46:16 +01002954 if (txn->flags & TX_WAIT_NEXT_RQ) {
2955 /* kill the pending keep-alive timeout */
2956 txn->flags &= ~TX_WAIT_NEXT_RQ;
2957 req->analyse_exp = TICK_ETERNITY;
2958 }
2959
2960
Willy Tarreaud787e662009-07-07 10:14:51 +02002961 /* Maybe we found in invalid header name while we were configured not
2962 * to block on that, so we have to capture it now.
2963 */
2964 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002965 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002966
Willy Tarreau59234e92008-11-30 23:51:27 +01002967 /*
2968 * 1: identify the method
2969 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002970 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002971
2972 /* we can make use of server redirect on GET and HEAD */
2973 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002974 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002975
Willy Tarreau59234e92008-11-30 23:51:27 +01002976 /*
2977 * 2: check if the URI matches the monitor_uri.
2978 * We have to do this for every request which gets in, because
2979 * the monitor-uri is defined by the frontend.
2980 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002981 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2982 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002983 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002984 sess->fe->monitor_uri,
2985 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002986 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002987 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002988 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002989 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002990
Willy Tarreaue7dff022015-04-03 01:14:29 +02002991 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002992 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002993
Willy Tarreau59234e92008-11-30 23:51:27 +01002994 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002995 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002996 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002997
Willy Tarreau59234e92008-11-30 23:51:27 +01002998 ret = acl_pass(ret);
2999 if (cond->pol == ACL_COND_UNLESS)
3000 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01003001
Willy Tarreau59234e92008-11-30 23:51:27 +01003002 if (ret) {
3003 /* we fail this request, let's return 503 service unavail */
3004 txn->status = 503;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003005 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02003006 if (!(s->flags & SF_ERR_MASK))
3007 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01003008 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01003009 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003010 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01003011
Willy Tarreau59234e92008-11-30 23:51:27 +01003012 /* nothing to fail, let's reply normaly */
3013 txn->status = 200;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003014 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02003015 if (!(s->flags & SF_ERR_MASK))
3016 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01003017 goto return_prx_cond;
3018 }
3019
3020 /*
3021 * 3: Maybe we have to copy the original REQURI for the logs ?
3022 * Note: we cannot log anymore if the request has been
3023 * classified as invalid.
3024 */
3025 if (unlikely(s->logs.logwait & LW_REQ)) {
3026 /* we have a complete HTTP request that we must log */
3027 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
3028 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003029
Willy Tarreau59234e92008-11-30 23:51:27 +01003030 if (urilen >= REQURI_LEN)
3031 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02003032 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01003033 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003034
Willy Tarreaud79a3b22012-12-28 09:40:16 +01003035 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01003036 s->do_log(s);
3037 } else {
3038 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003039 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003040 }
Willy Tarreau06619262006-12-17 08:37:22 +01003041
Willy Tarreau91852eb2015-05-01 13:26:00 +02003042 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
3043 * exactly one digit "." one digit. This check may be disabled using
3044 * option accept-invalid-http-request.
3045 */
3046 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
3047 if (msg->sl.rq.v_l != 8) {
3048 msg->err_pos = msg->sl.rq.v;
3049 goto return_bad_req;
3050 }
3051
3052 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
3053 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
3054 req->buf->p[msg->sl.rq.v + 6] != '.' ||
3055 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
3056 msg->err_pos = msg->sl.rq.v + 4;
3057 goto return_bad_req;
3058 }
3059 }
Willy Tarreau13317662015-05-01 13:47:08 +02003060 else {
3061 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
3062 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
3063 goto return_bad_req;
3064 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02003065
Willy Tarreau5b154472009-12-21 20:11:07 +01003066 /* ... and check if the request is HTTP/1.1 or above */
3067 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003068 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
3069 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
3070 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003071 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01003072
3073 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01003074 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01003075
Willy Tarreau88d349d2010-01-25 12:15:43 +01003076 /* if the frontend has "option http-use-proxy-header", we'll check if
3077 * we have what looks like a proxied connection instead of a connection,
3078 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
3079 * Note that this is *not* RFC-compliant, however browsers and proxies
3080 * happen to do that despite being non-standard :-(
3081 * We consider that a request not beginning with either '/' or '*' is
3082 * a proxied connection, which covers both "scheme://location" and
3083 * CONNECT ip:port.
3084 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003085 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003086 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01003087 txn->flags |= TX_USE_PX_CONN;
3088
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003089 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003090 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003091
Willy Tarreau59234e92008-11-30 23:51:27 +01003092 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003093 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003094 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003095 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003096
Willy Tarreau557f1992015-05-01 10:05:17 +02003097 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3098 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003099 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003100 * The length of a message body is determined by one of the following
3101 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003102 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003103 * 1. Any response to a HEAD request and any response with a 1xx
3104 * (Informational), 204 (No Content), or 304 (Not Modified) status
3105 * code is always terminated by the first empty line after the
3106 * header fields, regardless of the header fields present in the
3107 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003108 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003109 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3110 * the connection will become a tunnel immediately after the empty
3111 * line that concludes the header fields. A client MUST ignore any
3112 * Content-Length or Transfer-Encoding header fields received in
3113 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003114 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003115 * 3. If a Transfer-Encoding header field is present and the chunked
3116 * transfer coding (Section 4.1) is the final encoding, the message
3117 * body length is determined by reading and decoding the chunked
3118 * data until the transfer coding indicates the data is complete.
3119 *
3120 * If a Transfer-Encoding header field is present in a response and
3121 * the chunked transfer coding is not the final encoding, the
3122 * message body length is determined by reading the connection until
3123 * it is closed by the server. If a Transfer-Encoding header field
3124 * is present in a request and the chunked transfer coding is not
3125 * the final encoding, the message body length cannot be determined
3126 * reliably; the server MUST respond with the 400 (Bad Request)
3127 * status code and then close the connection.
3128 *
3129 * If a message is received with both a Transfer-Encoding and a
3130 * Content-Length header field, the Transfer-Encoding overrides the
3131 * Content-Length. Such a message might indicate an attempt to
3132 * perform request smuggling (Section 9.5) or response splitting
3133 * (Section 9.4) and ought to be handled as an error. A sender MUST
3134 * remove the received Content-Length field prior to forwarding such
3135 * a message downstream.
3136 *
3137 * 4. If a message is received without Transfer-Encoding and with
3138 * either multiple Content-Length header fields having differing
3139 * field-values or a single Content-Length header field having an
3140 * invalid value, then the message framing is invalid and the
3141 * recipient MUST treat it as an unrecoverable error. If this is a
3142 * request message, the server MUST respond with a 400 (Bad Request)
3143 * status code and then close the connection. If this is a response
3144 * message received by a proxy, the proxy MUST close the connection
3145 * to the server, discard the received response, and send a 502 (Bad
3146 * Gateway) response to the client. If this is a response message
3147 * received by a user agent, the user agent MUST close the
3148 * connection to the server and discard the received response.
3149 *
3150 * 5. If a valid Content-Length header field is present without
3151 * Transfer-Encoding, its decimal value defines the expected message
3152 * body length in octets. If the sender closes the connection or
3153 * the recipient times out before the indicated number of octets are
3154 * received, the recipient MUST consider the message to be
3155 * incomplete and close the connection.
3156 *
3157 * 6. If this is a request message and none of the above are true, then
3158 * the message body length is zero (no message body is present).
3159 *
3160 * 7. Otherwise, this is a response message without a declared message
3161 * body length, so the message body length is determined by the
3162 * number of octets received prior to the server closing the
3163 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003164 */
3165
Willy Tarreau32b47f42009-10-18 20:55:02 +02003166 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003167 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003168 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003169 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003170 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3171 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003172 /* chunked not last, return badreq */
3173 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003174 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003175 }
3176
Willy Tarreau1c913912015-04-30 10:57:51 +02003177 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003178 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003179 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3180 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3181 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3182 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003183 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003184 signed long long cl;
3185
Willy Tarreauad14f752011-09-02 20:33:27 +02003186 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003187 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003188 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003189 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003190
Willy Tarreauad14f752011-09-02 20:33:27 +02003191 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003192 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003193 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003194 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003195
Willy Tarreauad14f752011-09-02 20:33:27 +02003196 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003197 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003198 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003199 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003200
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003201 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003202 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003203 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003204 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003205
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003206 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003207 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003208 }
3209
Willy Tarreau34dfc602015-05-01 10:09:49 +02003210 /* even bodyless requests have a known length */
3211 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003212
Willy Tarreau179085c2014-04-28 16:48:56 +02003213 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3214 * only change if both the request and the config reference something else.
3215 * Option httpclose by itself sets tunnel mode where headers are mangled.
3216 * However, if another mode is set, it will affect it (eg: server-close/
3217 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3218 * if FE and BE have the same settings (common). The method consists in
3219 * checking if options changed between the two calls (implying that either
3220 * one is non-null, or one of them is non-null and we are there for the first
3221 * time.
3222 */
3223 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003224 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003225 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003226
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003227 /* we may have to wait for the request's body */
3228 if ((s->be->options & PR_O_WREQ_BODY) &&
3229 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3230 req->analysers |= AN_REQ_HTTP_BODY;
3231
Willy Tarreaud787e662009-07-07 10:14:51 +02003232 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003233 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003234 req->analyse_exp = TICK_ETERNITY;
3235 return 1;
3236
3237 return_bad_req:
3238 /* We centralize bad requests processing here */
3239 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3240 /* we detected a parsing error. We want to archive this request
3241 * in the dedicated proxy area for later troubleshooting.
3242 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003243 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003244 }
3245
3246 txn->req.msg_state = HTTP_MSG_ERROR;
3247 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003248 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003249
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003250 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003251 if (sess->listener->counters)
3252 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003253
3254 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003255 if (!(s->flags & SF_ERR_MASK))
3256 s->flags |= SF_ERR_PRXCOND;
3257 if (!(s->flags & SF_FINST_MASK))
3258 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003259
3260 req->analysers = 0;
3261 req->analyse_exp = TICK_ETERNITY;
3262 return 0;
3263}
3264
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003265
Willy Tarreau347a35d2013-11-22 17:51:09 +01003266/* This function prepares an applet to handle the stats. It can deal with the
3267 * "100-continue" expectation, check that admin rules are met for POST requests,
3268 * and program a response message if something was unexpected. It cannot fail
3269 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003270 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003271 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003272 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003273 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003274int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003275{
3276 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003277 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003278 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003279 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003280 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003281 struct uri_auth *uri_auth = s->be->uri_auth;
3282 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003283 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003284
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003285 appctx = si_appctx(si);
3286 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3287 appctx->st1 = appctx->st2 = 0;
3288 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3289 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003290 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003291 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003292
3293 uri = msg->chn->buf->p + msg->sl.rq.u;
3294 lookup = uri + uri_auth->uri_len;
3295
3296 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3297 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003298 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003299 break;
3300 }
3301 }
3302
3303 if (uri_auth->refresh) {
3304 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3305 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003306 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003307 break;
3308 }
3309 }
3310 }
3311
3312 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3313 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003314 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003315 break;
3316 }
3317 }
3318
3319 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3320 if (memcmp(h, ";st=", 4) == 0) {
3321 int i;
3322 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003323 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003324 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3325 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003326 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003327 break;
3328 }
3329 }
3330 break;
3331 }
3332 }
3333
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003334 appctx->ctx.stats.scope_str = 0;
3335 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003336 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3337 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3338 int itx = 0;
3339 const char *h2;
3340 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3341 const char *err;
3342
3343 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3344 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003345 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003346 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3347 itx++;
3348 h++;
3349 }
3350
3351 if (itx > STAT_SCOPE_TXT_MAXLEN)
3352 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003353 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003354
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003355 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003356 memcpy(scope_txt, h2, itx);
3357 scope_txt[itx] = '\0';
3358 err = invalid_char(scope_txt);
3359 if (err) {
3360 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003361 appctx->ctx.stats.scope_str = 0;
3362 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003363 }
3364 break;
3365 }
3366 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003367
3368 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003369 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003370 int ret = 1;
3371
3372 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003373 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003374 ret = acl_pass(ret);
3375 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3376 ret = !ret;
3377 }
3378
3379 if (ret) {
3380 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003381 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003382 break;
3383 }
3384 }
3385
3386 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003387 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003388 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003389 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003390 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3391 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003392 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003393 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003394 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003395 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3396 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003397 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003398 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003399 else {
3400 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003401 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003402 }
3403
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003404 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003405 return 1;
3406}
3407
Lukas Tribus67db8df2013-06-23 17:37:13 +02003408/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3409 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3410 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003411void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003412{
3413#ifdef IP_TOS
3414 if (from.ss_family == AF_INET)
3415 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3416#endif
3417#ifdef IPV6_TCLASS
3418 if (from.ss_family == AF_INET6) {
3419 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3420 /* v4-mapped addresses need IP_TOS */
3421 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3422 else
3423 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3424 }
3425#endif
3426}
3427
Willy Tarreau87b09662015-04-03 00:22:06 +02003428int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003429 const char* name, unsigned int name_len,
3430 const char *str, struct my_regex *re,
3431 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003432{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003433 struct hdr_ctx ctx;
3434 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003435 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003436 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3437 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003438 struct chunk *output = get_trash_chunk();
3439
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003440 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003441
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003442 /* Choose the header browsing function. */
3443 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003444 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003445 http_find_hdr_func = http_find_header2;
3446 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003447 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003448 http_find_hdr_func = http_find_full_header2;
3449 break;
3450 default: /* impossible */
3451 return -1;
3452 }
3453
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003454 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3455 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003456 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003457 char *val = ctx.line + ctx.val;
3458 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003459
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003460 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3461 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003462
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003463 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003464 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003465 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003466
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003467 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003468
3469 hdr->len += delta;
3470 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003471
3472 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003473 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003474 }
3475
3476 return 0;
3477}
3478
Willy Tarreau87b09662015-04-03 00:22:06 +02003479static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003480 const char* name, unsigned int name_len,
3481 struct list *fmt, struct my_regex *re,
3482 int action)
3483{
3484 struct chunk *replace = get_trash_chunk();
3485
3486 replace->len = build_logline(s, replace->str, replace->size, fmt);
3487 if (replace->len >= replace->size - 1)
3488 return -1;
3489
3490 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3491}
3492
Willy Tarreau87b09662015-04-03 00:22:06 +02003493/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003494 * transaction <txn>. Returns the verdict of the first rule that prevents
3495 * further processing of the request (auth, deny, ...), and defaults to
3496 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3497 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3498 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003499 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003500enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003501http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003502{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003503 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003504 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003505 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003506 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003507 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003508 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003509 int act_flags = 0;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003510
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003511 /* If "the current_rule_list" match the executed rule list, we are in
3512 * resume condition. If a resume is needed it is always in the action
3513 * and never in the ACL or converters. In this case, we initialise the
3514 * current rule, and go to the action execution point.
3515 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003516 if (s->current_rule) {
3517 rule = s->current_rule;
3518 s->current_rule = NULL;
3519 if (s->current_rule_list == rules)
3520 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003521 }
3522 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003523
Willy Tarreauff011f22011-01-06 17:51:27 +01003524 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003525
Willy Tarreau96257ec2012-12-27 10:46:37 +01003526 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003527 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003528 int ret;
3529
Willy Tarreau192252e2015-04-04 01:47:55 +02003530 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003531 ret = acl_pass(ret);
3532
Willy Tarreauff011f22011-01-06 17:51:27 +01003533 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003534 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003535
3536 if (!ret) /* condition not matched */
3537 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003538 }
3539
Willy Tarreauacc98002015-09-27 23:34:39 +02003540 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003541resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003542 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003543 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003544 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003545
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003546 case ACT_ACTION_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003547 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003548 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003549
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003550 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003551 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003552 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003553 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003554
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003555 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003556 /* Auth might be performed on regular http-req rules as well as on stats */
3557 auth_realm = rule->arg.auth.realm;
3558 if (!auth_realm) {
3559 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3560 auth_realm = STATS_DEFAULT_REALM;
3561 else
3562 auth_realm = px->id;
3563 }
3564 /* send 401/407 depending on whether we use a proxy or not. We still
3565 * count one error, because normal browsing won't significantly
3566 * increase the counter but brute force attempts will.
3567 */
3568 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3569 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003570 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003571 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003572 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003573
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003574 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003575 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3576 return HTTP_RULE_RES_BADREQ;
3577 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003578
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003579 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003580 s->task->nice = rule->arg.nice;
3581 break;
3582
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003583 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003584 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003585 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003586 break;
3587
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003588 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003589#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003590 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003591 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003592#endif
3593 break;
3594
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003595 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003596 s->logs.level = rule->arg.loglevel;
3597 break;
3598
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003599 case ACT_HTTP_REPLACE_HDR:
3600 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003601 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3602 rule->arg.hdr_add.name_len,
3603 &rule->arg.hdr_add.fmt,
3604 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003605 return HTTP_RULE_RES_BADREQ;
3606 break;
3607
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003608 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003609 ctx.idx = 0;
3610 /* remove all occurrences of the header */
3611 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3612 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3613 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003614 }
Willy Tarreau85603282015-01-21 20:39:27 +01003615 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003616
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003617 case ACT_HTTP_SET_HDR:
3618 case ACT_HTTP_ADD_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003619 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3620 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3621 trash.len = rule->arg.hdr_add.name_len;
3622 trash.str[trash.len++] = ':';
3623 trash.str[trash.len++] = ' ';
3624 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01003625
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003626 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003627 /* remove all occurrences of the header */
3628 ctx.idx = 0;
3629 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3630 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3631 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3632 }
3633 }
3634
Willy Tarreau96257ec2012-12-27 10:46:37 +01003635 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3636 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003637
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003638 case ACT_HTTP_DEL_ACL:
3639 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003640 struct pat_ref *ref;
3641 char *key;
3642 int len;
3643
3644 /* collect reference */
3645 ref = pat_ref_lookup(rule->arg.map.ref);
3646 if (!ref)
3647 continue;
3648
3649 /* collect key */
3650 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3651 key = trash.str;
3652 key[len] = '\0';
3653
3654 /* perform update */
3655 /* returned code: 1=ok, 0=ko */
3656 pat_ref_delete(ref, key);
3657
3658 break;
3659 }
3660
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003661 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003662 struct pat_ref *ref;
3663 char *key;
3664 struct chunk *trash_key;
3665 int len;
3666
3667 trash_key = get_trash_chunk();
3668
3669 /* collect reference */
3670 ref = pat_ref_lookup(rule->arg.map.ref);
3671 if (!ref)
3672 continue;
3673
3674 /* collect key */
3675 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3676 key = trash_key->str;
3677 key[len] = '\0';
3678
3679 /* perform update */
3680 /* add entry only if it does not already exist */
3681 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003682 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003683
3684 break;
3685 }
3686
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003687 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003688 struct pat_ref *ref;
3689 char *key, *value;
3690 struct chunk *trash_key, *trash_value;
3691 int len;
3692
3693 trash_key = get_trash_chunk();
3694 trash_value = get_trash_chunk();
3695
3696 /* collect reference */
3697 ref = pat_ref_lookup(rule->arg.map.ref);
3698 if (!ref)
3699 continue;
3700
3701 /* collect key */
3702 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3703 key = trash_key->str;
3704 key[len] = '\0';
3705
3706 /* collect value */
3707 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3708 value = trash_value->str;
3709 value[len] = '\0';
3710
3711 /* perform update */
3712 if (pat_ref_find_elt(ref, key) != NULL)
3713 /* update entry if it exists */
3714 pat_ref_set(ref, key, value, NULL);
3715 else
3716 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003717 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003718
3719 break;
3720 }
William Lallemand73025dd2014-04-24 14:38:37 +02003721
Thierry FOURNIER42148732015-09-02 17:17:33 +02003722 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003723 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3724 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003725
Willy Tarreauacc98002015-09-27 23:34:39 +02003726 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003727 case ACT_RET_ERR:
3728 case ACT_RET_CONT:
3729 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003730 case ACT_RET_STOP:
3731 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003732 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003733 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003734 return HTTP_RULE_RES_YIELD;
3735 }
William Lallemand73025dd2014-04-24 14:38:37 +02003736 break;
3737
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003738 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003739 /* Note: only the first valid tracking parameter of each
3740 * applies.
3741 */
3742
3743 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3744 struct stktable *t;
3745 struct stksess *ts;
3746 struct stktable_key *key;
3747 void *ptr;
3748
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003749 t = rule->arg.trk_ctr.table.t;
3750 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
Willy Tarreau09448f72014-06-25 18:12:15 +02003751
3752 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003753 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003754
3755 /* let's count a new HTTP request as it's the first time we do it */
3756 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3757 if (ptr)
3758 stktable_data_cast(ptr, http_req_cnt)++;
3759
3760 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3761 if (ptr)
3762 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3763 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3764
3765 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003766 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003767 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3768 }
3769 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003770 break;
3771
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003772 case ACT_HTTP_REQ_SET_SRC:
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003773 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
3774 struct sample *smp;
3775
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02003776 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003777
3778 if (smp) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003779 if (smp->data.type == SMP_T_IPV4) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003780 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003781 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003782 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003783 } else if (smp->data.type == SMP_T_IPV6) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003784 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003785 memcpy(&((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003786 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0;
3787 }
3788 }
3789 }
3790 break;
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003791
3792 /* other flags exists, but normaly, they never be matched. */
3793 default:
3794 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003795 }
3796 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003797
3798 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003799 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003800}
3801
Willy Tarreau71241ab2012-12-27 11:30:54 +01003802
Willy Tarreau51d861a2015-05-22 17:30:48 +02003803/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3804 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3805 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3806 * is returned, the process can continue the evaluation of next rule list. If
3807 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3808 * is returned, it means the operation could not be processed and a server error
3809 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3810 * deny rule. If *YIELD is returned, the caller must call again the function
3811 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003812 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003813static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003814http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003815{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003816 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003817 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003818 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003819 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003820 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003821 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003822
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003823 /* If "the current_rule_list" match the executed rule list, we are in
3824 * resume condition. If a resume is needed it is always in the action
3825 * and never in the ACL or converters. In this case, we initialise the
3826 * current rule, and go to the action execution point.
3827 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003828 if (s->current_rule) {
3829 rule = s->current_rule;
3830 s->current_rule = NULL;
3831 if (s->current_rule_list == rules)
3832 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003833 }
3834 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003835
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003836 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003837
3838 /* check optional condition */
3839 if (rule->cond) {
3840 int ret;
3841
Willy Tarreau192252e2015-04-04 01:47:55 +02003842 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003843 ret = acl_pass(ret);
3844
3845 if (rule->cond->pol == ACL_COND_UNLESS)
3846 ret = !ret;
3847
3848 if (!ret) /* condition not matched */
3849 continue;
3850 }
3851
Willy Tarreauacc98002015-09-27 23:34:39 +02003852 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003853resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003854 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003855 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003856 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003857
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003858 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003859 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003860 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003861
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003862 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003863 s->task->nice = rule->arg.nice;
3864 break;
3865
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003866 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003867 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003868 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003869 break;
3870
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003871 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003872#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003873 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003874 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003875#endif
3876 break;
3877
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003878 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003879 s->logs.level = rule->arg.loglevel;
3880 break;
3881
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003882 case ACT_HTTP_REPLACE_HDR:
3883 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003884 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3885 rule->arg.hdr_add.name_len,
3886 &rule->arg.hdr_add.fmt,
3887 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003888 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003889 break;
3890
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003891 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003892 ctx.idx = 0;
3893 /* remove all occurrences of the header */
3894 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3895 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3896 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3897 }
Willy Tarreau85603282015-01-21 20:39:27 +01003898 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003899
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003900 case ACT_HTTP_SET_HDR:
3901 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003902 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3903 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3904 trash.len = rule->arg.hdr_add.name_len;
3905 trash.str[trash.len++] = ':';
3906 trash.str[trash.len++] = ' ';
3907 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01003908
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003909 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003910 /* remove all occurrences of the header */
3911 ctx.idx = 0;
3912 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3913 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3914 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3915 }
3916 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003917 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3918 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003919
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003920 case ACT_HTTP_DEL_ACL:
3921 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003922 struct pat_ref *ref;
3923 char *key;
3924 int len;
3925
3926 /* collect reference */
3927 ref = pat_ref_lookup(rule->arg.map.ref);
3928 if (!ref)
3929 continue;
3930
3931 /* collect key */
3932 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3933 key = trash.str;
3934 key[len] = '\0';
3935
3936 /* perform update */
3937 /* returned code: 1=ok, 0=ko */
3938 pat_ref_delete(ref, key);
3939
3940 break;
3941 }
3942
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003943 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003944 struct pat_ref *ref;
3945 char *key;
3946 struct chunk *trash_key;
3947 int len;
3948
3949 trash_key = get_trash_chunk();
3950
3951 /* collect reference */
3952 ref = pat_ref_lookup(rule->arg.map.ref);
3953 if (!ref)
3954 continue;
3955
3956 /* collect key */
3957 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3958 key = trash_key->str;
3959 key[len] = '\0';
3960
3961 /* perform update */
3962 /* check if the entry already exists */
3963 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003964 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003965
3966 break;
3967 }
3968
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003969 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003970 struct pat_ref *ref;
3971 char *key, *value;
3972 struct chunk *trash_key, *trash_value;
3973 int len;
3974
3975 trash_key = get_trash_chunk();
3976 trash_value = get_trash_chunk();
3977
3978 /* collect reference */
3979 ref = pat_ref_lookup(rule->arg.map.ref);
3980 if (!ref)
3981 continue;
3982
3983 /* collect key */
3984 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3985 key = trash_key->str;
3986 key[len] = '\0';
3987
3988 /* collect value */
3989 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3990 value = trash_value->str;
3991 value[len] = '\0';
3992
3993 /* perform update */
3994 if (pat_ref_find_elt(ref, key) != NULL)
3995 /* update entry if it exists */
3996 pat_ref_set(ref, key, value, NULL);
3997 else
3998 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003999 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02004000
4001 break;
4002 }
William Lallemand73025dd2014-04-24 14:38:37 +02004003
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004004 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02004005 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
4006 return HTTP_RULE_RES_BADREQ;
4007 return HTTP_RULE_RES_DONE;
4008
Thierry FOURNIER42148732015-09-02 17:17:33 +02004009 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02004010 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
4011 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02004012
Willy Tarreauacc98002015-09-27 23:34:39 +02004013 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004014 case ACT_RET_ERR:
4015 case ACT_RET_CONT:
4016 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02004017 case ACT_RET_STOP:
4018 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004019 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02004020 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004021 return HTTP_RULE_RES_YIELD;
4022 }
William Lallemand73025dd2014-04-24 14:38:37 +02004023 break;
4024
Thierry FOURNIER22e49012015-08-05 19:13:48 +02004025 /* other flags exists, but normaly, they never be matched. */
4026 default:
4027 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004028 }
4029 }
4030
4031 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004032 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004033}
4034
4035
Willy Tarreau71241ab2012-12-27 11:30:54 +01004036/* Perform an HTTP redirect based on the information in <rule>. The function
4037 * returns non-zero on success, or zero in case of a, irrecoverable error such
4038 * as too large a request to build a valid response.
4039 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004040static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004041{
Willy Tarreaub329a312015-05-22 16:27:37 +02004042 struct http_msg *req = &txn->req;
4043 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004044 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004045 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004046
4047 /* build redirect message */
4048 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004049 case 308:
4050 msg_fmt = HTTP_308;
4051 break;
4052 case 307:
4053 msg_fmt = HTTP_307;
4054 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004055 case 303:
4056 msg_fmt = HTTP_303;
4057 break;
4058 case 301:
4059 msg_fmt = HTTP_301;
4060 break;
4061 case 302:
4062 default:
4063 msg_fmt = HTTP_302;
4064 break;
4065 }
4066
4067 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
4068 return 0;
4069
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004070 location = trash.str + trash.len;
4071
Willy Tarreau71241ab2012-12-27 11:30:54 +01004072 switch(rule->type) {
4073 case REDIRECT_TYPE_SCHEME: {
4074 const char *path;
4075 const char *host;
4076 struct hdr_ctx ctx;
4077 int pathlen;
4078 int hostlen;
4079
4080 host = "";
4081 hostlen = 0;
4082 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004083 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004084 host = ctx.line + ctx.val;
4085 hostlen = ctx.vlen;
4086 }
4087
4088 path = http_get_path(txn);
4089 /* build message using path */
4090 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004091 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004092 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4093 int qs = 0;
4094 while (qs < pathlen) {
4095 if (path[qs] == '?') {
4096 pathlen = qs;
4097 break;
4098 }
4099 qs++;
4100 }
4101 }
4102 } else {
4103 path = "/";
4104 pathlen = 1;
4105 }
4106
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004107 if (rule->rdr_str) { /* this is an old "redirect" rule */
4108 /* check if we can add scheme + "://" + host + path */
4109 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
4110 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004111
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004112 /* add scheme */
4113 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4114 trash.len += rule->rdr_len;
4115 }
4116 else {
4117 /* add scheme with executing log format */
4118 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004119
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004120 /* check if we can add scheme + "://" + host + path */
4121 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
4122 return 0;
4123 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004124 /* add "://" */
4125 memcpy(trash.str + trash.len, "://", 3);
4126 trash.len += 3;
4127
4128 /* add host */
4129 memcpy(trash.str + trash.len, host, hostlen);
4130 trash.len += hostlen;
4131
4132 /* add path */
4133 memcpy(trash.str + trash.len, path, pathlen);
4134 trash.len += pathlen;
4135
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004136 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004137 if (trash.len && trash.str[trash.len - 1] != '/' &&
4138 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4139 if (trash.len > trash.size - 5)
4140 return 0;
4141 trash.str[trash.len] = '/';
4142 trash.len++;
4143 }
4144
4145 break;
4146 }
4147 case REDIRECT_TYPE_PREFIX: {
4148 const char *path;
4149 int pathlen;
4150
4151 path = http_get_path(txn);
4152 /* build message using path */
4153 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004154 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004155 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4156 int qs = 0;
4157 while (qs < pathlen) {
4158 if (path[qs] == '?') {
4159 pathlen = qs;
4160 break;
4161 }
4162 qs++;
4163 }
4164 }
4165 } else {
4166 path = "/";
4167 pathlen = 1;
4168 }
4169
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004170 if (rule->rdr_str) { /* this is an old "redirect" rule */
4171 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4172 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004173
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004174 /* add prefix. Note that if prefix == "/", we don't want to
4175 * add anything, otherwise it makes it hard for the user to
4176 * configure a self-redirection.
4177 */
4178 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4179 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4180 trash.len += rule->rdr_len;
4181 }
4182 }
4183 else {
4184 /* add prefix with executing log format */
4185 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4186
4187 /* Check length */
4188 if (trash.len + pathlen > trash.size - 4)
4189 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004190 }
4191
4192 /* add path */
4193 memcpy(trash.str + trash.len, path, pathlen);
4194 trash.len += pathlen;
4195
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004196 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004197 if (trash.len && trash.str[trash.len - 1] != '/' &&
4198 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4199 if (trash.len > trash.size - 5)
4200 return 0;
4201 trash.str[trash.len] = '/';
4202 trash.len++;
4203 }
4204
4205 break;
4206 }
4207 case REDIRECT_TYPE_LOCATION:
4208 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004209 if (rule->rdr_str) { /* this is an old "redirect" rule */
4210 if (trash.len + rule->rdr_len > trash.size - 4)
4211 return 0;
4212
4213 /* add location */
4214 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4215 trash.len += rule->rdr_len;
4216 }
4217 else {
4218 /* add location with executing log format */
4219 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004220
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004221 /* Check left length */
4222 if (trash.len > trash.size - 4)
4223 return 0;
4224 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004225 break;
4226 }
4227
4228 if (rule->cookie_len) {
4229 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4230 trash.len += 14;
4231 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4232 trash.len += rule->cookie_len;
4233 memcpy(trash.str + trash.len, "\r\n", 2);
4234 trash.len += 2;
4235 }
4236
4237 /* add end of headers and the keep-alive/close status.
4238 * We may choose to set keep-alive if the Location begins
4239 * with a slash, because the client will come back to the
4240 * same server.
4241 */
4242 txn->status = rule->code;
4243 /* let's log the request time */
4244 s->logs.tv_request = now;
4245
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004246 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004247 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004248 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004249 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4250 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4251 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004252 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004253 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4254 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4255 trash.len += 30;
4256 } else {
4257 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4258 trash.len += 24;
4259 }
4260 }
4261 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4262 trash.len += 4;
Willy Tarreaub329a312015-05-22 16:27:37 +02004263 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004264 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004265 bi_fast_delete(req->chn->buf, req->sov);
4266 req->next -= req->sov;
4267 req->sov = 0;
4268 s->req.analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004269 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreaub329a312015-05-22 16:27:37 +02004270 req->msg_state = HTTP_MSG_CLOSED;
4271 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004272 /* Trim any possible response */
4273 res->chn->buf->i = 0;
4274 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004275 } else {
4276 /* keep-alive not possible */
4277 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4278 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4279 trash.len += 29;
4280 } else {
4281 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4282 trash.len += 23;
4283 }
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004284 http_reply_and_close(s, txn->status, &trash);
Willy Tarreaub329a312015-05-22 16:27:37 +02004285 req->chn->analysers = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004286 }
4287
Willy Tarreaue7dff022015-04-03 01:14:29 +02004288 if (!(s->flags & SF_ERR_MASK))
4289 s->flags |= SF_ERR_LOCAL;
4290 if (!(s->flags & SF_FINST_MASK))
4291 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004292
4293 return 1;
4294}
4295
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004296/* This stream analyser runs all HTTP request processing which is common to
4297 * frontends and backends, which means blocking ACLs, filters, connection-close,
4298 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004299 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004300 * either needs more data or wants to immediately abort the request (eg: deny,
4301 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004302 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004303int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004304{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004305 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004306 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004307 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004308 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004309 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004310 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004311
Willy Tarreau655dce92009-11-08 13:10:58 +01004312 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004313 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004314 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004315 }
4316
Willy Tarreau87b09662015-04-03 00:22:06 +02004317 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02004318 now_ms, __FUNCTION__,
4319 s,
4320 req,
4321 req->rex, req->wex,
4322 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004323 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004324 req->analysers);
4325
Willy Tarreau65410832014-04-28 21:25:43 +02004326 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004327 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004328
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004329 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004330 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004331 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004332
Willy Tarreau0b748332014-04-29 00:13:29 +02004333 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004334 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4335 goto return_prx_yield;
4336
Willy Tarreau0b748332014-04-29 00:13:29 +02004337 case HTTP_RULE_RES_CONT:
4338 case HTTP_RULE_RES_STOP: /* nothing to do */
4339 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004340
Willy Tarreau0b748332014-04-29 00:13:29 +02004341 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4342 if (txn->flags & TX_CLTARPIT)
4343 goto tarpit;
4344 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004345
Willy Tarreau0b748332014-04-29 00:13:29 +02004346 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4347 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004348
Willy Tarreau0b748332014-04-29 00:13:29 +02004349 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004350 goto done;
4351
Willy Tarreau0b748332014-04-29 00:13:29 +02004352 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4353 goto return_bad_req;
4354 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004355 }
4356
Willy Tarreau52542592014-04-28 18:33:26 +02004357 /* OK at this stage, we know that the request was accepted according to
4358 * the http-request rules, we can check for the stats. Note that the
4359 * URI is detected *before* the req* rules in order not to be affected
4360 * by a possible reqrep, while they are processed *after* so that a
4361 * reqdeny can still block them. This clearly needs to change in 1.6!
4362 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004363 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004364 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004365 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004366 txn->status = 500;
4367 s->logs.tv_request = now;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004368 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004369
Willy Tarreaue7dff022015-04-03 01:14:29 +02004370 if (!(s->flags & SF_ERR_MASK))
4371 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004372 goto return_prx_cond;
4373 }
4374
4375 /* parse the whole stats request and extract the relevant information */
4376 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004377 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004378 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004379
Willy Tarreau0b748332014-04-29 00:13:29 +02004380 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4381 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004382
Willy Tarreau0b748332014-04-29 00:13:29 +02004383 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4384 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004385 }
4386
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004387 /* evaluate the req* rules except reqadd */
4388 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004389 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004390 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004391
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004392 if (txn->flags & TX_CLDENY)
4393 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004394
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004395 if (txn->flags & TX_CLTARPIT)
4396 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004397 }
Willy Tarreau06619262006-12-17 08:37:22 +01004398
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004399 /* add request headers from the rule sets in the same order */
4400 list_for_each_entry(wl, &px->req_add, list) {
4401 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004402 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004403 ret = acl_pass(ret);
4404 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4405 ret = !ret;
4406 if (!ret)
4407 continue;
4408 }
4409
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004410 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004411 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004412 }
4413
Willy Tarreau52542592014-04-28 18:33:26 +02004414
4415 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004416 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004417 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004418 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4419 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004420
Willy Tarreaue7dff022015-04-03 01:14:29 +02004421 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4422 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4423 if (!(s->flags & SF_FINST_MASK))
4424 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004425
Willy Tarreau70730dd2014-04-24 18:06:27 +02004426 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004427 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004428 select_compression_request_header(s, req->buf);
4429
4430 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004431 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004432 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004433 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004434
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004435 /* check whether we have some ACLs set to redirect this request */
4436 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004437 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004438 int ret;
4439
Willy Tarreau192252e2015-04-04 01:47:55 +02004440 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004441 ret = acl_pass(ret);
4442 if (rule->cond->pol == ACL_COND_UNLESS)
4443 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004444 if (!ret)
4445 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004446 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004447 if (!http_apply_redirect_rule(rule, s, txn))
4448 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004449 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004450 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004451
Willy Tarreau2be39392010-01-03 17:24:51 +01004452 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4453 * If this happens, then the data will not come immediately, so we must
4454 * send all what we have without waiting. Note that due to the small gain
4455 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004456 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004457 * itself once used.
4458 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004459 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004460
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004461 done: /* done with this analyser, continue with next ones that the calling
4462 * points will have set, if any.
4463 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004464 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004465 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4466 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004467 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004468
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004469 tarpit:
4470 /* When a connection is tarpitted, we use the tarpit timeout,
4471 * which may be the same as the connect timeout if unspecified.
4472 * If unset, then set it to zero because we really want it to
4473 * eventually expire. We build the tarpit as an analyser.
4474 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004475 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004476
4477 /* wipe the request out so that we can drop the connection early
4478 * if the client closes first.
4479 */
4480 channel_dont_connect(req);
4481 req->analysers = 0; /* remove switching rules etc... */
4482 req->analysers |= AN_REQ_HTTP_TARPIT;
4483 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4484 if (!req->analyse_exp)
4485 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004486 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004487 sess->fe->fe_counters.denied_req++;
4488 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004489 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004490 if (sess->listener->counters)
4491 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004492 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004493
4494 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004495 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004496 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004497 s->logs.tv_request = now;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004498 http_reply_and_close(s, txn->status, http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004499 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004500 sess->fe->fe_counters.denied_req++;
4501 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004502 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004503 if (sess->listener->counters)
4504 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004505 goto return_prx_cond;
4506
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004507 return_bad_req:
4508 /* We centralize bad requests processing here */
4509 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4510 /* we detected a parsing error. We want to archive this request
4511 * in the dedicated proxy area for later troubleshooting.
4512 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004513 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004514 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004515
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004516 txn->req.msg_state = HTTP_MSG_ERROR;
4517 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004518 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004519
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004520 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004521 if (sess->listener->counters)
4522 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004523
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004524 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004525 if (!(s->flags & SF_ERR_MASK))
4526 s->flags |= SF_ERR_PRXCOND;
4527 if (!(s->flags & SF_FINST_MASK))
4528 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004529
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004530 req->analysers = 0;
4531 req->analyse_exp = TICK_ETERNITY;
4532 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004533
4534 return_prx_yield:
4535 channel_dont_connect(req);
4536 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004537}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004538
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004539/* This function performs all the processing enabled for the current request.
4540 * It returns 1 if the processing can continue on next analysers, or zero if it
4541 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004542 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004543 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004544int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004545{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004546 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004547 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004548 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004549 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004550
Willy Tarreau655dce92009-11-08 13:10:58 +01004551 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004552 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004553 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004554 return 0;
4555 }
4556
Willy Tarreau87b09662015-04-03 00:22:06 +02004557 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004558 now_ms, __FUNCTION__,
4559 s,
4560 req,
4561 req->rex, req->wex,
4562 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004563 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004564 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004565
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004566 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004567 select_compression_request_header(s, req->buf);
4568
Willy Tarreau59234e92008-11-30 23:51:27 +01004569 /*
4570 * Right now, we know that we have processed the entire headers
4571 * and that unwanted requests have been filtered out. We can do
4572 * whatever we want with the remaining request. Also, now we
4573 * may have separate values for ->fe, ->be.
4574 */
Willy Tarreau06619262006-12-17 08:37:22 +01004575
Willy Tarreau59234e92008-11-30 23:51:27 +01004576 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004577 * If HTTP PROXY is set we simply get remote server address parsing
4578 * incoming request. Note that this requires that a connection is
4579 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004580 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004581 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004582 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004583 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004584
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004585 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004586 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004587 txn->req.msg_state = HTTP_MSG_ERROR;
4588 txn->status = 500;
4589 req->analysers = 0;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004590 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004591
Willy Tarreaue7dff022015-04-03 01:14:29 +02004592 if (!(s->flags & SF_ERR_MASK))
4593 s->flags |= SF_ERR_RESOURCE;
4594 if (!(s->flags & SF_FINST_MASK))
4595 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004596
4597 return 0;
4598 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004599
4600 path = http_get_path(txn);
4601 url2sa(req->buf->p + msg->sl.rq.u,
4602 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004603 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004604 /* if the path was found, we have to remove everything between
4605 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4606 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4607 * u_l characters by a single "/".
4608 */
4609 if (path) {
4610 char *cur_ptr = req->buf->p;
4611 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4612 int delta;
4613
4614 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4615 http_msg_move_end(&txn->req, delta);
4616 cur_end += delta;
4617 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4618 goto return_bad_req;
4619 }
4620 else {
4621 char *cur_ptr = req->buf->p;
4622 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4623 int delta;
4624
4625 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4626 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4627 http_msg_move_end(&txn->req, delta);
4628 cur_end += delta;
4629 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4630 goto return_bad_req;
4631 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004632 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004633
Willy Tarreau59234e92008-11-30 23:51:27 +01004634 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004635 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004636 * Note that doing so might move headers in the request, but
4637 * the fields will stay coherent and the URI will not move.
4638 * This should only be performed in the backend.
4639 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02004640 if ((s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004641 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4642 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004643
William Lallemanda73203e2012-03-12 12:48:57 +01004644 /* add unique-id if "header-unique-id" is specified */
4645
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004646 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004647 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4648 goto return_bad_req;
4649 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004650 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004651 }
William Lallemanda73203e2012-03-12 12:48:57 +01004652
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004653 if (sess->fe->header_unique_id && s->unique_id) {
4654 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004655 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004656 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004657 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004658 goto return_bad_req;
4659 }
4660
Cyril Bontéb21570a2009-11-29 20:04:48 +01004661 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004662 * 9: add X-Forwarded-For if either the frontend or the backend
4663 * asks for it.
4664 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004665 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004666 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004667 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4668 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4669 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004670 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004671 /* The header is set to be added only if none is present
4672 * and we found it, so don't do anything.
4673 */
4674 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004675 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004676 /* Add an X-Forwarded-For header unless the source IP is
4677 * in the 'except' network range.
4678 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004679 if ((!sess->fe->except_mask.s_addr ||
4680 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4681 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004682 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004683 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004684 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004685 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004686 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004687 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004688
4689 /* Note: we rely on the backend to get the header name to be used for
4690 * x-forwarded-for, because the header is really meant for the backends.
4691 * However, if the backend did not specify any option, we have to rely
4692 * on the frontend's header name.
4693 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004694 if (s->be->fwdfor_hdr_len) {
4695 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004696 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004697 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004698 len = sess->fe->fwdfor_hdr_len;
4699 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004700 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004701 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01004702
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004703 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004704 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004705 }
4706 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004707 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004708 /* FIXME: for the sake of completeness, we should also support
4709 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004710 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004711 int len;
4712 char pn[INET6_ADDRSTRLEN];
4713 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004714 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004715 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004716
Willy Tarreau59234e92008-11-30 23:51:27 +01004717 /* Note: we rely on the backend to get the header name to be used for
4718 * x-forwarded-for, because the header is really meant for the backends.
4719 * However, if the backend did not specify any option, we have to rely
4720 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004721 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004722 if (s->be->fwdfor_hdr_len) {
4723 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004724 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004725 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004726 len = sess->fe->fwdfor_hdr_len;
4727 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004728 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004729 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004730
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004731 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004732 goto return_bad_req;
4733 }
4734 }
4735
4736 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004737 * 10: add X-Original-To if either the frontend or the backend
4738 * asks for it.
4739 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004740 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004741
4742 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004743 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004744 /* Add an X-Original-To header unless the destination IP is
4745 * in the 'except' network range.
4746 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004747 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004748
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004749 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004750 ((!sess->fe->except_mask_to.s_addr ||
4751 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4752 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004753 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004754 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02004755 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004756 int len;
4757 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004758 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004759
4760 /* Note: we rely on the backend to get the header name to be used for
4761 * x-original-to, because the header is really meant for the backends.
4762 * However, if the backend did not specify any option, we have to rely
4763 * on the frontend's header name.
4764 */
4765 if (s->be->orgto_hdr_len) {
4766 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004767 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004768 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004769 len = sess->fe->orgto_hdr_len;
4770 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004771 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004772 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004773
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004774 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004775 goto return_bad_req;
4776 }
4777 }
4778 }
4779
Willy Tarreau50fc7772012-11-11 22:19:57 +01004780 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4781 * If an "Upgrade" token is found, the header is left untouched in order not to have
4782 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4783 * "Upgrade" is present in the Connection header.
4784 */
4785 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4786 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004787 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004788 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004789 unsigned int want_flags = 0;
4790
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004791 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004792 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004793 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004794 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004795 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004796 want_flags |= TX_CON_CLO_SET;
4797 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004798 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004799 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004800 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004801 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004802 want_flags |= TX_CON_KAL_SET;
4803 }
4804
4805 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004806 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004807 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004808
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004809
Willy Tarreau522d6c02009-12-06 18:49:18 +01004810 /* If we have no server assigned yet and we're balancing on url_param
4811 * with a POST request, we may be interested in checking the body for
4812 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004813 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004814 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004815 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004816 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004817 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004818 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004819 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004820
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004821 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004822 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004823#ifdef TCP_QUICKACK
4824 /* We expect some data from the client. Unless we know for sure
4825 * we already have a full request, we have to re-enable quick-ack
4826 * in case we previously disabled it, otherwise we might cause
4827 * the client to delay further data.
4828 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004829 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004830 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004831 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004832 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004833 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004834#endif
4835 }
Willy Tarreau03945942009-12-22 16:50:27 +01004836
Willy Tarreau59234e92008-11-30 23:51:27 +01004837 /*************************************************************
4838 * OK, that's finished for the headers. We have done what we *
4839 * could. Let's switch to the DATA state. *
4840 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004841 req->analyse_exp = TICK_ETERNITY;
4842 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004843
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004844 /* if the server closes the connection, we want to immediately react
4845 * and close the socket to save packets and syscalls.
4846 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004847 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004848 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004849
Willy Tarreau59234e92008-11-30 23:51:27 +01004850 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004851 /* OK let's go on with the BODY now */
4852 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004853
Willy Tarreau59234e92008-11-30 23:51:27 +01004854 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004855 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004856 /* we detected a parsing error. We want to archive this request
4857 * in the dedicated proxy area for later troubleshooting.
4858 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004859 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004860 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004861
Willy Tarreau59234e92008-11-30 23:51:27 +01004862 txn->req.msg_state = HTTP_MSG_ERROR;
4863 txn->status = 400;
4864 req->analysers = 0;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004865 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004866
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004867 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004868 if (sess->listener->counters)
4869 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004870
Willy Tarreaue7dff022015-04-03 01:14:29 +02004871 if (!(s->flags & SF_ERR_MASK))
4872 s->flags |= SF_ERR_PRXCOND;
4873 if (!(s->flags & SF_FINST_MASK))
4874 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004875 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004876}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004877
Willy Tarreau60b85b02008-11-30 23:28:40 +01004878/* This function is an analyser which processes the HTTP tarpit. It always
4879 * returns zero, at the beginning because it prevents any other processing
4880 * from occurring, and at the end because it terminates the request.
4881 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004882int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004883{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004884 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004885
4886 /* This connection is being tarpitted. The CLIENT side has
4887 * already set the connect expiration date to the right
4888 * timeout. We just have to check that the client is still
4889 * there and that the timeout has not expired.
4890 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004891 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004892 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004893 !tick_is_expired(req->analyse_exp, now_ms))
4894 return 0;
4895
4896 /* We will set the queue timer to the time spent, just for
4897 * logging purposes. We fake a 500 server error, so that the
4898 * attacker will not suspect his connection has been tarpitted.
4899 * It will not cause trouble to the logs because we can exclude
4900 * the tarpitted connections by filtering on the 'PT' status flags.
4901 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004902 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4903
4904 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004905 if (!(req->flags & CF_READ_ERROR))
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004906 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004907
4908 req->analysers = 0;
4909 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004910
Willy Tarreaue7dff022015-04-03 01:14:29 +02004911 if (!(s->flags & SF_ERR_MASK))
4912 s->flags |= SF_ERR_PRXCOND;
4913 if (!(s->flags & SF_FINST_MASK))
4914 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004915 return 0;
4916}
4917
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004918/* This function is an analyser which waits for the HTTP request body. It waits
4919 * for either the buffer to be full, or the full advertised contents to have
4920 * reached the buffer. It must only be called after the standard HTTP request
4921 * processing has occurred, because it expects the request to be parsed and will
4922 * look for the Expect header. It may send a 100-Continue interim response. It
4923 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4924 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4925 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004926 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004927int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004928{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004929 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004930 struct http_txn *txn = s->txn;
4931 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004932
4933 /* We have to parse the HTTP request body to find any required data.
4934 * "balance url_param check_post" should have been the only way to get
4935 * into this. We were brought here after HTTP header analysis, so all
4936 * related structures are ready.
4937 */
4938
Willy Tarreau890988f2014-04-10 11:59:33 +02004939 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4940 /* This is the first call */
4941 if (msg->msg_state < HTTP_MSG_BODY)
4942 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004943
Willy Tarreau890988f2014-04-10 11:59:33 +02004944 if (msg->msg_state < HTTP_MSG_100_SENT) {
4945 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4946 * send an HTTP/1.1 100 Continue intermediate response.
4947 */
4948 if (msg->flags & HTTP_MSGF_VER_11) {
4949 struct hdr_ctx ctx;
4950 ctx.idx = 0;
4951 /* Expect is allowed in 1.1, look for it */
4952 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4953 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004954 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004955 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004956 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004957 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004958 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004959
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004960 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004961 * req->buf->p still points to the beginning of the message. We
4962 * must save the body in msg->next because it survives buffer
4963 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004964 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004965 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004966
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004967 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004968 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4969 else
4970 msg->msg_state = HTTP_MSG_DATA;
4971 }
4972
Willy Tarreau890988f2014-04-10 11:59:33 +02004973 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4974 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004975 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004976 goto missing_data;
4977
4978 /* OK we have everything we need now */
4979 goto http_end;
4980 }
4981
4982 /* OK here we're parsing a chunked-encoded message */
4983
Willy Tarreau522d6c02009-12-06 18:49:18 +01004984 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004985 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004986 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004987 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004988 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004989 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004990
Willy Tarreau115acb92009-12-26 13:56:06 +01004991 if (!ret)
4992 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004993 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004994 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004995 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004996 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004997 }
4998
Willy Tarreaud98cf932009-12-27 22:54:55 +01004999 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02005000 * We have the first data byte is in msg->sov + msg->sol. We're waiting
5001 * for at least a whole chunk or the whole content length bytes after
5002 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01005003 */
Willy Tarreau890988f2014-04-10 11:59:33 +02005004 if (msg->msg_state == HTTP_MSG_TRAILERS)
5005 goto http_end;
5006
Willy Tarreaue115b492015-05-01 23:05:14 +02005007 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01005008 goto http_end;
5009
5010 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02005011 /* we get here if we need to wait for more data. If the buffer is full,
5012 * we have the maximum we can expect.
5013 */
5014 if (buffer_full(req->buf, global.tune.maxrewrite))
5015 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01005016
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005017 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01005018 txn->status = 408;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005019 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02005020
Willy Tarreaue7dff022015-04-03 01:14:29 +02005021 if (!(s->flags & SF_ERR_MASK))
5022 s->flags |= SF_ERR_CLITO;
5023 if (!(s->flags & SF_FINST_MASK))
5024 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005025 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005026 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005027
5028 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005029 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005030 /* Not enough data. We'll re-use the http-request
5031 * timeout here. Ideally, we should set the timeout
5032 * relative to the accept() date. We just set the
5033 * request timeout once at the beginning of the
5034 * request.
5035 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005036 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005037 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005038 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005039 return 0;
5040 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005041
5042 http_end:
5043 /* The situation will not evolve, so let's give up on the analysis. */
5044 s->logs.tv_request = now; /* update the request timer to reflect full request */
5045 req->analysers &= ~an_bit;
5046 req->analyse_exp = TICK_ETERNITY;
5047 return 1;
5048
5049 return_bad_req: /* let's centralize all bad requests */
5050 txn->req.msg_state = HTTP_MSG_ERROR;
5051 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005052 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005053
Willy Tarreaue7dff022015-04-03 01:14:29 +02005054 if (!(s->flags & SF_ERR_MASK))
5055 s->flags |= SF_ERR_PRXCOND;
5056 if (!(s->flags & SF_FINST_MASK))
5057 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005058
Willy Tarreau522d6c02009-12-06 18:49:18 +01005059 return_err_msg:
5060 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005061 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005062 if (sess->listener->counters)
5063 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005064 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005065}
5066
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005067/* send a server's name with an outgoing request over an established connection.
5068 * Note: this function is designed to be called once the request has been scheduled
5069 * for being forwarded. This is the reason why it rewinds the buffer before
5070 * proceeding.
5071 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005072int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005073
5074 struct hdr_ctx ctx;
5075
Mark Lamourinec2247f02012-01-04 13:02:01 -05005076 char *hdr_name = be->server_id_hdr_name;
5077 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005078 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005079 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005080 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005081
William Lallemandd9e90662012-01-30 17:27:17 +01005082 ctx.idx = 0;
5083
Willy Tarreau211cdec2014-04-17 20:18:08 +02005084 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005085 if (old_o) {
5086 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005087 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005088 txn->req.next += old_o;
5089 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005090 }
5091
Willy Tarreau9b28e032012-10-12 23:49:43 +02005092 old_i = chn->buf->i;
5093 while (http_find_header2(hdr_name, hdr_name_len, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005094 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005095 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005096 }
5097
5098 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005099 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005100 memcpy(hdr_val, hdr_name, hdr_name_len);
5101 hdr_val += hdr_name_len;
5102 *hdr_val++ = ':';
5103 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005104 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5105 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005106
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005107 if (old_o) {
5108 /* If this was a forwarded request, we must readjust the amount of
5109 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005110 * variations. Note that the current state is >= HTTP_MSG_BODY,
5111 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005112 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005113 old_o += chn->buf->i - old_i;
5114 b_adv(chn->buf, old_o);
5115 txn->req.next -= old_o;
5116 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005117 }
5118
Mark Lamourinec2247f02012-01-04 13:02:01 -05005119 return 0;
5120}
5121
Willy Tarreau610ecce2010-01-04 21:15:02 +01005122/* Terminate current transaction and prepare a new one. This is very tricky
5123 * right now but it works.
5124 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005125void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005126{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005127 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005128 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01005129 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02005130 struct connection *srv_conn;
5131 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005132 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005133
Willy Tarreau610ecce2010-01-04 21:15:02 +01005134 /* FIXME: We need a more portable way of releasing a backend's and a
5135 * server's connections. We need a safer way to reinitialize buffer
5136 * flags. We also need a more accurate method for computing per-request
5137 * data.
5138 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005139 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005140
Willy Tarreau4213a112013-12-15 10:25:42 +01005141 /* unless we're doing keep-alive, we want to quickly close the connection
5142 * to the server.
5143 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005144 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005145 !si_conn_ready(&s->si[1])) {
5146 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5147 si_shutr(&s->si[1]);
5148 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005149 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005150
Willy Tarreaue7dff022015-04-03 01:14:29 +02005151 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01005152 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005153 if (unlikely(s->srv_conn))
5154 sess_change_server(s, NULL);
5155 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005156
5157 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005158 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005159
Willy Tarreaueee5b512015-04-03 23:46:31 +02005160 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005161 int n;
5162
Willy Tarreaueee5b512015-04-03 23:46:31 +02005163 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005164 if (n < 1 || n > 5)
5165 n = 0;
5166
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005167 if (fe->mode == PR_MODE_HTTP) {
5168 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005169 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005170 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005171 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005172 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01005173 (be->mode == PR_MODE_HTTP)) {
5174 be->be_counters.p.http.rsp[n]++;
5175 be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005176 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau858b1032015-12-07 17:04:59 +01005177 be->be_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005178 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005179 }
5180
5181 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005182 s->logs.bytes_in -= s->req.buf->i;
5183 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005184
5185 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005186 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005187 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005188 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005189 s->do_log(s);
5190 }
5191
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005192 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005193 stream_stop_content_counters(s);
5194 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005195
Willy Tarreau610ecce2010-01-04 21:15:02 +01005196 s->logs.accept_date = date; /* user-visible date for logging */
5197 s->logs.tv_accept = now; /* corrected date for internal use */
5198 tv_zero(&s->logs.tv_request);
5199 s->logs.t_queue = -1;
5200 s->logs.t_connect = -1;
5201 s->logs.t_data = -1;
5202 s->logs.t_close = 0;
5203 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5204 s->logs.srv_queue_size = 0; /* we will get this number soon */
5205
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005206 s->logs.bytes_in = s->req.total = s->req.buf->i;
5207 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005208
5209 if (s->pend_pos)
5210 pendconn_free(s->pend_pos);
5211
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005212 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005213 if (s->flags & SF_CURR_SESS) {
5214 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005215 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005216 }
Willy Tarreau858b1032015-12-07 17:04:59 +01005217 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005218 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005219 }
5220
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005221 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005222
Willy Tarreau4213a112013-12-15 10:25:42 +01005223 /* only release our endpoint if we don't intend to reuse the
5224 * connection.
5225 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005226 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005227 !si_conn_ready(&s->si[1])) {
5228 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005229 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005230 }
5231
Willy Tarreau350f4872014-11-28 14:42:25 +01005232 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5233 s->si[1].err_type = SI_ET_NONE;
5234 s->si[1].conn_retries = 0; /* used for logging too */
5235 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005236 s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_stream */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005237 s->req.flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WAKE_CONNECT|CF_WROTE_DATA);
5238 s->res.flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA);
Willy Tarreaue7dff022015-04-03 01:14:29 +02005239 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5240 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5241 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005242
Willy Tarreaueee5b512015-04-03 23:46:31 +02005243 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005244 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005245 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005246
5247 if (prev_status == 401 || prev_status == 407) {
5248 /* In HTTP keep-alive mode, if we receive a 401, we still have
5249 * a chance of being able to send the visitor again to the same
5250 * server over the same connection. This is required by some
5251 * broken protocols such as NTLM, and anyway whenever there is
5252 * an opportunity for sending the challenge to the proper place,
5253 * it's better to do it (at least it helps with debugging).
5254 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005255 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005256 if (srv_conn)
5257 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005258 }
5259
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005260 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005261 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005262
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005263 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005264 s->req.flags |= CF_NEVER_WAIT;
5265 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005266 }
5267
Willy Tarreau610ecce2010-01-04 21:15:02 +01005268 /* if the request buffer is not empty, it means we're
5269 * about to process another request, so send pending
5270 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005271 * Just don't do this if the buffer is close to be full,
5272 * because the request will wait for it to flush a little
5273 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005274 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005275 if (s->req.buf->i) {
5276 if (s->res.buf->o &&
5277 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5278 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5279 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005280 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005281
Willy Tarreau714ea782015-11-25 20:11:11 +01005282 /* we're removing the analysers, we MUST re-enable events detection.
5283 * We don't enable close on the response channel since it's either
5284 * already closed, or in keep-alive with an idle connection handler.
5285 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005286 channel_auto_read(&s->req);
5287 channel_auto_close(&s->req);
5288 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005289
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005290 /* we're in keep-alive with an idle connection, monitor it if not already done */
5291 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005292 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005293 if (!srv)
5294 si_idle_conn(&s->si[1], NULL);
5295 else if ((srv_conn->flags & CO_FL_PRIVATE) ||
Willy Tarreau858b1032015-12-07 17:04:59 +01005296 ((be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR))
Willy Tarreau8dff9982015-08-04 20:45:52 +02005297 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005298 else if (prev_flags & TX_NOT_FIRST)
5299 /* note: we check the request, not the connection, but
5300 * this is valid for strategies SAFE and AGGR, and in
5301 * case of ALWS, we don't care anyway.
5302 */
5303 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005304 else
5305 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005306 }
Willy Tarreau27375622013-12-17 00:00:28 +01005307
Willy Tarreauc29d0cd2015-09-23 12:18:14 +02005308 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005309 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005310}
5311
5312
5313/* This function updates the request state machine according to the response
5314 * state machine and buffer flags. It returns 1 if it changes anything (flag
5315 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5316 * it is only used to find when a request/response couple is complete. Both
5317 * this function and its equivalent should loop until both return zero. It
5318 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5319 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005320int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005321{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005322 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005323 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005324 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005325 unsigned int old_state = txn->req.msg_state;
5326
Willy Tarreau610ecce2010-01-04 21:15:02 +01005327 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5328 return 0;
5329
5330 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005331 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005332 * We can shut the read side unless we want to abort_on_close,
5333 * or we have a POST request. The issue with POST requests is
5334 * that some browsers still send a CRLF after the request, and
5335 * this CRLF must be read so that it does not remain in the kernel
5336 * buffers, otherwise a close could cause an RST on some systems
5337 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005338 * Note that if we're using keep-alive on the client side, we'd
5339 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005340 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005341 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005342 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005343 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5344 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5345 !(s->be->options & PR_O_ABRT_CLOSE) &&
5346 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005347 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005348
Willy Tarreau40f151a2012-12-20 12:10:09 +01005349 /* if the server closes the connection, we want to immediately react
5350 * and close the socket to save packets and syscalls.
5351 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005352 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005353
Willy Tarreau7f876a12015-11-18 11:59:55 +01005354 /* In any case we've finished parsing the request so we must
5355 * disable Nagle when sending data because 1) we're not going
5356 * to shut this side, and 2) the server is waiting for us to
5357 * send pending data.
5358 */
5359 chn->flags |= CF_NEVER_WAIT;
5360
Willy Tarreau610ecce2010-01-04 21:15:02 +01005361 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5362 goto wait_other_side;
5363
5364 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5365 /* The server has not finished to respond, so we
5366 * don't want to move in order not to upset it.
5367 */
5368 goto wait_other_side;
5369 }
5370
5371 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5372 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005373 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005374 txn->req.msg_state = HTTP_MSG_TUNNEL;
5375 goto wait_other_side;
5376 }
5377
5378 /* When we get here, it means that both the request and the
5379 * response have finished receiving. Depending on the connection
5380 * mode, we'll have to wait for the last bytes to leave in either
5381 * direction, and sometimes for a close to be effective.
5382 */
5383
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005384 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5385 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005386 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5387 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005388 }
5389 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5390 /* Option forceclose is set, or either side wants to close,
5391 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005392 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005393 * once both states are CLOSED.
5394 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005395 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5396 channel_shutr_now(chn);
5397 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005398 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005399 }
5400 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005401 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5402 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005403 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005404 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5405 channel_auto_read(chn);
5406 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005407 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005408 }
5409
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005410 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005411 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005412 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005413
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005414 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005415 txn->req.msg_state = HTTP_MSG_CLOSING;
5416 goto http_msg_closing;
5417 }
5418 else {
5419 txn->req.msg_state = HTTP_MSG_CLOSED;
5420 goto http_msg_closed;
5421 }
5422 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005423 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005424 }
5425
5426 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5427 http_msg_closing:
5428 /* nothing else to forward, just waiting for the output buffer
5429 * to be empty and for the shutw_now to take effect.
5430 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005431 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005432 txn->req.msg_state = HTTP_MSG_CLOSED;
5433 goto http_msg_closed;
5434 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005435 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005436 txn->req.msg_state = HTTP_MSG_ERROR;
5437 goto wait_other_side;
5438 }
5439 }
5440
5441 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5442 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005443 /* see above in MSG_DONE why we only do this in these states */
5444 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5445 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5446 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005447 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005448 goto wait_other_side;
5449 }
5450
5451 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005452 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005453}
5454
5455
5456/* This function updates the response state machine according to the request
5457 * state machine and buffer flags. It returns 1 if it changes anything (flag
5458 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5459 * it is only used to find when a request/response couple is complete. Both
5460 * this function and its equivalent should loop until both return zero. It
5461 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5462 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005463int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005464{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005465 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005466 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005467 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005468 unsigned int old_state = txn->rsp.msg_state;
5469
Willy Tarreau610ecce2010-01-04 21:15:02 +01005470 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5471 return 0;
5472
5473 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5474 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005475 * still monitor the server connection for a possible close
5476 * while the request is being uploaded, so we don't disable
5477 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005478 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005479 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005480
5481 if (txn->req.msg_state == HTTP_MSG_ERROR)
5482 goto wait_other_side;
5483
5484 if (txn->req.msg_state < HTTP_MSG_DONE) {
5485 /* The client seems to still be sending data, probably
5486 * because we got an error response during an upload.
5487 * We have the choice of either breaking the connection
5488 * or letting it pass through. Let's do the later.
5489 */
5490 goto wait_other_side;
5491 }
5492
5493 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5494 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005495 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005496 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005497 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005498 goto wait_other_side;
5499 }
5500
5501 /* When we get here, it means that both the request and the
5502 * response have finished receiving. Depending on the connection
5503 * mode, we'll have to wait for the last bytes to leave in either
5504 * direction, and sometimes for a close to be effective.
5505 */
5506
5507 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5508 /* Server-close mode : shut read and wait for the request
5509 * side to close its output buffer. The caller will detect
5510 * when we're in DONE and the other is in CLOSED and will
5511 * catch that for the final cleanup.
5512 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005513 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5514 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005515 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005516 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5517 /* Option forceclose is set, or either side wants to close,
5518 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005519 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005520 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005521 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005522 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5523 channel_shutr_now(chn);
5524 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005525 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005526 }
5527 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005528 /* The last possible modes are keep-alive and tunnel. Tunnel will
5529 * need to forward remaining data. Keep-alive will need to monitor
5530 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005531 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005532 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005533 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005534 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5535 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005536 }
5537
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005538 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005539 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005540 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005541 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5542 goto http_msg_closing;
5543 }
5544 else {
5545 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5546 goto http_msg_closed;
5547 }
5548 }
5549 goto wait_other_side;
5550 }
5551
5552 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5553 http_msg_closing:
5554 /* nothing else to forward, just waiting for the output buffer
5555 * to be empty and for the shutw_now to take effect.
5556 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005557 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005558 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5559 goto http_msg_closed;
5560 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005561 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005562 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005563 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005564 if (objt_server(s->target))
5565 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005566 goto wait_other_side;
5567 }
5568 }
5569
5570 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5571 http_msg_closed:
5572 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005573 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005574 channel_auto_close(chn);
5575 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005576 goto wait_other_side;
5577 }
5578
5579 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005580 /* We force the response to leave immediately if we're waiting for the
5581 * other side, since there is no pending shutdown to push it out.
5582 */
5583 if (!channel_is_empty(chn))
5584 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005585 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005586}
5587
5588
5589/* Resync the request and response state machines. Return 1 if either state
5590 * changes.
5591 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005592int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005593{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005594 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005595 int old_req_state = txn->req.msg_state;
5596 int old_res_state = txn->rsp.msg_state;
5597
Willy Tarreau610ecce2010-01-04 21:15:02 +01005598 http_sync_req_state(s);
5599 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005600 if (!http_sync_res_state(s))
5601 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005602 if (!http_sync_req_state(s))
5603 break;
5604 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005605
Willy Tarreau610ecce2010-01-04 21:15:02 +01005606 /* OK, both state machines agree on a compatible state.
5607 * There are a few cases we're interested in :
5608 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5609 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5610 * directions, so let's simply disable both analysers.
5611 * - HTTP_MSG_CLOSED on the response only means we must abort the
5612 * request.
5613 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5614 * with server-close mode means we've completed one request and we
5615 * must re-initialize the server connection.
5616 */
5617
5618 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5619 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5620 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5621 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005622 s->req.analysers = 0;
5623 channel_auto_close(&s->req);
5624 channel_auto_read(&s->req);
5625 s->res.analysers = 0;
5626 channel_auto_close(&s->res);
5627 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005628 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005629 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005630 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005631 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005632 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005633 s->res.analysers = 0;
5634 channel_auto_close(&s->res);
5635 channel_auto_read(&s->res);
5636 s->req.analysers = 0;
5637 channel_abort(&s->req);
5638 channel_auto_close(&s->req);
5639 channel_auto_read(&s->req);
5640 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005641 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005642 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5643 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005644 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005645 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5646 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5647 /* server-close/keep-alive: terminate this transaction,
5648 * possibly killing the server connection and reinitialize
5649 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005650 */
5651 http_end_txn_clean_session(s);
5652 }
5653
Willy Tarreau610ecce2010-01-04 21:15:02 +01005654 return txn->req.msg_state != old_req_state ||
5655 txn->rsp.msg_state != old_res_state;
5656}
5657
Willy Tarreaud98cf932009-12-27 22:54:55 +01005658/* This function is an analyser which forwards request body (including chunk
5659 * sizes if any). It is called as soon as we must forward, even if we forward
5660 * zero byte. The only situation where it must not be called is when we're in
5661 * tunnel mode and we want to forward till the close. It's used both to forward
5662 * remaining data and to resync after end of body. It expects the msg_state to
5663 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005664 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005665 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005666 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005667 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005668int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005669{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005670 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005671 struct http_txn *txn = s->txn;
5672 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005673
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005674 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5675 return 0;
5676
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005677 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005678 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005679 /* Output closed while we were sending data. We must abort and
5680 * wake the other side up.
5681 */
5682 msg->msg_state = HTTP_MSG_ERROR;
5683 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005684 return 1;
5685 }
5686
Willy Tarreaud98cf932009-12-27 22:54:55 +01005687 /* Note that we don't have to send 100-continue back because we don't
5688 * need the data to complete our job, and it's up to the server to
5689 * decide whether to return 100, 417 or anything else in return of
5690 * an "Expect: 100-continue" header.
5691 */
5692
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005693 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005694 /* we have msg->sov which points to the first byte of message
5695 * body, and req->buf.p still points to the beginning of the
5696 * message. We forward the headers now, as we don't need them
5697 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005698 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005699 b_adv(req->buf, msg->sov);
5700 msg->next -= msg->sov;
5701 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005702
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005703 /* The previous analysers guarantee that the state is somewhere
5704 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5705 * msg->next are always correct.
5706 */
5707 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5708 if (msg->flags & HTTP_MSGF_TE_CHNK)
5709 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5710 else
5711 msg->msg_state = HTTP_MSG_DATA;
5712 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005713 }
5714
Willy Tarreau7ba23542014-04-17 21:50:00 +02005715 /* Some post-connect processing might want us to refrain from starting to
5716 * forward data. Currently, the only reason for this is "balance url_param"
5717 * whichs need to parse/process the request after we've enabled forwarding.
5718 */
5719 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005720 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005721 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005722 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005723 goto missing_data;
5724 }
5725 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5726 }
5727
Willy Tarreau80a92c02014-03-12 10:41:13 +01005728 /* in most states, we should abort in case of early close */
5729 channel_auto_close(req);
5730
Willy Tarreauefdf0942014-04-24 20:08:57 +02005731 if (req->to_forward) {
5732 /* We can't process the buffer's contents yet */
5733 req->flags |= CF_WAKE_WRITE;
5734 goto missing_data;
5735 }
5736
Willy Tarreaud98cf932009-12-27 22:54:55 +01005737 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005738 if (msg->msg_state == HTTP_MSG_DATA) {
5739 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005740 /* we may have some pending data starting at req->buf->p */
5741 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005742 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005743 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005744 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005745 msg->next += msg->chunk_len;
5746 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005747
5748 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005749 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005750 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005751 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005752 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005753 }
5754 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005755 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005756 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005757 * TRAILERS state.
5758 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005759 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005760
Willy Tarreau54d23df2012-10-25 19:04:45 +02005761 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005762 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005763 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005764 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005765 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005766 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005767 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005768 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005769 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005770 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005771 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005772 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005773 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005774
5775 if (ret == 0)
5776 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005777 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005778 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005779 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005780 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005781 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005782 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005783 /* we're in MSG_CHUNK_SIZE now */
5784 }
5785 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005786 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005787
5788 if (ret == 0)
5789 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005790 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005791 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005792 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005793 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005794 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005795 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005796 /* we're in HTTP_MSG_DONE now */
5797 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005798 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005799 int old_state = msg->msg_state;
5800
Willy Tarreau610ecce2010-01-04 21:15:02 +01005801 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005802
5803 /* we may have some pending data starting at req->buf->p
5804 * such as last chunk of data or trailers.
5805 */
5806 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005807 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005808 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005809 msg->next = 0;
5810
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005811 /* we don't want to forward closes on DONE except in
5812 * tunnel mode.
5813 */
5814 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005815 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005816 if (http_resync_states(s)) {
5817 /* some state changes occurred, maybe the analyser
5818 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005819 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005820 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005821 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005822 /* request errors are most likely due to
5823 * the server aborting the transfer.
5824 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005825 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005826 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005827 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005828 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005829 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005830 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005831 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005832 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005833
5834 /* If "option abortonclose" is set on the backend, we
5835 * want to monitor the client's connection and forward
5836 * any shutdown notification to the server, which will
5837 * decide whether to close or to go on processing the
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005838 * request. We only do that in tunnel mode, and not in
5839 * other modes since it can be abused to exhaust source
5840 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005841 */
5842 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005843 channel_auto_read(req);
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005844 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5845 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5846 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005847 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005848 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005849 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005850 /* POST requests may require to read extra CRLF
5851 * sent by broken browsers and which could cause
5852 * an RST to be sent upon close on some systems
5853 * (eg: Linux).
5854 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005855 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005856 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005857
Willy Tarreau610ecce2010-01-04 21:15:02 +01005858 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005859 }
5860 }
5861
Willy Tarreaud98cf932009-12-27 22:54:55 +01005862 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005863 /* we may have some pending data starting at req->buf->p */
5864 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005865 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005866 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5867
Willy Tarreaubed410e2014-04-22 08:19:34 +02005868 msg->next = 0;
5869 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5870
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005871 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005872 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005873 if (!(s->flags & SF_ERR_MASK))
5874 s->flags |= SF_ERR_CLICL;
5875 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005876 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005877 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005878 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005879 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005880 }
5881
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005882 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005883 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005884 if (objt_server(s->target))
5885 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005886
5887 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005888 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005889
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005890 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005891 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005892 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005893
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005894 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005895 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005896 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005897 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005898 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005899
Willy Tarreau5c620922011-05-11 19:56:11 +02005900 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005901 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005902 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005903 * modes are already handled by the stream sock layer. We must not do
5904 * this in content-length mode because it could present the MSG_MORE
5905 * flag with the last block of forwarded data, which would cause an
5906 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005907 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005908 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005909 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005910
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005911 return 0;
5912
Willy Tarreaud98cf932009-12-27 22:54:55 +01005913 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005914 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005915 if (sess->listener->counters)
5916 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005917
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005918 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005919 /* we may have some pending data starting at req->buf->p */
5920 b_adv(req->buf, msg->next);
5921 msg->next = 0;
5922
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005923 txn->req.msg_state = HTTP_MSG_ERROR;
5924 if (txn->status) {
5925 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005926 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005927 } else {
5928 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005929 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005930 }
5931 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005932 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005933
Willy Tarreaue7dff022015-04-03 01:14:29 +02005934 if (!(s->flags & SF_ERR_MASK))
5935 s->flags |= SF_ERR_PRXCOND;
5936 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005937 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005938 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005939 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005940 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005941 }
5942 return 0;
5943
5944 aborted_xfer:
5945 txn->req.msg_state = HTTP_MSG_ERROR;
5946 if (txn->status) {
5947 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005948 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005949 } else {
5950 txn->status = 502;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005951 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005952 }
5953 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005954 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005955
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005956 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005957 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005958 if (objt_server(s->target))
5959 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005960
Willy Tarreaue7dff022015-04-03 01:14:29 +02005961 if (!(s->flags & SF_ERR_MASK))
5962 s->flags |= SF_ERR_SRVCL;
5963 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005964 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005965 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005966 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005967 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005968 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005969 return 0;
5970}
5971
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005972/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5973 * processing can continue on next analysers, or zero if it either needs more
5974 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005975 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005976 * when it has nothing left to do, and may remove any analyser when it wants to
5977 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005978 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005979int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005980{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005981 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005982 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005983 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005984 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005985 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005986 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005987 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005988
Willy Tarreau87b09662015-04-03 00:22:06 +02005989 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 +02005990 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005991 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005992 rep,
5993 rep->rex, rep->wex,
5994 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005995 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005996 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005997
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005998 /*
5999 * Now parse the partial (or complete) lines.
6000 * We will check the response syntax, and also join multi-line
6001 * headers. An index of all the lines will be elaborated while
6002 * parsing.
6003 *
6004 * For the parsing, we use a 28 states FSM.
6005 *
6006 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02006007 * rep->buf->p = beginning of response
6008 * rep->buf->p + msg->eoh = end of processed headers / start of current one
6009 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02006010 * msg->eol = end of current header or line (LF or CRLF)
6011 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006012 */
6013
Willy Tarreau628c40c2014-04-24 19:11:26 +02006014 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01006015 /* There's a protected area at the end of the buffer for rewriting
6016 * purposes. We don't want to start to parse the request if the
6017 * protected area is affected, because we may have to move processed
6018 * data later, which is much more complicated.
6019 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006020 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01006021 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02006022 /* some data has still not left the buffer, wake us once that's done */
6023 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
6024 goto abort_response;
6025 channel_dont_close(rep);
6026 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01006027 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02006028 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01006029 }
6030
Willy Tarreau379357a2013-06-08 12:55:46 +02006031 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
6032 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
6033 buffer_slow_realign(rep->buf);
6034
Willy Tarreau9b28e032012-10-12 23:49:43 +02006035 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01006036 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01006037 }
6038
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006039 /* 1: we might have to print this header in debug mode */
6040 if (unlikely((global.mode & MODE_DEBUG) &&
6041 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02006042 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006043 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006044
Willy Tarreau9b28e032012-10-12 23:49:43 +02006045 sol = rep->buf->p;
6046 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006047 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006048
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006049 sol += hdr_idx_first_pos(&txn->hdr_idx);
6050 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006051
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006052 while (cur_idx) {
6053 eol = sol + txn->hdr_idx.v[cur_idx].len;
6054 debug_hdr("srvhdr", s, sol, eol);
6055 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
6056 cur_idx = txn->hdr_idx.v[cur_idx].next;
6057 }
6058 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006059
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006060 /*
6061 * Now we quickly check if we have found a full valid response.
6062 * If not so, we check the FD and buffer states before leaving.
6063 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01006064 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006065 * responses are checked first.
6066 *
6067 * Depending on whether the client is still there or not, we
6068 * may send an error response back or not. Note that normally
6069 * we should only check for HTTP status there, and check I/O
6070 * errors somewhere else.
6071 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006072
Willy Tarreau655dce92009-11-08 13:10:58 +01006073 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006074 /* Invalid response */
6075 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6076 /* we detected a parsing error. We want to archive this response
6077 * in the dedicated proxy area for later troubleshooting.
6078 */
6079 hdr_response_bad:
6080 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006081 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006082
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006083 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006084 if (objt_server(s->target)) {
6085 objt_server(s->target)->counters.failed_resp++;
6086 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006087 }
Willy Tarreau64648412010-03-05 10:41:54 +01006088 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006089 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006090 rep->analysers = 0;
6091 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006092 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006093 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006094 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006095
Willy Tarreaue7dff022015-04-03 01:14:29 +02006096 if (!(s->flags & SF_ERR_MASK))
6097 s->flags |= SF_ERR_PRXCOND;
6098 if (!(s->flags & SF_FINST_MASK))
6099 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006100
6101 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006102 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006103
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006104 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006105 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006106 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006107 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006108 goto hdr_response_bad;
6109 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006110
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006111 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006112 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006113 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006114 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006115 else if (txn->flags & TX_NOT_FIRST)
6116 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006117
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006118 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006119 if (objt_server(s->target)) {
6120 objt_server(s->target)->counters.failed_resp++;
6121 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006122 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006123
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006124 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006125 rep->analysers = 0;
6126 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006127 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006128 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006129 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006130
Willy Tarreaue7dff022015-04-03 01:14:29 +02006131 if (!(s->flags & SF_ERR_MASK))
6132 s->flags |= SF_ERR_SRVCL;
6133 if (!(s->flags & SF_FINST_MASK))
6134 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006135 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006136 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006137
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006138 /* read timeout : return a 504 to the client. */
6139 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006140 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006141 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006142
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006143 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006144 if (objt_server(s->target)) {
6145 objt_server(s->target)->counters.failed_resp++;
6146 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006147 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006148
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006149 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006150 rep->analysers = 0;
6151 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006152 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006153 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006154 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006155
Willy Tarreaue7dff022015-04-03 01:14:29 +02006156 if (!(s->flags & SF_ERR_MASK))
6157 s->flags |= SF_ERR_SRVTO;
6158 if (!(s->flags & SF_FINST_MASK))
6159 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006160 return 0;
6161 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006162
Willy Tarreauf003d372012-11-26 13:35:37 +01006163 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006164 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006165 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006166 s->be->be_counters.cli_aborts++;
6167 if (objt_server(s->target))
6168 objt_server(s->target)->counters.cli_aborts++;
6169
6170 rep->analysers = 0;
6171 channel_auto_close(rep);
6172
6173 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006174 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006175 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006176
Willy Tarreaue7dff022015-04-03 01:14:29 +02006177 if (!(s->flags & SF_ERR_MASK))
6178 s->flags |= SF_ERR_CLICL;
6179 if (!(s->flags & SF_FINST_MASK))
6180 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006181
Willy Tarreau87b09662015-04-03 00:22:06 +02006182 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006183 return 0;
6184 }
6185
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006186 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006187 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006188 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006189 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006190 else if (txn->flags & TX_NOT_FIRST)
6191 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006192
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006193 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006194 if (objt_server(s->target)) {
6195 objt_server(s->target)->counters.failed_resp++;
6196 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006197 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006198
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006199 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006200 rep->analysers = 0;
6201 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006202 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006203 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006204 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006205
Willy Tarreaue7dff022015-04-03 01:14:29 +02006206 if (!(s->flags & SF_ERR_MASK))
6207 s->flags |= SF_ERR_SRVCL;
6208 if (!(s->flags & SF_FINST_MASK))
6209 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006210 return 0;
6211 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006212
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006213 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006214 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006215 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006216 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006217 else if (txn->flags & TX_NOT_FIRST)
6218 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006219
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006220 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006221 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006222 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006223
Willy Tarreaue7dff022015-04-03 01:14:29 +02006224 if (!(s->flags & SF_ERR_MASK))
6225 s->flags |= SF_ERR_CLICL;
6226 if (!(s->flags & SF_FINST_MASK))
6227 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006228
Willy Tarreau87b09662015-04-03 00:22:06 +02006229 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006230 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006231 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006232
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006233 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006234 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006235 return 0;
6236 }
6237
6238 /* More interesting part now : we know that we have a complete
6239 * response which at least looks like HTTP. We have an indicator
6240 * of each header's length, so we can parse them quickly.
6241 */
6242
6243 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006244 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006245
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006246 /*
6247 * 1: get the status code
6248 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006249 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006250 if (n < 1 || n > 5)
6251 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006252 /* when the client triggers a 4xx from the server, it's most often due
6253 * to a missing object or permission. These events should be tracked
6254 * because if they happen often, it may indicate a brute force or a
6255 * vulnerability scan.
6256 */
6257 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006258 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006259
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006260 if (objt_server(s->target))
6261 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006262
Willy Tarreau91852eb2015-05-01 13:26:00 +02006263 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6264 * exactly one digit "." one digit. This check may be disabled using
6265 * option accept-invalid-http-response.
6266 */
6267 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6268 if (msg->sl.st.v_l != 8) {
6269 msg->err_pos = 0;
6270 goto hdr_response_bad;
6271 }
6272
6273 if (rep->buf->p[4] != '/' ||
6274 !isdigit((unsigned char)rep->buf->p[5]) ||
6275 rep->buf->p[6] != '.' ||
6276 !isdigit((unsigned char)rep->buf->p[7])) {
6277 msg->err_pos = 4;
6278 goto hdr_response_bad;
6279 }
6280 }
6281
Willy Tarreau5b154472009-12-21 20:11:07 +01006282 /* check if the response is HTTP/1.1 or above */
6283 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006284 ((rep->buf->p[5] > '1') ||
6285 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006286 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006287
6288 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006289 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 +01006290
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006291 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006292 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006293
Willy Tarreau9b28e032012-10-12 23:49:43 +02006294 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006295
Willy Tarreau39650402010-03-15 19:44:39 +01006296 /* Adjust server's health based on status code. Note: status codes 501
6297 * and 505 are triggered on demand by client request, so we must not
6298 * count them as server failures.
6299 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006300 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006301 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006302 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006303 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006304 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006305 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006306
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006307 /*
6308 * 2: check for cacheability.
6309 */
6310
6311 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006312 case 100:
6313 /*
6314 * We may be facing a 100-continue response, in which case this
6315 * is not the right response, and we're waiting for the next one.
6316 * Let's allow this response to go to the client and wait for the
6317 * next one.
6318 */
6319 hdr_idx_init(&txn->hdr_idx);
6320 msg->next -= channel_forward(rep, msg->next);
6321 msg->msg_state = HTTP_MSG_RPBEFORE;
6322 txn->status = 0;
6323 s->logs.t_data = -1; /* was not a response yet */
6324 goto next_one;
6325
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006326 case 200:
6327 case 203:
6328 case 206:
6329 case 300:
6330 case 301:
6331 case 410:
6332 /* RFC2616 @13.4:
6333 * "A response received with a status code of
6334 * 200, 203, 206, 300, 301 or 410 MAY be stored
6335 * by a cache (...) unless a cache-control
6336 * directive prohibits caching."
6337 *
6338 * RFC2616 @9.5: POST method :
6339 * "Responses to this method are not cacheable,
6340 * unless the response includes appropriate
6341 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006342 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006343 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006344 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006345 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6346 break;
6347 default:
6348 break;
6349 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006350
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006351 /*
6352 * 3: we may need to capture headers
6353 */
6354 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006355 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006356 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006357 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006358
Willy Tarreau557f1992015-05-01 10:05:17 +02006359 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6360 * by RFC7230#3.3.3 :
6361 *
6362 * The length of a message body is determined by one of the following
6363 * (in order of precedence):
6364 *
6365 * 1. Any response to a HEAD request and any response with a 1xx
6366 * (Informational), 204 (No Content), or 304 (Not Modified) status
6367 * code is always terminated by the first empty line after the
6368 * header fields, regardless of the header fields present in the
6369 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006370 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006371 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6372 * the connection will become a tunnel immediately after the empty
6373 * line that concludes the header fields. A client MUST ignore any
6374 * Content-Length or Transfer-Encoding header fields received in
6375 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006376 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006377 * 3. If a Transfer-Encoding header field is present and the chunked
6378 * transfer coding (Section 4.1) is the final encoding, the message
6379 * body length is determined by reading and decoding the chunked
6380 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006381 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006382 * If a Transfer-Encoding header field is present in a response and
6383 * the chunked transfer coding is not the final encoding, the
6384 * message body length is determined by reading the connection until
6385 * it is closed by the server. If a Transfer-Encoding header field
6386 * is present in a request and the chunked transfer coding is not
6387 * the final encoding, the message body length cannot be determined
6388 * reliably; the server MUST respond with the 400 (Bad Request)
6389 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006390 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006391 * If a message is received with both a Transfer-Encoding and a
6392 * Content-Length header field, the Transfer-Encoding overrides the
6393 * Content-Length. Such a message might indicate an attempt to
6394 * perform request smuggling (Section 9.5) or response splitting
6395 * (Section 9.4) and ought to be handled as an error. A sender MUST
6396 * remove the received Content-Length field prior to forwarding such
6397 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006398 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006399 * 4. If a message is received without Transfer-Encoding and with
6400 * either multiple Content-Length header fields having differing
6401 * field-values or a single Content-Length header field having an
6402 * invalid value, then the message framing is invalid and the
6403 * recipient MUST treat it as an unrecoverable error. If this is a
6404 * request message, the server MUST respond with a 400 (Bad Request)
6405 * status code and then close the connection. If this is a response
6406 * message received by a proxy, the proxy MUST close the connection
6407 * to the server, discard the received response, and send a 502 (Bad
6408 * Gateway) response to the client. If this is a response message
6409 * received by a user agent, the user agent MUST close the
6410 * connection to the server and discard the received response.
6411 *
6412 * 5. If a valid Content-Length header field is present without
6413 * Transfer-Encoding, its decimal value defines the expected message
6414 * body length in octets. If the sender closes the connection or
6415 * the recipient times out before the indicated number of octets are
6416 * received, the recipient MUST consider the message to be
6417 * incomplete and close the connection.
6418 *
6419 * 6. If this is a request message and none of the above are true, then
6420 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006421 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006422 * 7. Otherwise, this is a response message without a declared message
6423 * body length, so the message body length is determined by the
6424 * number of octets received prior to the server closing the
6425 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006426 */
6427
6428 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006429 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006430 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006431 * FIXME: should we parse anyway and return an error on chunked encoding ?
6432 */
6433 if (txn->meth == HTTP_METH_HEAD ||
6434 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006435 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006436 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006437 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006438 goto skip_content_length;
6439 }
6440
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006441 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006442 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006443 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006444 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006445 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6446 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006447 /* bad transfer-encoding (chunked followed by something else) */
6448 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006449 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006450 break;
6451 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006452 }
6453
Willy Tarreau1c913912015-04-30 10:57:51 +02006454 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006455 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006456 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006457 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6458 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6459 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006460 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006461 signed long long cl;
6462
Willy Tarreauad14f752011-09-02 20:33:27 +02006463 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006464 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006465 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006466 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006467
Willy Tarreauad14f752011-09-02 20:33:27 +02006468 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006469 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006470 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006471 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006472
Willy Tarreauad14f752011-09-02 20:33:27 +02006473 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006474 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006475 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006476 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006477
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006478 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006479 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006480 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006481 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006482
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006483 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006484 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006485 }
6486
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006487 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006488 select_compression_response_header(s, rep->buf);
6489
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006490skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006491 /* Now we have to check if we need to modify the Connection header.
6492 * This is more difficult on the response than it is on the request,
6493 * because we can have two different HTTP versions and we don't know
6494 * how the client will interprete a response. For instance, let's say
6495 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6496 * HTTP/1.1 response without any header. Maybe it will bound itself to
6497 * HTTP/1.0 because it only knows about it, and will consider the lack
6498 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6499 * the lack of header as a keep-alive. Thus we will use two flags
6500 * indicating how a request MAY be understood by the client. In case
6501 * of multiple possibilities, we'll fix the header to be explicit. If
6502 * ambiguous cases such as both close and keepalive are seen, then we
6503 * will fall back to explicit close. Note that we won't take risks with
6504 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006505 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006506 */
6507
Willy Tarreaudc008c52010-02-01 16:20:08 +01006508 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6509 txn->status == 101)) {
6510 /* Either we've established an explicit tunnel, or we're
6511 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006512 * to understand the next protocols. We have to switch to tunnel
6513 * mode, so that we transfer the request and responses then let
6514 * this protocol pass unmodified. When we later implement specific
6515 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006516 * header which contains information about that protocol for
6517 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006518 */
6519 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6520 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006521 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6522 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
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 Tarreau60466522010-01-18 19:08:45 +01006525 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006526
Willy Tarreau70dffda2014-01-30 03:07:23 +01006527 /* this situation happens when combining pretend-keepalive with httpclose. */
6528 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006529 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006530 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006531 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6532
Willy Tarreau60466522010-01-18 19:08:45 +01006533 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006534 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006535 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6536 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006537
Willy Tarreau60466522010-01-18 19:08:45 +01006538 /* now adjust header transformations depending on current state */
6539 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6540 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6541 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006542 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006543 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006544 }
Willy Tarreau60466522010-01-18 19:08:45 +01006545 else { /* SCL / KAL */
6546 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006547 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006548 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006549 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006550
Willy Tarreau60466522010-01-18 19:08:45 +01006551 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006552 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006553
Willy Tarreau60466522010-01-18 19:08:45 +01006554 /* Some keep-alive responses are converted to Server-close if
6555 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006556 */
Willy Tarreau60466522010-01-18 19:08:45 +01006557 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6558 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006559 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006560 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006561 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006562 }
6563
Willy Tarreau7959a552013-09-23 16:44:27 +02006564 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006565 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006566
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006567 /* end of job, return OK */
6568 rep->analysers &= ~an_bit;
6569 rep->analyse_exp = TICK_ETERNITY;
6570 channel_auto_close(rep);
6571 return 1;
6572
6573 abort_keep_alive:
6574 /* A keep-alive request to the server failed on a network error.
6575 * The client is required to retry. We need to close without returning
6576 * any other information so that the client retries.
6577 */
6578 txn->status = 0;
6579 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006580 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006581 channel_auto_close(rep);
6582 s->logs.logwait = 0;
6583 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006584 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006585 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006586 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006587 return 0;
6588}
6589
6590/* This function performs all the processing enabled for the current response.
6591 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006592 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006593 * other functions. It works like process_request (see indications above).
6594 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006595int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006596{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006597 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006598 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006599 struct http_msg *msg = &txn->rsp;
6600 struct proxy *cur_proxy;
6601 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006602 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006603
Willy Tarreau87b09662015-04-03 00:22:06 +02006604 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 +02006605 now_ms, __FUNCTION__,
6606 s,
6607 rep,
6608 rep->rex, rep->wex,
6609 rep->flags,
6610 rep->buf->i,
6611 rep->analysers);
6612
6613 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6614 return 0;
6615
Willy Tarreau70730dd2014-04-24 18:06:27 +02006616 /* The stats applet needs to adjust the Connection header but we don't
6617 * apply any filter there.
6618 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006619 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6620 rep->analysers &= ~an_bit;
6621 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006622 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006623 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006624
Willy Tarreau58975672014-04-24 21:13:57 +02006625 /*
6626 * We will have to evaluate the filters.
6627 * As opposed to version 1.2, now they will be evaluated in the
6628 * filters order and not in the header order. This means that
6629 * each filter has to be validated among all headers.
6630 *
6631 * Filters are tried with ->be first, then with ->fe if it is
6632 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006633 *
6634 * Maybe we are in resume condiion. In this case I choose the
6635 * "struct proxy" which contains the rule list matching the resume
6636 * pointer. If none of theses "struct proxy" match, I initialise
6637 * the process with the first one.
6638 *
6639 * In fact, I check only correspondance betwwen the current list
6640 * pointer and the ->fe rule list. If it doesn't match, I initialize
6641 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006642 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006643 if (s->current_rule_list == &sess->fe->http_res_rules)
6644 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006645 else
6646 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006647 while (1) {
6648 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006649
Willy Tarreau58975672014-04-24 21:13:57 +02006650 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006651 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006652 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006653
Willy Tarreau51d861a2015-05-22 17:30:48 +02006654 if (ret == HTTP_RULE_RES_BADREQ)
6655 goto return_srv_prx_502;
6656
6657 if (ret == HTTP_RULE_RES_DONE) {
6658 rep->analysers &= ~an_bit;
6659 rep->analyse_exp = TICK_ETERNITY;
6660 return 1;
6661 }
6662 }
6663
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006664 /* we need to be called again. */
6665 if (ret == HTTP_RULE_RES_YIELD) {
6666 channel_dont_close(rep);
6667 return 0;
6668 }
6669
Willy Tarreau58975672014-04-24 21:13:57 +02006670 /* try headers filters */
6671 if (rule_set->rsp_exp != NULL) {
6672 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6673 return_bad_resp:
6674 if (objt_server(s->target)) {
6675 objt_server(s->target)->counters.failed_resp++;
6676 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006677 }
Willy Tarreau58975672014-04-24 21:13:57 +02006678 s->be->be_counters.failed_resp++;
6679 return_srv_prx_502:
6680 rep->analysers = 0;
6681 txn->status = 502;
6682 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006683 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006684 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006685 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006686 if (!(s->flags & SF_ERR_MASK))
6687 s->flags |= SF_ERR_PRXCOND;
6688 if (!(s->flags & SF_FINST_MASK))
6689 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006690 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006691 }
Willy Tarreau58975672014-04-24 21:13:57 +02006692 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006693
Willy Tarreau58975672014-04-24 21:13:57 +02006694 /* has the response been denied ? */
6695 if (txn->flags & TX_SVDENY) {
6696 if (objt_server(s->target))
6697 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006698
Willy Tarreau58975672014-04-24 21:13:57 +02006699 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006700 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006701 if (sess->listener->counters)
6702 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006703
Willy Tarreau58975672014-04-24 21:13:57 +02006704 goto return_srv_prx_502;
6705 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006706
Willy Tarreau58975672014-04-24 21:13:57 +02006707 /* add response headers from the rule sets in the same order */
6708 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006709 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006710 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006711 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006712 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006713 ret = acl_pass(ret);
6714 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6715 ret = !ret;
6716 if (!ret)
6717 continue;
6718 }
6719 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6720 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006721 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006722
Willy Tarreau58975672014-04-24 21:13:57 +02006723 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006724 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006725 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006726 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006727 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006728
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006729 /* After this point, this anayzer can't return yield, so we can
6730 * remove the bit corresponding to this analyzer from the list.
6731 *
6732 * Note that the intermediate returns and goto found previously
6733 * reset the analyzers.
6734 */
6735 rep->analysers &= ~an_bit;
6736 rep->analyse_exp = TICK_ETERNITY;
6737
Willy Tarreau58975672014-04-24 21:13:57 +02006738 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006739 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006740 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006741
Willy Tarreau58975672014-04-24 21:13:57 +02006742 /*
6743 * Now check for a server cookie.
6744 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006745 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006746 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006747
Willy Tarreau58975672014-04-24 21:13:57 +02006748 /*
6749 * Check for cache-control or pragma headers if required.
6750 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006751 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 +02006752 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006753
Willy Tarreau58975672014-04-24 21:13:57 +02006754 /*
6755 * Add server cookie in the response if needed
6756 */
6757 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6758 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006759 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006760 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6761 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6762 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6763 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6764 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006765 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006766 /* the server is known, it's not the one the client requested, or the
6767 * cookie's last seen date needs to be refreshed. We have to
6768 * insert a set-cookie here, except if we want to insert only on POST
6769 * requests and this one isn't. Note that servers which don't have cookies
6770 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006771 */
Willy Tarreau58975672014-04-24 21:13:57 +02006772 if (!objt_server(s->target)->cookie) {
6773 chunk_printf(&trash,
6774 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6775 s->be->cookie_name);
6776 }
6777 else {
6778 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006779
Willy Tarreau58975672014-04-24 21:13:57 +02006780 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6781 /* emit last_date, which is mandatory */
6782 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6783 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6784 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006785
Willy Tarreau58975672014-04-24 21:13:57 +02006786 if (s->be->cookie_maxlife) {
6787 /* emit first_date, which is either the original one or
6788 * the current date.
6789 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006790 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006791 s30tob64(txn->cookie_first_date ?
6792 txn->cookie_first_date >> 2 :
6793 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006794 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006795 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006796 }
Willy Tarreau58975672014-04-24 21:13:57 +02006797 chunk_appendf(&trash, "; path=/");
6798 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006799
Willy Tarreau58975672014-04-24 21:13:57 +02006800 if (s->be->cookie_domain)
6801 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006802
Willy Tarreau58975672014-04-24 21:13:57 +02006803 if (s->be->ck_opts & PR_CK_HTTPONLY)
6804 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006805
Willy Tarreau58975672014-04-24 21:13:57 +02006806 if (s->be->ck_opts & PR_CK_SECURE)
6807 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006808
Willy Tarreau58975672014-04-24 21:13:57 +02006809 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6810 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006811
Willy Tarreau58975672014-04-24 21:13:57 +02006812 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006813 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006814 /* the server did not change, only the date was updated */
6815 txn->flags |= TX_SCK_UPDATED;
6816 else
6817 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006818
Willy Tarreau58975672014-04-24 21:13:57 +02006819 /* Here, we will tell an eventual cache on the client side that we don't
6820 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6821 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6822 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006823 */
Willy Tarreau58975672014-04-24 21:13:57 +02006824 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006825
Willy Tarreau58975672014-04-24 21:13:57 +02006826 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006827
Willy Tarreau58975672014-04-24 21:13:57 +02006828 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6829 "Cache-control: private", 22) < 0))
6830 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006831 }
Willy Tarreau58975672014-04-24 21:13:57 +02006832 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006833
Willy Tarreau58975672014-04-24 21:13:57 +02006834 /*
6835 * Check if result will be cacheable with a cookie.
6836 * We'll block the response if security checks have caught
6837 * nasty things such as a cacheable cookie.
6838 */
6839 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6840 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6841 (s->be->options & PR_O_CHK_CACHE)) {
6842 /* we're in presence of a cacheable response containing
6843 * a set-cookie header. We'll block it as requested by
6844 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006845 */
Willy Tarreau58975672014-04-24 21:13:57 +02006846 if (objt_server(s->target))
6847 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006848
Willy Tarreau58975672014-04-24 21:13:57 +02006849 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006850 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006851 if (sess->listener->counters)
6852 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006853
Willy Tarreau58975672014-04-24 21:13:57 +02006854 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6855 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6856 send_log(s->be, LOG_ALERT,
6857 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6858 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6859 goto return_srv_prx_502;
6860 }
Willy Tarreau03945942009-12-22 16:50:27 +01006861
Willy Tarreau70730dd2014-04-24 18:06:27 +02006862 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006863 /*
6864 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6865 * If an "Upgrade" token is found, the header is left untouched in order
6866 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006867 * if anything but "Upgrade" is present in the Connection header. We don't
6868 * want to touch any 101 response either since it's switching to another
6869 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006870 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006871 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006872 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006873 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006874 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6875 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006876
Willy Tarreau58975672014-04-24 21:13:57 +02006877 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6878 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6879 /* we want a keep-alive response here. Keep-alive header
6880 * required if either side is not 1.1.
6881 */
6882 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6883 want_flags |= TX_CON_KAL_SET;
6884 }
6885 else {
6886 /* we want a close response here. Close header required if
6887 * the server is 1.1, regardless of the client.
6888 */
6889 if (msg->flags & HTTP_MSGF_VER_11)
6890 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006891 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006892
Willy Tarreau58975672014-04-24 21:13:57 +02006893 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6894 http_change_connection_header(txn, msg, want_flags);
6895 }
6896
6897 skip_header_mangling:
6898 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6899 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6900 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006901
Willy Tarreau58975672014-04-24 21:13:57 +02006902 /* if the user wants to log as soon as possible, without counting
6903 * bytes from the server, then this is the right moment. We have
6904 * to temporarily assign bytes_out to log what we currently have.
6905 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006906 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006907 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6908 s->logs.bytes_out = txn->rsp.eoh;
6909 s->do_log(s);
6910 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006911 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006912 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006913}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006914
Willy Tarreaud98cf932009-12-27 22:54:55 +01006915/* This function is an analyser which forwards response body (including chunk
6916 * sizes if any). It is called as soon as we must forward, even if we forward
6917 * zero byte. The only situation where it must not be called is when we're in
6918 * tunnel mode and we want to forward till the close. It's used both to forward
6919 * remaining data and to resync after end of body. It expects the msg_state to
6920 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006921 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006922 *
6923 * It is capable of compressing response data both in content-length mode and
6924 * in chunked mode. The state machines follows different flows depending on
6925 * whether content-length and chunked modes are used, since there are no
6926 * trailers in content-length :
6927 *
6928 * chk-mode cl-mode
6929 * ,----- BODY -----.
6930 * / \
6931 * V size > 0 V chk-mode
6932 * .--> SIZE -------------> DATA -------------> CRLF
6933 * | | size == 0 | last byte |
6934 * | v final crlf v inspected |
6935 * | TRAILERS -----------> DONE |
6936 * | |
6937 * `----------------------------------------------'
6938 *
6939 * Compression only happens in the DATA state, and must be flushed in final
6940 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6941 * is performed at once on final states for all bytes parsed, or when leaving
6942 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006943 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006944int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006945{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006946 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006947 struct http_txn *txn = s->txn;
6948 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006949 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006950 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006951 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006952
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006953 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6954 return 0;
6955
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006956 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006957 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006958 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006959 /* Output closed while we were sending data. We must abort and
6960 * wake the other side up.
6961 */
6962 msg->msg_state = HTTP_MSG_ERROR;
6963 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006964 return 1;
6965 }
6966
Willy Tarreau4fe41902010-06-07 22:27:41 +02006967 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006968 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006969
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006970 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006971 /* we have msg->sov which points to the first byte of message
6972 * body, and res->buf.p still points to the beginning of the
6973 * message. We forward the headers now, as we don't need them
6974 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006975 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006976 b_adv(res->buf, msg->sov);
6977 msg->next -= msg->sov;
6978 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006979
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006980 /* The previous analysers guarantee that the state is somewhere
6981 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6982 * msg->next are always correct.
6983 */
6984 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6985 if (msg->flags & HTTP_MSGF_TE_CHNK)
6986 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6987 else
6988 msg->msg_state = HTTP_MSG_DATA;
6989 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006990 }
6991
Willy Tarreauefdf0942014-04-24 20:08:57 +02006992 if (res->to_forward) {
6993 /* We can't process the buffer's contents yet */
6994 res->flags |= CF_WAKE_WRITE;
6995 goto missing_data;
6996 }
6997
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006998 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6999 /* We need a compression buffer in the DATA state to put the
7000 * output of compressed data, and in CRLF state to let the
7001 * TRAILERS state finish the job of removing the trailing CRLF.
7002 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01007003 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02007004 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01007005 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02007006 goto aborted_xfer; /* no memory */
7007 }
7008
7009 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01007010 if (ret < 0) {
7011 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02007012 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01007013 }
William Lallemand82fe75c2012-10-23 10:25:10 +02007014 compressing = 1;
7015 }
7016
Willy Tarreaud98cf932009-12-27 22:54:55 +01007017 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007018 switch (msg->msg_state - HTTP_MSG_DATA) {
7019 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02007020 /* we may have some pending data starting at res->buf->p */
7021 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02007022 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01007023 if (ret < 0)
7024 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02007025
Willy Tarreaud5a67832014-04-21 10:54:27 +02007026 if (msg->chunk_len) {
7027 /* input empty or output full */
7028 if (res->buf->i > msg->next)
7029 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02007030 goto missing_data;
7031 }
William Lallemandbf3ae612012-11-19 12:35:37 +01007032 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007033 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02007034 if (msg->chunk_len > res->buf->i - msg->next) {
7035 /* output full */
7036 res->flags |= CF_WAKE_WRITE;
7037 goto missing_data;
7038 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007039 msg->next += msg->chunk_len;
7040 msg->chunk_len = 0;
7041 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01007042
7043 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01007044 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02007045 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01007046 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01007047 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007048 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01007049 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007050 /* fall through for HTTP_MSG_CHUNK_CRLF */
7051
7052 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
7053 /* we want the CRLF after the data */
7054
7055 ret = http_skip_chunk_crlf(msg);
7056 if (ret == 0)
7057 goto missing_data;
7058 else if (ret < 0) {
7059 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007060 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007061 goto return_bad_res;
7062 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007063 /* we're in MSG_CHUNK_SIZE now, fall through */
7064
7065 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01007066 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02007067 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01007068 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01007069 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007070
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007071 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02007072 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01007073 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007074 else if (ret < 0) {
7075 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007076 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007077 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007078 }
Willy Tarreau0161d622013-04-02 01:26:55 +02007079 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007080 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01007081
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007082 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02007083 if (unlikely(compressing)) {
7084 /* we need to flush output contents before syncing FSMs */
7085 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7086 compressing = 0;
7087 }
7088
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007089 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007090 if (ret == 0)
7091 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007092 else if (ret < 0) {
7093 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007094 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007095 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007096 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02007097 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007098
7099 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01007100 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007101 if (unlikely(compressing)) {
7102 /* we need to flush output contents before syncing FSMs */
7103 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7104 compressing = 0;
7105 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007106
Willy Tarreauc623c172014-04-18 09:53:50 +02007107 /* we may have some pending data starting at res->buf->p
7108 * such as a last chunk of data or trailers.
7109 */
7110 b_adv(res->buf, msg->next);
7111 msg->next = 0;
7112
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007113 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02007114 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007115 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7116 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007117 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02007118
Willy Tarreau610ecce2010-01-04 21:15:02 +01007119 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01007120 /* some state changes occurred, maybe the analyser
7121 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01007122 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007123 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007124 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007125 /* response errors are most likely due to
7126 * the client aborting the transfer.
7127 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007128 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007129 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007130 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007131 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01007132 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007133 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007134 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01007135 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007136 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007137 }
7138 }
7139
Willy Tarreaud98cf932009-12-27 22:54:55 +01007140 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02007141 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007142 if (unlikely(compressing)) {
7143 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02007144 compressing = 0;
7145 }
Willy Tarreauf003d372012-11-26 13:35:37 +01007146
Willy Tarreauc623c172014-04-18 09:53:50 +02007147 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
7148 b_adv(res->buf, msg->next);
7149 msg->next = 0;
7150 msg->chunk_len -= channel_forward(res, msg->chunk_len);
7151 }
7152
Willy Tarreauf003d372012-11-26 13:35:37 +01007153 if (res->flags & CF_SHUTW)
7154 goto aborted_xfer;
7155
7156 /* stop waiting for data if the input is closed before the end. If the
7157 * client side was already closed, it means that the client has aborted,
7158 * so we don't want to count this as a server abort. Otherwise it's a
7159 * server abort.
7160 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007161 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007162 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01007163 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02007164 /* If we have some pending data, we continue the processing */
7165 if (!buffer_pending(res->buf)) {
7166 if (!(s->flags & SF_ERR_MASK))
7167 s->flags |= SF_ERR_SRVCL;
7168 s->be->be_counters.srv_aborts++;
7169 if (objt_server(s->target))
7170 objt_server(s->target)->counters.srv_aborts++;
7171 goto return_bad_res_stats_ok;
7172 }
Willy Tarreau40dba092010-03-04 18:14:51 +01007173 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01007174
Willy Tarreau40dba092010-03-04 18:14:51 +01007175 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007176 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01007177 goto return_bad_res;
7178
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007179 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007180 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007181 * Similarly, with keep-alive on the client side, we don't want to forward a
7182 * close.
7183 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007184 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007185 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7186 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007187 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007188
Willy Tarreau5c620922011-05-11 19:56:11 +02007189 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007190 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007191 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007192 * modes are already handled by the stream sock layer. We must not do
7193 * this in content-length mode because it could present the MSG_MORE
7194 * flag with the last block of forwarded data, which would cause an
7195 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007196 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007197 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007198 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007199
Willy Tarreau87b09662015-04-03 00:22:06 +02007200 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007201 return 0;
7202
Willy Tarreau40dba092010-03-04 18:14:51 +01007203 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007204 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007205 if (objt_server(s->target))
7206 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007207
7208 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02007209 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02007210 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02007211 compressing = 0;
7212 }
7213
Willy Tarreauc623c172014-04-18 09:53:50 +02007214 /* we may have some pending data starting at res->buf->p */
7215 if (s->comp_algo == NULL) {
7216 b_adv(res->buf, msg->next);
7217 msg->next = 0;
7218 }
7219
Willy Tarreaud98cf932009-12-27 22:54:55 +01007220 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007221 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007222 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007223 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007224 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007225 if (objt_server(s->target))
7226 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007227
Willy Tarreaue7dff022015-04-03 01:14:29 +02007228 if (!(s->flags & SF_ERR_MASK))
7229 s->flags |= SF_ERR_PRXCOND;
7230 if (!(s->flags & SF_FINST_MASK))
7231 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007232 return 0;
7233
7234 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02007235 if (unlikely(compressing)) {
7236 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
7237 compressing = 0;
7238 }
7239
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007240 txn->rsp.msg_state = HTTP_MSG_ERROR;
7241 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007242 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007243 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007244 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007245
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007246 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007247 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007248 if (objt_server(s->target))
7249 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007250
Willy Tarreaue7dff022015-04-03 01:14:29 +02007251 if (!(s->flags & SF_ERR_MASK))
7252 s->flags |= SF_ERR_CLICL;
7253 if (!(s->flags & SF_FINST_MASK))
7254 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007255 return 0;
7256}
7257
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007258/* Iterate the same filter through all request headers.
7259 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007260 * Since it can manage the switch to another backend, it updates the per-proxy
7261 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007262 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007263int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007264{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007265 char *cur_ptr, *cur_end, *cur_next;
7266 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007267 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007268 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007269 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007270
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007271 last_hdr = 0;
7272
Willy Tarreau9b28e032012-10-12 23:49:43 +02007273 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007274 old_idx = 0;
7275
7276 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007277 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007278 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007279 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007280 (exp->action == ACT_ALLOW ||
7281 exp->action == ACT_DENY ||
7282 exp->action == ACT_TARPIT))
7283 return 0;
7284
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007285 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007286 if (!cur_idx)
7287 break;
7288
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007289 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007290 cur_ptr = cur_next;
7291 cur_end = cur_ptr + cur_hdr->len;
7292 cur_next = cur_end + cur_hdr->cr + 1;
7293
7294 /* Now we have one header between cur_ptr and cur_end,
7295 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007296 */
7297
Willy Tarreau15a53a42015-01-21 13:39:42 +01007298 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007299 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007300 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007301 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007302 last_hdr = 1;
7303 break;
7304
7305 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007306 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007307 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007308 break;
7309
7310 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007311 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007312 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007313 break;
7314
7315 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007316 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7317 if (trash.len < 0)
7318 return -1;
7319
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007320 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007321 /* FIXME: if the user adds a newline in the replacement, the
7322 * index will not be recalculated for now, and the new line
7323 * will not be counted as a new header.
7324 */
7325
7326 cur_end += delta;
7327 cur_next += delta;
7328 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007329 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007330 break;
7331
7332 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007333 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007334 cur_next += delta;
7335
Willy Tarreaufa355d42009-11-29 18:12:29 +01007336 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007337 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7338 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007339 cur_hdr->len = 0;
7340 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007341 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007342 break;
7343
7344 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007345 }
7346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007347 /* keep the link from this header to next one in case of later
7348 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007349 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007350 old_idx = cur_idx;
7351 }
7352 return 0;
7353}
7354
7355
7356/* Apply the filter to the request line.
7357 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7358 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007359 * Since it can manage the switch to another backend, it updates the per-proxy
7360 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007361 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007362int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007363{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007364 char *cur_ptr, *cur_end;
7365 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007366 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007367 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007368
Willy Tarreau3d300592007-03-18 18:34:41 +01007369 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007370 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007371 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007372 (exp->action == ACT_ALLOW ||
7373 exp->action == ACT_DENY ||
7374 exp->action == ACT_TARPIT))
7375 return 0;
7376 else if (exp->action == ACT_REMOVE)
7377 return 0;
7378
7379 done = 0;
7380
Willy Tarreau9b28e032012-10-12 23:49:43 +02007381 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007382 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007383
7384 /* Now we have the request line between cur_ptr and cur_end */
7385
Willy Tarreau15a53a42015-01-21 13:39:42 +01007386 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007387 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007388 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007389 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007390 done = 1;
7391 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007393 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007394 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007395 done = 1;
7396 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007397
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007398 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007399 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007400 done = 1;
7401 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007403 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007404 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7405 if (trash.len < 0)
7406 return -1;
7407
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007408 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007409 /* FIXME: if the user adds a newline in the replacement, the
7410 * index will not be recalculated for now, and the new line
7411 * will not be counted as a new header.
7412 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007413
Willy Tarreaufa355d42009-11-29 18:12:29 +01007414 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007415 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007416 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007417 HTTP_MSG_RQMETH,
7418 cur_ptr, cur_end + 1,
7419 NULL, NULL);
7420 if (unlikely(!cur_end))
7421 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007422
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007423 /* we have a full request and we know that we have either a CR
7424 * or an LF at <ptr>.
7425 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007426 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7427 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007428 /* there is no point trying this regex on headers */
7429 return 1;
7430 }
7431 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007432 return done;
7433}
Willy Tarreau97de6242006-12-27 17:18:38 +01007434
Willy Tarreau58f10d72006-12-04 02:26:12 +01007435
Willy Tarreau58f10d72006-12-04 02:26:12 +01007436
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007437/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007438 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007439 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007440 * unparsable request. Since it can manage the switch to another backend, it
7441 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007442 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007443int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007444{
Willy Tarreau192252e2015-04-04 01:47:55 +02007445 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007446 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007447 struct hdr_exp *exp;
7448
7449 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007450 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007451
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007452 /*
7453 * The interleaving of transformations and verdicts
7454 * makes it difficult to decide to continue or stop
7455 * the evaluation.
7456 */
7457
Willy Tarreau6c123b12010-01-28 20:22:06 +01007458 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7459 break;
7460
Willy Tarreau3d300592007-03-18 18:34:41 +01007461 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007462 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007463 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007464 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007465
7466 /* if this filter had a condition, evaluate it now and skip to
7467 * next filter if the condition does not match.
7468 */
7469 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007470 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007471 ret = acl_pass(ret);
7472 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7473 ret = !ret;
7474
7475 if (!ret)
7476 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007477 }
7478
7479 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007480 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007481 if (unlikely(ret < 0))
7482 return -1;
7483
7484 if (likely(ret == 0)) {
7485 /* The filter did not match the request, it can be
7486 * iterated through all headers.
7487 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007488 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7489 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007490 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007491 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007492 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007493}
7494
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007495
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007496/* Find the end of a cookie value contained between <s> and <e>. It works the
7497 * same way as with headers above except that the semi-colon also ends a token.
7498 * See RFC2965 for more information. Note that it requires a valid header to
7499 * return a valid result.
7500 */
7501char *find_cookie_value_end(char *s, const char *e)
7502{
7503 int quoted, qdpair;
7504
7505 quoted = qdpair = 0;
7506 for (; s < e; s++) {
7507 if (qdpair) qdpair = 0;
7508 else if (quoted) {
7509 if (*s == '\\') qdpair = 1;
7510 else if (*s == '"') quoted = 0;
7511 }
7512 else if (*s == '"') quoted = 1;
7513 else if (*s == ',' || *s == ';') return s;
7514 }
7515 return s;
7516}
7517
7518/* Delete a value in a header between delimiters <from> and <next> in buffer
7519 * <buf>. The number of characters displaced is returned, and the pointer to
7520 * the first delimiter is updated if required. The function tries as much as
7521 * possible to respect the following principles :
7522 * - replace <from> delimiter by the <next> one unless <from> points to a
7523 * colon, in which case <next> is simply removed
7524 * - set exactly one space character after the new first delimiter, unless
7525 * there are not enough characters in the block being moved to do so.
7526 * - remove unneeded spaces before the previous delimiter and after the new
7527 * one.
7528 *
7529 * It is the caller's responsibility to ensure that :
7530 * - <from> points to a valid delimiter or the colon ;
7531 * - <next> points to a valid delimiter or the final CR/LF ;
7532 * - there are non-space chars before <from> ;
7533 * - there is a CR/LF at or after <next>.
7534 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007535int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007536{
7537 char *prev = *from;
7538
7539 if (*prev == ':') {
7540 /* We're removing the first value, preserve the colon and add a
7541 * space if possible.
7542 */
7543 if (!http_is_crlf[(unsigned char)*next])
7544 next++;
7545 prev++;
7546 if (prev < next)
7547 *prev++ = ' ';
7548
7549 while (http_is_spht[(unsigned char)*next])
7550 next++;
7551 } else {
7552 /* Remove useless spaces before the old delimiter. */
7553 while (http_is_spht[(unsigned char)*(prev-1)])
7554 prev--;
7555 *from = prev;
7556
7557 /* copy the delimiter and if possible a space if we're
7558 * not at the end of the line.
7559 */
7560 if (!http_is_crlf[(unsigned char)*next]) {
7561 *prev++ = *next++;
7562 if (prev + 1 < next)
7563 *prev++ = ' ';
7564 while (http_is_spht[(unsigned char)*next])
7565 next++;
7566 }
7567 }
7568 return buffer_replace2(buf, prev, next, NULL, 0);
7569}
7570
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007571/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007572 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007573 * desirable to call it only when needed. This code is quite complex because
7574 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7575 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007576 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007577void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007578{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007579 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007580 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007581 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007582 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007583 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7584 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007585
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007586 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007587 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007588 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007589
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007590 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007591 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007592 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007593
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007594 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007595 hdr_beg = hdr_next;
7596 hdr_end = hdr_beg + cur_hdr->len;
7597 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007598
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007599 /* We have one full header between hdr_beg and hdr_end, and the
7600 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007601 * "Cookie:" headers.
7602 */
7603
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007604 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007605 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007606 old_idx = cur_idx;
7607 continue;
7608 }
7609
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007610 del_from = NULL; /* nothing to be deleted */
7611 preserve_hdr = 0; /* assume we may kill the whole header */
7612
Willy Tarreau58f10d72006-12-04 02:26:12 +01007613 /* Now look for cookies. Conforming to RFC2109, we have to support
7614 * attributes whose name begin with a '$', and associate them with
7615 * the right cookie, if we want to delete this cookie.
7616 * So there are 3 cases for each cookie read :
7617 * 1) it's a special attribute, beginning with a '$' : ignore it.
7618 * 2) it's a server id cookie that we *MAY* want to delete : save
7619 * some pointers on it (last semi-colon, beginning of cookie...)
7620 * 3) it's an application cookie : we *MAY* have to delete a previous
7621 * "special" cookie.
7622 * At the end of loop, if a "special" cookie remains, we may have to
7623 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007624 * *MUST* delete it.
7625 *
7626 * Note: RFC2965 is unclear about the processing of spaces around
7627 * the equal sign in the ATTR=VALUE form. A careful inspection of
7628 * the RFC explicitly allows spaces before it, and not within the
7629 * tokens (attrs or values). An inspection of RFC2109 allows that
7630 * too but section 10.1.3 lets one think that spaces may be allowed
7631 * after the equal sign too, resulting in some (rare) buggy
7632 * implementations trying to do that. So let's do what servers do.
7633 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7634 * allowed quoted strings in values, with any possible character
7635 * after a backslash, including control chars and delimitors, which
7636 * causes parsing to become ambiguous. Browsers also allow spaces
7637 * within values even without quotes.
7638 *
7639 * We have to keep multiple pointers in order to support cookie
7640 * removal at the beginning, middle or end of header without
7641 * corrupting the header. All of these headers are valid :
7642 *
7643 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7644 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7645 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7646 * | | | | | | | | |
7647 * | | | | | | | | hdr_end <--+
7648 * | | | | | | | +--> next
7649 * | | | | | | +----> val_end
7650 * | | | | | +-----------> val_beg
7651 * | | | | +--------------> equal
7652 * | | | +----------------> att_end
7653 * | | +---------------------> att_beg
7654 * | +--------------------------> prev
7655 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007656 */
7657
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007658 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7659 /* Iterate through all cookies on this line */
7660
7661 /* find att_beg */
7662 att_beg = prev + 1;
7663 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7664 att_beg++;
7665
7666 /* find att_end : this is the first character after the last non
7667 * space before the equal. It may be equal to hdr_end.
7668 */
7669 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007670
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007671 while (equal < hdr_end) {
7672 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007673 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007674 if (http_is_spht[(unsigned char)*equal++])
7675 continue;
7676 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007677 }
7678
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007679 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7680 * is between <att_beg> and <equal>, both may be identical.
7681 */
7682
7683 /* look for end of cookie if there is an equal sign */
7684 if (equal < hdr_end && *equal == '=') {
7685 /* look for the beginning of the value */
7686 val_beg = equal + 1;
7687 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7688 val_beg++;
7689
7690 /* find the end of the value, respecting quotes */
7691 next = find_cookie_value_end(val_beg, hdr_end);
7692
7693 /* make val_end point to the first white space or delimitor after the value */
7694 val_end = next;
7695 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7696 val_end--;
7697 } else {
7698 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007699 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007700
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007701 /* We have nothing to do with attributes beginning with '$'. However,
7702 * they will automatically be removed if a header before them is removed,
7703 * since they're supposed to be linked together.
7704 */
7705 if (*att_beg == '$')
7706 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007707
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007708 /* Ignore cookies with no equal sign */
7709 if (equal == next) {
7710 /* This is not our cookie, so we must preserve it. But if we already
7711 * scheduled another cookie for removal, we cannot remove the
7712 * complete header, but we can remove the previous block itself.
7713 */
7714 preserve_hdr = 1;
7715 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007716 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007717 val_end += delta;
7718 next += delta;
7719 hdr_end += delta;
7720 hdr_next += delta;
7721 cur_hdr->len += delta;
7722 http_msg_move_end(&txn->req, delta);
7723 prev = del_from;
7724 del_from = NULL;
7725 }
7726 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007727 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007728
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007729 /* if there are spaces around the equal sign, we need to
7730 * strip them otherwise we'll get trouble for cookie captures,
7731 * or even for rewrites. Since this happens extremely rarely,
7732 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007733 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007734 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7735 int stripped_before = 0;
7736 int stripped_after = 0;
7737
7738 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007739 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007740 equal += stripped_before;
7741 val_beg += stripped_before;
7742 }
7743
7744 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007745 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007746 val_beg += stripped_after;
7747 stripped_before += stripped_after;
7748 }
7749
7750 val_end += stripped_before;
7751 next += stripped_before;
7752 hdr_end += stripped_before;
7753 hdr_next += stripped_before;
7754 cur_hdr->len += stripped_before;
7755 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007756 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007757 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007758
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007759 /* First, let's see if we want to capture this cookie. We check
7760 * that we don't already have a client side cookie, because we
7761 * can only capture one. Also as an optimisation, we ignore
7762 * cookies shorter than the declared name.
7763 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007764 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7765 (val_end - att_beg >= sess->fe->capture_namelen) &&
7766 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007767 int log_len = val_end - att_beg;
7768
7769 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7770 Alert("HTTP logging : out of memory.\n");
7771 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007772 if (log_len > sess->fe->capture_len)
7773 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007774 memcpy(txn->cli_cookie, att_beg, log_len);
7775 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007776 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007777 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007778
Willy Tarreaubca99692010-10-06 19:25:55 +02007779 /* Persistence cookies in passive, rewrite or insert mode have the
7780 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007781 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007782 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007783 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007784 * For cookies in prefix mode, the form is :
7785 *
7786 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007787 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007788 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7789 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7790 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007791 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007792
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007793 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7794 * have the server ID between val_beg and delim, and the original cookie between
7795 * delim+1 and val_end. Otherwise, delim==val_end :
7796 *
7797 * Cookie: NAME=SRV; # in all but prefix modes
7798 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7799 * | || || | |+-> next
7800 * | || || | +--> val_end
7801 * | || || +---------> delim
7802 * | || |+------------> val_beg
7803 * | || +-------------> att_end = equal
7804 * | |+-----------------> att_beg
7805 * | +------------------> prev
7806 * +-------------------------> hdr_beg
7807 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007808
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007809 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007810 for (delim = val_beg; delim < val_end; delim++)
7811 if (*delim == COOKIE_DELIM)
7812 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007813 } else {
7814 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007815 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007816 /* Now check if the cookie contains a date field, which would
7817 * appear after a vertical bar ('|') just after the server name
7818 * and before the delimiter.
7819 */
7820 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7821 if (vbar1) {
7822 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007823 * right is the last seen date. It is a base64 encoded
7824 * 30-bit value representing the UNIX date since the
7825 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007826 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007827 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007828 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007829 if (val_end - vbar1 >= 5) {
7830 val = b64tos30(vbar1);
7831 if (val > 0)
7832 txn->cookie_last_date = val << 2;
7833 }
7834 /* look for a second vertical bar */
7835 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7836 if (vbar1 && (val_end - vbar1 > 5)) {
7837 val = b64tos30(vbar1 + 1);
7838 if (val > 0)
7839 txn->cookie_first_date = val << 2;
7840 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007841 }
7842 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007843
Willy Tarreauf64d1412010-10-07 20:06:11 +02007844 /* if the cookie has an expiration date and the proxy wants to check
7845 * it, then we do that now. We first check if the cookie is too old,
7846 * then only if it has expired. We detect strict overflow because the
7847 * time resolution here is not great (4 seconds). Cookies with dates
7848 * in the future are ignored if their offset is beyond one day. This
7849 * allows an admin to fix timezone issues without expiring everyone
7850 * and at the same time avoids keeping unwanted side effects for too
7851 * long.
7852 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007853 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7854 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007855 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007856 txn->flags &= ~TX_CK_MASK;
7857 txn->flags |= TX_CK_OLD;
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 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007862 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7863 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007864 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007865 txn->flags &= ~TX_CK_MASK;
7866 txn->flags |= TX_CK_EXPIRED;
7867 delim = val_beg; // let's pretend we have not found the cookie
7868 txn->cookie_first_date = 0;
7869 txn->cookie_last_date = 0;
7870 }
7871
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007872 /* Here, we'll look for the first running server which supports the cookie.
7873 * This allows to share a same cookie between several servers, for example
7874 * to dedicate backup servers to specific servers only.
7875 * However, to prevent clients from sticking to cookie-less backup server
7876 * when they have incidentely learned an empty cookie, we simply ignore
7877 * empty cookies and mark them as invalid.
7878 * The same behaviour is applied when persistence must be ignored.
7879 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007880 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007881 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007882
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007883 while (srv) {
7884 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7885 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007886 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007887 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007888 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007889 /* we found the server and we can use it */
7890 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007891 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007892 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007893 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007894 break;
7895 } else {
7896 /* we found a server, but it's down,
7897 * mark it as such and go on in case
7898 * another one is available.
7899 */
7900 txn->flags &= ~TX_CK_MASK;
7901 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007902 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007903 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007904 srv = srv->next;
7905 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007906
Willy Tarreauf64d1412010-10-07 20:06:11 +02007907 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007908 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007909 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007910 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007911 txn->flags |= TX_CK_UNUSED;
7912 else
7913 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007914 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007915
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007916 /* depending on the cookie mode, we may have to either :
7917 * - delete the complete cookie if we're in insert+indirect mode, so that
7918 * the server never sees it ;
7919 * - remove the server id from the cookie value, and tag the cookie as an
7920 * application cookie so that it does not get accidentely removed later,
7921 * if we're in cookie prefix mode
7922 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007923 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007924 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007925
Willy Tarreau9b28e032012-10-12 23:49:43 +02007926 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007927 val_end += delta;
7928 next += delta;
7929 hdr_end += delta;
7930 hdr_next += delta;
7931 cur_hdr->len += delta;
7932 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007933
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007934 del_from = NULL;
7935 preserve_hdr = 1; /* we want to keep this cookie */
7936 }
7937 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007938 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007939 del_from = prev;
7940 }
7941 } else {
7942 /* This is not our cookie, so we must preserve it. But if we already
7943 * scheduled another cookie for removal, we cannot remove the
7944 * complete header, but we can remove the previous block itself.
7945 */
7946 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007947
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007948 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007949 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007950 if (att_beg >= del_from)
7951 att_beg += delta;
7952 if (att_end >= del_from)
7953 att_end += delta;
7954 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007955 val_end += delta;
7956 next += delta;
7957 hdr_end += delta;
7958 hdr_next += delta;
7959 cur_hdr->len += delta;
7960 http_msg_move_end(&txn->req, delta);
7961 prev = del_from;
7962 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007963 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007964 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007965
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007966 /* continue with next cookie on this header line */
7967 att_beg = next;
7968 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007969
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007970 /* There are no more cookies on this line.
7971 * We may still have one (or several) marked for deletion at the
7972 * end of the line. We must do this now in two ways :
7973 * - if some cookies must be preserved, we only delete from the
7974 * mark to the end of line ;
7975 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007976 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007977 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007978 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007979 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007980 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007981 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007982 cur_hdr->len += delta;
7983 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007984 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007985
7986 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007987 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7988 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007989 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007990 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007991 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007992 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007993 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007994 }
7995
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007996 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007997 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007998 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007999}
8000
8001
Willy Tarreaua15645d2007-03-18 16:22:39 +01008002/* Iterate the same filter through all response headers contained in <rtr>.
8003 * Returns 1 if this filter can be stopped upon return, otherwise 0.
8004 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008005int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008006{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008007 char *cur_ptr, *cur_end, *cur_next;
8008 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008009 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008010 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008011 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008012
8013 last_hdr = 0;
8014
Willy Tarreau9b28e032012-10-12 23:49:43 +02008015 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008016 old_idx = 0;
8017
8018 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008019 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008020 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008021 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008022 (exp->action == ACT_ALLOW ||
8023 exp->action == ACT_DENY))
8024 return 0;
8025
8026 cur_idx = txn->hdr_idx.v[old_idx].next;
8027 if (!cur_idx)
8028 break;
8029
8030 cur_hdr = &txn->hdr_idx.v[cur_idx];
8031 cur_ptr = cur_next;
8032 cur_end = cur_ptr + cur_hdr->len;
8033 cur_next = cur_end + cur_hdr->cr + 1;
8034
8035 /* Now we have one header between cur_ptr and cur_end,
8036 * and the next header starts at cur_next.
8037 */
8038
Willy Tarreau15a53a42015-01-21 13:39:42 +01008039 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008040 switch (exp->action) {
8041 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008042 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008043 last_hdr = 1;
8044 break;
8045
8046 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008047 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008048 last_hdr = 1;
8049 break;
8050
8051 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008052 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8053 if (trash.len < 0)
8054 return -1;
8055
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008056 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008057 /* FIXME: if the user adds a newline in the replacement, the
8058 * index will not be recalculated for now, and the new line
8059 * will not be counted as a new header.
8060 */
8061
8062 cur_end += delta;
8063 cur_next += delta;
8064 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008065 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008066 break;
8067
8068 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008069 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008070 cur_next += delta;
8071
Willy Tarreaufa355d42009-11-29 18:12:29 +01008072 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008073 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8074 txn->hdr_idx.used--;
8075 cur_hdr->len = 0;
8076 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008077 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008078 break;
8079
8080 }
8081 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008082
8083 /* keep the link from this header to next one in case of later
8084 * removal of next header.
8085 */
8086 old_idx = cur_idx;
8087 }
8088 return 0;
8089}
8090
8091
8092/* Apply the filter to the status line in the response buffer <rtr>.
8093 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8094 * or -1 if a replacement resulted in an invalid status line.
8095 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008096int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008097{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008098 char *cur_ptr, *cur_end;
8099 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008100 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008101 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008102
8103
Willy Tarreau3d300592007-03-18 18:34:41 +01008104 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008105 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008106 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008107 (exp->action == ACT_ALLOW ||
8108 exp->action == ACT_DENY))
8109 return 0;
8110 else if (exp->action == ACT_REMOVE)
8111 return 0;
8112
8113 done = 0;
8114
Willy Tarreau9b28e032012-10-12 23:49:43 +02008115 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008116 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008117
8118 /* Now we have the status line between cur_ptr and cur_end */
8119
Willy Tarreau15a53a42015-01-21 13:39:42 +01008120 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008121 switch (exp->action) {
8122 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008123 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008124 done = 1;
8125 break;
8126
8127 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008128 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008129 done = 1;
8130 break;
8131
8132 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008133 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8134 if (trash.len < 0)
8135 return -1;
8136
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008137 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008138 /* FIXME: if the user adds a newline in the replacement, the
8139 * index will not be recalculated for now, and the new line
8140 * will not be counted as a new header.
8141 */
8142
Willy Tarreaufa355d42009-11-29 18:12:29 +01008143 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008144 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008145 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008146 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008147 cur_ptr, cur_end + 1,
8148 NULL, NULL);
8149 if (unlikely(!cur_end))
8150 return -1;
8151
8152 /* we have a full respnse and we know that we have either a CR
8153 * or an LF at <ptr>.
8154 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008155 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008156 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008157 /* there is no point trying this regex on headers */
8158 return 1;
8159 }
8160 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008161 return done;
8162}
8163
8164
8165
8166/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008167 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008168 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8169 * unparsable response.
8170 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008171int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008172{
Willy Tarreau192252e2015-04-04 01:47:55 +02008173 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008174 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008175 struct hdr_exp *exp;
8176
8177 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008178 int ret;
8179
8180 /*
8181 * The interleaving of transformations and verdicts
8182 * makes it difficult to decide to continue or stop
8183 * the evaluation.
8184 */
8185
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008186 if (txn->flags & TX_SVDENY)
8187 break;
8188
Willy Tarreau3d300592007-03-18 18:34:41 +01008189 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008190 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8191 exp->action == ACT_PASS)) {
8192 exp = exp->next;
8193 continue;
8194 }
8195
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008196 /* if this filter had a condition, evaluate it now and skip to
8197 * next filter if the condition does not match.
8198 */
8199 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008200 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008201 ret = acl_pass(ret);
8202 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8203 ret = !ret;
8204 if (!ret)
8205 continue;
8206 }
8207
Willy Tarreaua15645d2007-03-18 16:22:39 +01008208 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008209 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008210 if (unlikely(ret < 0))
8211 return -1;
8212
8213 if (likely(ret == 0)) {
8214 /* The filter did not match the response, it can be
8215 * iterated through all headers.
8216 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008217 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8218 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008219 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008220 }
8221 return 0;
8222}
8223
8224
Willy Tarreaua15645d2007-03-18 16:22:39 +01008225/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008226 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008227 * desirable to call it only when needed. This function is also used when we
8228 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008229 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008230void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008231{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008232 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008233 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008234 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008235 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008236 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008237 char *hdr_beg, *hdr_end, *hdr_next;
8238 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008239
Willy Tarreaua15645d2007-03-18 16:22:39 +01008240 /* Iterate through the headers.
8241 * we start with the start line.
8242 */
8243 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008244 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008245
8246 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8247 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008248 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008249
8250 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008251 hdr_beg = hdr_next;
8252 hdr_end = hdr_beg + cur_hdr->len;
8253 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008254
Willy Tarreau24581ba2010-08-31 22:39:35 +02008255 /* We have one full header between hdr_beg and hdr_end, and the
8256 * next header starts at hdr_next. We're only interested in
8257 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008258 */
8259
Willy Tarreau24581ba2010-08-31 22:39:35 +02008260 is_cookie2 = 0;
8261 prev = hdr_beg + 10;
8262 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008263 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008264 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8265 if (!val) {
8266 old_idx = cur_idx;
8267 continue;
8268 }
8269 is_cookie2 = 1;
8270 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008271 }
8272
Willy Tarreau24581ba2010-08-31 22:39:35 +02008273 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8274 * <prev> points to the colon.
8275 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008276 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008277
Willy Tarreau24581ba2010-08-31 22:39:35 +02008278 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8279 * check-cache is enabled) and we are not interested in checking
8280 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008281 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008282 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008283 return;
8284
Willy Tarreau24581ba2010-08-31 22:39:35 +02008285 /* OK so now we know we have to process this response cookie.
8286 * The format of the Set-Cookie header is slightly different
8287 * from the format of the Cookie header in that it does not
8288 * support the comma as a cookie delimiter (thus the header
8289 * cannot be folded) because the Expires attribute described in
8290 * the original Netscape's spec may contain an unquoted date
8291 * with a comma inside. We have to live with this because
8292 * many browsers don't support Max-Age and some browsers don't
8293 * support quoted strings. However the Set-Cookie2 header is
8294 * clean.
8295 *
8296 * We have to keep multiple pointers in order to support cookie
8297 * removal at the beginning, middle or end of header without
8298 * corrupting the header (in case of set-cookie2). A special
8299 * pointer, <scav> points to the beginning of the set-cookie-av
8300 * fields after the first semi-colon. The <next> pointer points
8301 * either to the end of line (set-cookie) or next unquoted comma
8302 * (set-cookie2). All of these headers are valid :
8303 *
8304 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8305 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8306 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8307 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8308 * | | | | | | | | | |
8309 * | | | | | | | | +-> next hdr_end <--+
8310 * | | | | | | | +------------> scav
8311 * | | | | | | +--------------> val_end
8312 * | | | | | +--------------------> val_beg
8313 * | | | | +----------------------> equal
8314 * | | | +------------------------> att_end
8315 * | | +----------------------------> att_beg
8316 * | +------------------------------> prev
8317 * +-----------------------------------------> hdr_beg
8318 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008319
Willy Tarreau24581ba2010-08-31 22:39:35 +02008320 for (; prev < hdr_end; prev = next) {
8321 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008322
Willy Tarreau24581ba2010-08-31 22:39:35 +02008323 /* find att_beg */
8324 att_beg = prev + 1;
8325 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8326 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008327
Willy Tarreau24581ba2010-08-31 22:39:35 +02008328 /* find att_end : this is the first character after the last non
8329 * space before the equal. It may be equal to hdr_end.
8330 */
8331 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008332
Willy Tarreau24581ba2010-08-31 22:39:35 +02008333 while (equal < hdr_end) {
8334 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8335 break;
8336 if (http_is_spht[(unsigned char)*equal++])
8337 continue;
8338 att_end = equal;
8339 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008340
Willy Tarreau24581ba2010-08-31 22:39:35 +02008341 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8342 * is between <att_beg> and <equal>, both may be identical.
8343 */
8344
8345 /* look for end of cookie if there is an equal sign */
8346 if (equal < hdr_end && *equal == '=') {
8347 /* look for the beginning of the value */
8348 val_beg = equal + 1;
8349 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8350 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008351
Willy Tarreau24581ba2010-08-31 22:39:35 +02008352 /* find the end of the value, respecting quotes */
8353 next = find_cookie_value_end(val_beg, hdr_end);
8354
8355 /* make val_end point to the first white space or delimitor after the value */
8356 val_end = next;
8357 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8358 val_end--;
8359 } else {
8360 /* <equal> points to next comma, semi-colon or EOL */
8361 val_beg = val_end = next = equal;
8362 }
8363
8364 if (next < hdr_end) {
8365 /* Set-Cookie2 supports multiple cookies, and <next> points to
8366 * a colon or semi-colon before the end. So skip all attr-value
8367 * pairs and look for the next comma. For Set-Cookie, since
8368 * commas are permitted in values, skip to the end.
8369 */
8370 if (is_cookie2)
8371 next = find_hdr_value_end(next, hdr_end);
8372 else
8373 next = hdr_end;
8374 }
8375
8376 /* Now everything is as on the diagram above */
8377
8378 /* Ignore cookies with no equal sign */
8379 if (equal == val_end)
8380 continue;
8381
8382 /* If there are spaces around the equal sign, we need to
8383 * strip them otherwise we'll get trouble for cookie captures,
8384 * or even for rewrites. Since this happens extremely rarely,
8385 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008386 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008387 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8388 int stripped_before = 0;
8389 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008390
Willy Tarreau24581ba2010-08-31 22:39:35 +02008391 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008392 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008393 equal += stripped_before;
8394 val_beg += stripped_before;
8395 }
8396
8397 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008398 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008399 val_beg += stripped_after;
8400 stripped_before += stripped_after;
8401 }
8402
8403 val_end += stripped_before;
8404 next += stripped_before;
8405 hdr_end += stripped_before;
8406 hdr_next += stripped_before;
8407 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008408 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008409 }
8410
8411 /* First, let's see if we want to capture this cookie. We check
8412 * that we don't already have a server side cookie, because we
8413 * can only capture one. Also as an optimisation, we ignore
8414 * cookies shorter than the declared name.
8415 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008416 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008417 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008418 (val_end - att_beg >= sess->fe->capture_namelen) &&
8419 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008420 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008421 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008422 Alert("HTTP logging : out of memory.\n");
8423 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008424 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008425 if (log_len > sess->fe->capture_len)
8426 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008427 memcpy(txn->srv_cookie, att_beg, log_len);
8428 txn->srv_cookie[log_len] = 0;
8429 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008430 }
8431
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008432 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008433 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008434 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008435 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8436 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008437 /* assume passive cookie by default */
8438 txn->flags &= ~TX_SCK_MASK;
8439 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008440
8441 /* If the cookie is in insert mode on a known server, we'll delete
8442 * this occurrence because we'll insert another one later.
8443 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008444 * a direct access.
8445 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008446 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008447 /* The "preserve" flag was set, we don't want to touch the
8448 * server's cookie.
8449 */
8450 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008451 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008452 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008453 /* this cookie must be deleted */
8454 if (*prev == ':' && next == hdr_end) {
8455 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008456 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008457 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8458 txn->hdr_idx.used--;
8459 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008460 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008461 hdr_next += delta;
8462 http_msg_move_end(&txn->rsp, delta);
8463 /* note: while both invalid now, <next> and <hdr_end>
8464 * are still equal, so the for() will stop as expected.
8465 */
8466 } else {
8467 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008468 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008469 next = prev;
8470 hdr_end += delta;
8471 hdr_next += delta;
8472 cur_hdr->len += delta;
8473 http_msg_move_end(&txn->rsp, delta);
8474 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008475 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008476 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008477 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008478 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008479 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008480 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008481 * with this server since we know it.
8482 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008483 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008484 next += delta;
8485 hdr_end += delta;
8486 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008487 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008488 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008489
Willy Tarreauf1348312010-10-07 15:54:11 +02008490 txn->flags &= ~TX_SCK_MASK;
8491 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008492 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008493 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008494 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008495 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008496 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008497 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008498 next += delta;
8499 hdr_end += delta;
8500 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008501 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008502 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008503
Willy Tarreau827aee92011-03-10 16:55:02 +01008504 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008505 txn->flags &= ~TX_SCK_MASK;
8506 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008507 }
8508 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008509 /* that's done for this cookie, check the next one on the same
8510 * line when next != hdr_end (only if is_cookie2).
8511 */
8512 }
8513 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008514 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008515 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008516}
8517
8518
Willy Tarreaua15645d2007-03-18 16:22:39 +01008519/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008520 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008521 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008522void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008523{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008524 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008525 char *p1, *p2;
8526
8527 char *cur_ptr, *cur_end, *cur_next;
8528 int cur_idx;
8529
Willy Tarreau5df51872007-11-25 16:20:08 +01008530 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008531 return;
8532
8533 /* Iterate through the headers.
8534 * we start with the start line.
8535 */
8536 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008537 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008538
8539 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8540 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008541 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008542
8543 cur_hdr = &txn->hdr_idx.v[cur_idx];
8544 cur_ptr = cur_next;
8545 cur_end = cur_ptr + cur_hdr->len;
8546 cur_next = cur_end + cur_hdr->cr + 1;
8547
8548 /* We have one full header between cur_ptr and cur_end, and the
8549 * next header starts at cur_next. We're only interested in
8550 * "Cookie:" headers.
8551 */
8552
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008553 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8554 if (val) {
8555 if ((cur_end - (cur_ptr + val) >= 8) &&
8556 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8557 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8558 return;
8559 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008560 }
8561
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008562 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8563 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008564 continue;
8565
8566 /* OK, right now we know we have a cache-control header at cur_ptr */
8567
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008568 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008569
8570 if (p1 >= cur_end) /* no more info */
8571 continue;
8572
8573 /* p1 is at the beginning of the value */
8574 p2 = p1;
8575
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008576 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008577 p2++;
8578
8579 /* we have a complete value between p1 and p2 */
8580 if (p2 < cur_end && *p2 == '=') {
8581 /* we have something of the form no-cache="set-cookie" */
8582 if ((cur_end - p1 >= 21) &&
8583 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8584 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008585 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008586 continue;
8587 }
8588
8589 /* OK, so we know that either p2 points to the end of string or to a comma */
8590 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008591 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008592 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8593 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8594 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008595 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008596 return;
8597 }
8598
8599 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008600 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008601 continue;
8602 }
8603 }
8604}
8605
Willy Tarreau58f10d72006-12-04 02:26:12 +01008606
Willy Tarreaub2513902006-12-17 14:52:38 +01008607/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008608 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008609 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008610 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008611 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008612 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008613 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008614 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008615 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008616int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008617{
8618 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008619 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008620 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008621
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008622 if (!uri_auth)
8623 return 0;
8624
Cyril Bonté70be45d2010-10-12 00:14:35 +02008625 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008626 return 0;
8627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008628 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008629 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008630 return 0;
8631
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008632 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008633 return 0;
8634
Willy Tarreaub2513902006-12-17 14:52:38 +01008635 return 1;
8636}
8637
Willy Tarreau4076a152009-04-02 15:18:36 +02008638/*
8639 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008640 * By default it tries to report the error position as msg->err_pos. However if
8641 * this one is not set, it will then report msg->next, which is the last known
8642 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008643 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008644 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008645void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008646 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008647 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008648{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008649 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008650 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008651 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008652
Willy Tarreau9b28e032012-10-12 23:49:43 +02008653 es->len = MIN(chn->buf->i, sizeof(es->buf));
8654 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008655 len1 = MIN(len1, es->len);
8656 len2 = es->len - len1; /* remaining data if buffer wraps */
8657
Willy Tarreau9b28e032012-10-12 23:49:43 +02008658 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008659 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008660 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008661
Willy Tarreau4076a152009-04-02 15:18:36 +02008662 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008663 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008664 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008665 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008666
Willy Tarreau4076a152009-04-02 15:18:36 +02008667 es->when = date; // user-visible date
8668 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008669 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008670 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008671 if (objt_conn(sess->origin))
8672 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008673 else
8674 memset(&es->src, 0, sizeof(es->src));
8675
Willy Tarreau078272e2010-12-12 12:46:33 +01008676 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008677 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008678 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008679 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008680 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008681 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008682 es->b_out = chn->buf->o;
8683 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008684 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008685 es->m_clen = msg->chunk_len;
8686 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008687}
Willy Tarreaub2513902006-12-17 14:52:38 +01008688
Willy Tarreau294c4732011-12-16 21:35:50 +01008689/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8690 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8691 * performed over the whole headers. Otherwise it must contain a valid header
8692 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8693 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8694 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8695 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008696 * -1. The value fetch stops at commas, so this function is suited for use with
8697 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008698 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008699 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008700unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008701 struct hdr_idx *idx, int occ,
8702 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008703{
Willy Tarreau294c4732011-12-16 21:35:50 +01008704 struct hdr_ctx local_ctx;
8705 char *ptr_hist[MAX_HDR_HISTORY];
8706 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008707 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008708 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008709
Willy Tarreau294c4732011-12-16 21:35:50 +01008710 if (!ctx) {
8711 local_ctx.idx = 0;
8712 ctx = &local_ctx;
8713 }
8714
Willy Tarreaubce70882009-09-07 11:51:47 +02008715 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008716 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008717 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008718 occ--;
8719 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008720 *vptr = ctx->line + ctx->val;
8721 *vlen = ctx->vlen;
8722 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008723 }
8724 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008725 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008726 }
8727
8728 /* negative occurrence, we scan all the list then walk back */
8729 if (-occ > MAX_HDR_HISTORY)
8730 return 0;
8731
Willy Tarreau294c4732011-12-16 21:35:50 +01008732 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008733 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008734 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8735 len_hist[hist_ptr] = ctx->vlen;
8736 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008737 hist_ptr = 0;
8738 found++;
8739 }
8740 if (-occ > found)
8741 return 0;
8742 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008743 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8744 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8745 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008746 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008747 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008748 if (hist_ptr >= MAX_HDR_HISTORY)
8749 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008750 *vptr = ptr_hist[hist_ptr];
8751 *vlen = len_hist[hist_ptr];
8752 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008753}
8754
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008755/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8756 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8757 * performed over the whole headers. Otherwise it must contain a valid header
8758 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8759 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8760 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8761 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8762 * -1. This function differs from http_get_hdr() in that it only returns full
8763 * line header values and does not stop at commas.
8764 * The return value is 0 if nothing was found, or non-zero otherwise.
8765 */
8766unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8767 struct hdr_idx *idx, int occ,
8768 struct hdr_ctx *ctx, char **vptr, int *vlen)
8769{
8770 struct hdr_ctx local_ctx;
8771 char *ptr_hist[MAX_HDR_HISTORY];
8772 int len_hist[MAX_HDR_HISTORY];
8773 unsigned int hist_ptr;
8774 int found;
8775
8776 if (!ctx) {
8777 local_ctx.idx = 0;
8778 ctx = &local_ctx;
8779 }
8780
8781 if (occ >= 0) {
8782 /* search from the beginning */
8783 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8784 occ--;
8785 if (occ <= 0) {
8786 *vptr = ctx->line + ctx->val;
8787 *vlen = ctx->vlen;
8788 return 1;
8789 }
8790 }
8791 return 0;
8792 }
8793
8794 /* negative occurrence, we scan all the list then walk back */
8795 if (-occ > MAX_HDR_HISTORY)
8796 return 0;
8797
8798 found = hist_ptr = 0;
8799 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8800 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8801 len_hist[hist_ptr] = ctx->vlen;
8802 if (++hist_ptr >= MAX_HDR_HISTORY)
8803 hist_ptr = 0;
8804 found++;
8805 }
8806 if (-occ > found)
8807 return 0;
8808 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8809 * find occurrence -occ, so we have to check [hist_ptr+occ].
8810 */
8811 hist_ptr += occ;
8812 if (hist_ptr >= MAX_HDR_HISTORY)
8813 hist_ptr -= MAX_HDR_HISTORY;
8814 *vptr = ptr_hist[hist_ptr];
8815 *vlen = len_hist[hist_ptr];
8816 return 1;
8817}
8818
Willy Tarreaubaaee002006-06-26 02:48:02 +02008819/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008820 * Print a debug line with a header. Always stop at the first CR or LF char,
8821 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8822 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008823 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008824void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008825{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008826 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008827 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008828
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008829 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008830 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008831 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008832 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 +02008833
8834 for (max = 0; start + max < end; max++)
8835 if (start[max] == '\r' || start[max] == '\n')
8836 break;
8837
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008838 UBOUND(max, trash.size - trash.len - 3);
8839 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8840 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008841 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008842}
8843
Willy Tarreaueee5b512015-04-03 23:46:31 +02008844
8845/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8846 * The hdr_idx is allocated as well. In case of allocation failure, everything
8847 * allocated is freed and NULL is returned. Otherwise the new transaction is
8848 * assigned to the stream and returned.
8849 */
8850struct http_txn *http_alloc_txn(struct stream *s)
8851{
8852 struct http_txn *txn = s->txn;
8853
8854 if (txn)
8855 return txn;
8856
8857 txn = pool_alloc2(pool2_http_txn);
8858 if (!txn)
8859 return txn;
8860
8861 txn->hdr_idx.size = global.tune.max_http_hdr;
8862 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8863 if (!txn->hdr_idx.v) {
8864 pool_free2(pool2_http_txn, txn);
8865 return NULL;
8866 }
8867
8868 s->txn = txn;
8869 return txn;
8870}
8871
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008872void http_txn_reset_req(struct http_txn *txn)
8873{
8874 txn->req.flags = 0;
8875 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8876 txn->req.next = 0;
8877 txn->req.chunk_len = 0LL;
8878 txn->req.body_len = 0LL;
8879 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8880}
8881
8882void http_txn_reset_res(struct http_txn *txn)
8883{
8884 txn->rsp.flags = 0;
8885 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8886 txn->rsp.next = 0;
8887 txn->rsp.chunk_len = 0LL;
8888 txn->rsp.body_len = 0LL;
8889 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8890}
8891
Willy Tarreau0937bc42009-12-22 15:03:09 +01008892/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008893 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008894 * the required fields are properly allocated and that we only need to (re)init
8895 * them. This should be used before processing any new request.
8896 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008897void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008898{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008899 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008900 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008901
8902 txn->flags = 0;
8903 txn->status = -1;
8904
Willy Tarreauf64d1412010-10-07 20:06:11 +02008905 txn->cookie_first_date = 0;
8906 txn->cookie_last_date = 0;
8907
Willy Tarreaueee5b512015-04-03 23:46:31 +02008908 txn->srv_cookie = NULL;
8909 txn->cli_cookie = NULL;
8910 txn->uri = NULL;
8911
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008912 http_txn_reset_req(txn);
8913 http_txn_reset_res(txn);
8914
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008915 txn->req.chn = &s->req;
8916 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008917
8918 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008919
8920 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8921 if (fe->options2 & PR_O2_REQBUG_OK)
8922 txn->req.err_pos = -1; /* let buggy requests pass */
8923
Willy Tarreau0937bc42009-12-22 15:03:09 +01008924 if (txn->hdr_idx.v)
8925 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008926
8927 vars_init(&s->vars_txn, SCOPE_TXN);
8928 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008929}
8930
8931/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008932void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008933{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008934 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008935 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008936
Willy Tarreau75195602014-03-11 15:48:55 +01008937 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008938 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008939 s->comp_algo->end(&s->comp_ctx);
8940 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008941 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008942
Willy Tarreau0937bc42009-12-22 15:03:09 +01008943 /* these ones will have been dynamically allocated */
8944 pool_free2(pool2_requri, txn->uri);
8945 pool_free2(pool2_capture, txn->cli_cookie);
8946 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008947 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008948
William Lallemanda73203e2012-03-12 12:48:57 +01008949 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008950 txn->uri = NULL;
8951 txn->srv_cookie = NULL;
8952 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008953
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008954 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008955 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008956 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008957 pool_free2(h->pool, s->req_cap[h->index]);
8958 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008959 }
8960
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008961 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008962 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008963 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008964 pool_free2(h->pool, s->res_cap[h->index]);
8965 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008966 }
8967
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008968 vars_prune(&s->vars_txn, s);
8969 vars_prune(&s->vars_reqres, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008970}
8971
8972/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008973void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008974{
8975 http_end_txn(s);
8976 http_init_txn(s);
8977
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008978 /* reinitialise the current rule list pointer to NULL. We are sure that
8979 * any rulelist match the NULL pointer.
8980 */
8981 s->current_rule_list = NULL;
8982
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008983 s->be = strm_fe(s);
8984 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008985 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008986 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008987 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008988 /* re-init store persistence */
8989 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008990 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008991
Willy Tarreau0937bc42009-12-22 15:03:09 +01008992 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008993
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008994 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008995
Willy Tarreau739cfba2010-01-25 23:11:14 +01008996 /* We must trim any excess data from the response buffer, because we
8997 * may have blocked an invalid response from a server that we don't
8998 * want to accidentely forward once we disable the analysers, nor do
8999 * we want those data to come along with next response. A typical
9000 * example of such data would be from a buggy server responding to
9001 * a HEAD with some data, or sending more than the advertised
9002 * content-length.
9003 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009004 if (unlikely(s->res.buf->i))
9005 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01009006
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009007 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009008 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009009
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009010 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009011 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009012
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009013 s->req.rex = TICK_ETERNITY;
9014 s->req.wex = TICK_ETERNITY;
9015 s->req.analyse_exp = TICK_ETERNITY;
9016 s->res.rex = TICK_ETERNITY;
9017 s->res.wex = TICK_ETERNITY;
9018 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009019}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009020
Sasha Pachev218f0642014-06-16 12:05:59 -06009021void free_http_res_rules(struct list *r)
9022{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009023 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06009024
9025 list_for_each_entry_safe(pr, tr, r, list) {
9026 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009027 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009028 free(pr);
9029 }
9030}
9031
9032void free_http_req_rules(struct list *r)
9033{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009034 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009035
9036 list_for_each_entry_safe(pr, tr, r, list) {
9037 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009038 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009039 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009040
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009041 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009042 free(pr);
9043 }
9044}
9045
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009046/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009047struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009048{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009049 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009050 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009051 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009052 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009053
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009054 rule = (struct act_rule*)calloc(1, sizeof(struct act_rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009055 if (!rule) {
9056 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009057 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009058 }
9059
CJ Ess108b1dd2015-04-07 12:03:37 -04009060 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009061 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009062 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009063 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009064 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009065 int code;
9066 int hc;
9067
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009068 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01009069 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009070 if (strcmp(args[cur_arg], "deny_status") == 0) {
9071 cur_arg++;
9072 if (!args[cur_arg]) {
9073 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9074 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9075 goto out_err;
9076 }
9077
9078 code = atol(args[cur_arg]);
9079 cur_arg++;
9080 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9081 if (http_err_codes[hc] == code) {
9082 rule->deny_status = hc;
9083 break;
9084 }
9085 }
9086
9087 if (hc >= HTTP_ERR_SIZE) {
9088 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9089 file, linenum, code);
9090 }
9091 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009092 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009093 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009094 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009095 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009096 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009097 cur_arg = 1;
9098
9099 while(*args[cur_arg]) {
9100 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009101 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009102 cur_arg+=2;
9103 continue;
9104 } else
9105 break;
9106 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009107 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009108 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009109 cur_arg = 1;
9110
9111 if (!*args[cur_arg] ||
9112 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9113 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9114 file, linenum, args[0]);
9115 goto out_err;
9116 }
9117 rule->arg.nice = atoi(args[cur_arg]);
9118 if (rule->arg.nice < -1024)
9119 rule->arg.nice = -1024;
9120 else if (rule->arg.nice > 1024)
9121 rule->arg.nice = 1024;
9122 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009123 } else if (!strcmp(args[0], "set-tos")) {
9124#ifdef IP_TOS
9125 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009126 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009127 cur_arg = 1;
9128
9129 if (!*args[cur_arg] ||
9130 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9131 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9132 file, linenum, args[0]);
9133 goto out_err;
9134 }
9135
9136 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9137 if (err && *err != '\0') {
9138 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9139 file, linenum, err, args[0]);
9140 goto out_err;
9141 }
9142 cur_arg++;
9143#else
9144 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9145 goto out_err;
9146#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009147 } else if (!strcmp(args[0], "set-mark")) {
9148#ifdef SO_MARK
9149 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009150 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009151 cur_arg = 1;
9152
9153 if (!*args[cur_arg] ||
9154 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9155 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9156 file, linenum, args[0]);
9157 goto out_err;
9158 }
9159
9160 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9161 if (err && *err != '\0') {
9162 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9163 file, linenum, err, args[0]);
9164 goto out_err;
9165 }
9166 cur_arg++;
9167 global.last_checks |= LSTCHK_NETADM;
9168#else
9169 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9170 goto out_err;
9171#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009172 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009173 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009174 cur_arg = 1;
9175
9176 if (!*args[cur_arg] ||
9177 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9178 bad_log_level:
9179 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9180 file, linenum, args[0]);
9181 goto out_err;
9182 }
9183 if (strcmp(args[cur_arg], "silent") == 0)
9184 rule->arg.loglevel = -1;
9185 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9186 goto bad_log_level;
9187 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009188 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009189 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009190 cur_arg = 1;
9191
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009192 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9193 (*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 +01009194 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9195 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009196 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009197 }
9198
9199 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9200 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9201 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009202
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009203 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009204 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009205 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9206 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009207 free(proxy->conf.lfs_file);
9208 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9209 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009210 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009211 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009212 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009213 cur_arg = 1;
9214
9215 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009216 (*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 -06009217 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9218 file, linenum, args[0]);
9219 goto out_err;
9220 }
9221
9222 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9223 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9224 LIST_INIT(&rule->arg.hdr_add.fmt);
9225
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009226 error = NULL;
9227 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9228 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9229 args[cur_arg + 1], error);
9230 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009231 goto out_err;
9232 }
9233
9234 proxy->conf.args.ctx = ARGC_HRQ;
9235 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9236 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9237 file, linenum);
9238
9239 free(proxy->conf.lfs_file);
9240 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9241 proxy->conf.lfs_line = proxy->conf.args.line;
9242 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009243 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009244 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009245 cur_arg = 1;
9246
9247 if (!*args[cur_arg] ||
9248 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9249 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9250 file, linenum, args[0]);
9251 goto out_err;
9252 }
9253
9254 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9255 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9256
9257 proxy->conf.args.ctx = ARGC_HRQ;
9258 free(proxy->conf.lfs_file);
9259 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9260 proxy->conf.lfs_line = proxy->conf.args.line;
9261 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009262 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9263 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009264 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009265 struct sample_expr *expr;
9266 unsigned int where;
9267 char *err = NULL;
9268
9269 cur_arg = 1;
9270 proxy->conf.args.ctx = ARGC_TRK;
9271
9272 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9273 if (!expr) {
9274 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9275 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9276 free(err);
9277 goto out_err;
9278 }
9279
9280 where = 0;
9281 if (proxy->cap & PR_CAP_FE)
9282 where |= SMP_VAL_FE_HRQ_HDR;
9283 if (proxy->cap & PR_CAP_BE)
9284 where |= SMP_VAL_BE_HRQ_HDR;
9285
9286 if (!(expr->fetch->val & where)) {
9287 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9288 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9289 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9290 args[cur_arg-1], sample_src_names(expr->fetch->use));
9291 free(expr);
9292 goto out_err;
9293 }
9294
9295 if (strcmp(args[cur_arg], "table") == 0) {
9296 cur_arg++;
9297 if (!args[cur_arg]) {
9298 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9299 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9300 free(expr);
9301 goto out_err;
9302 }
9303 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009304 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009305 cur_arg++;
9306 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009307 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009308 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009309 } else if (strcmp(args[0], "redirect") == 0) {
9310 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009311 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009312
Willy Tarreaube4653b2015-05-28 15:26:58 +02009313 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009314 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9315 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9316 goto out_err;
9317 }
9318
9319 /* this redirect rule might already contain a parsed condition which
9320 * we'll pass to the http-request rule.
9321 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009322 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009323 rule->arg.redir = redir;
9324 rule->cond = redir->cond;
9325 redir->cond = NULL;
9326 cur_arg = 2;
9327 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009328 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9329 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009330 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009331 /*
9332 * '+ 8' for 'add-acl('
9333 * '- 9' for 'add-acl(' + trailing ')'
9334 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009335 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009336
9337 cur_arg = 1;
9338
9339 if (!*args[cur_arg] ||
9340 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9341 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9342 file, linenum, args[0]);
9343 goto out_err;
9344 }
9345
9346 LIST_INIT(&rule->arg.map.key);
9347 proxy->conf.args.ctx = ARGC_HRQ;
9348 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9349 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9350 file, linenum);
9351 free(proxy->conf.lfs_file);
9352 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9353 proxy->conf.lfs_line = proxy->conf.args.line;
9354 cur_arg += 1;
9355 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9356 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009357 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009358 /*
9359 * '+ 8' for 'del-acl('
9360 * '- 9' for 'del-acl(' + trailing ')'
9361 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009362 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009363
9364 cur_arg = 1;
9365
9366 if (!*args[cur_arg] ||
9367 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9368 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9369 file, linenum, args[0]);
9370 goto out_err;
9371 }
9372
9373 LIST_INIT(&rule->arg.map.key);
9374 proxy->conf.args.ctx = ARGC_HRQ;
9375 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9376 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9377 file, linenum);
9378 free(proxy->conf.lfs_file);
9379 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9380 proxy->conf.lfs_line = proxy->conf.args.line;
9381 cur_arg += 1;
9382 } else if (strncmp(args[0], "del-map", 7) == 0) {
9383 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009384 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009385 /*
9386 * '+ 8' for 'del-map('
9387 * '- 9' for 'del-map(' + trailing ')'
9388 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009389 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009390
9391 cur_arg = 1;
9392
9393 if (!*args[cur_arg] ||
9394 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9395 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9396 file, linenum, args[0]);
9397 goto out_err;
9398 }
9399
9400 LIST_INIT(&rule->arg.map.key);
9401 proxy->conf.args.ctx = ARGC_HRQ;
9402 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9403 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9404 file, linenum);
9405 free(proxy->conf.lfs_file);
9406 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9407 proxy->conf.lfs_line = proxy->conf.args.line;
9408 cur_arg += 1;
9409 } else if (strncmp(args[0], "set-map", 7) == 0) {
9410 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009411 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009412 /*
9413 * '+ 8' for 'set-map('
9414 * '- 9' for 'set-map(' + trailing ')'
9415 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009416 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009417
9418 cur_arg = 1;
9419
9420 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9421 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9422 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9423 file, linenum, args[0]);
9424 goto out_err;
9425 }
9426
9427 LIST_INIT(&rule->arg.map.key);
9428 LIST_INIT(&rule->arg.map.value);
9429 proxy->conf.args.ctx = ARGC_HRQ;
9430
9431 /* key pattern */
9432 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9433 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9434 file, linenum);
9435
9436 /* value pattern */
9437 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9438 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9439 file, linenum);
9440 free(proxy->conf.lfs_file);
9441 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9442 proxy->conf.lfs_line = proxy->conf.args.line;
9443
9444 cur_arg += 2;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009445 } else if (strncmp(args[0], "set-src", 7) == 0) {
9446 struct sample_expr *expr;
9447 unsigned int where;
9448 char *err = NULL;
9449
9450 cur_arg = 1;
9451 proxy->conf.args.ctx = ARGC_HRQ;
9452
9453 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9454 if (!expr) {
9455 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9456 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9457 free(err);
9458 goto out_err;
9459 }
9460
9461 where = 0;
9462 if (proxy->cap & PR_CAP_FE)
9463 where |= SMP_VAL_FE_HRQ_HDR;
9464 if (proxy->cap & PR_CAP_BE)
9465 where |= SMP_VAL_BE_HRQ_HDR;
9466
9467 if (!(expr->fetch->val & where)) {
9468 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9469 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9470 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9471 args[cur_arg-1], sample_src_names(expr->fetch->use));
9472 free(expr);
9473 goto out_err;
9474 }
9475
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02009476 rule->arg.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009477 rule->action = ACT_HTTP_REQ_SET_SRC;
William Lallemand73025dd2014-04-24 14:38:37 +02009478 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9479 char *errmsg = NULL;
9480 cur_arg = 1;
9481 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009482 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009483 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009484 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009485 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9486 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9487 free(errmsg);
9488 goto out_err;
9489 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009490 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009491 action_build_list(&http_req_keywords.list, &trash);
9492 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9493 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9494 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', "
9495 "'set-src'%s%s, but got '%s'%s.\n",
9496 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009497 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009498 }
9499
9500 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9501 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009502 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009503
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009504 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9505 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9506 file, linenum, args[0], errmsg);
9507 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009508 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009509 }
9510 rule->cond = cond;
9511 }
9512 else if (*args[cur_arg]) {
9513 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9514 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9515 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009516 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009517 }
9518
9519 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009520 out_err:
9521 free(rule);
9522 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009523}
9524
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009525/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009526struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009527{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009528 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009529 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009530 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009531 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009532
9533 rule = calloc(1, sizeof(*rule));
9534 if (!rule) {
9535 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9536 goto out_err;
9537 }
9538
9539 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009540 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009541 cur_arg = 1;
9542 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009543 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009544 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009545 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009546 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009547 cur_arg = 1;
9548
9549 if (!*args[cur_arg] ||
9550 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9551 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9552 file, linenum, args[0]);
9553 goto out_err;
9554 }
9555 rule->arg.nice = atoi(args[cur_arg]);
9556 if (rule->arg.nice < -1024)
9557 rule->arg.nice = -1024;
9558 else if (rule->arg.nice > 1024)
9559 rule->arg.nice = 1024;
9560 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009561 } else if (!strcmp(args[0], "set-tos")) {
9562#ifdef IP_TOS
9563 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009564 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009565 cur_arg = 1;
9566
9567 if (!*args[cur_arg] ||
9568 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9569 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9570 file, linenum, args[0]);
9571 goto out_err;
9572 }
9573
9574 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9575 if (err && *err != '\0') {
9576 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9577 file, linenum, err, args[0]);
9578 goto out_err;
9579 }
9580 cur_arg++;
9581#else
9582 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9583 goto out_err;
9584#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009585 } else if (!strcmp(args[0], "set-mark")) {
9586#ifdef SO_MARK
9587 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009588 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009589 cur_arg = 1;
9590
9591 if (!*args[cur_arg] ||
9592 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9593 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9594 file, linenum, args[0]);
9595 goto out_err;
9596 }
9597
9598 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9599 if (err && *err != '\0') {
9600 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9601 file, linenum, err, args[0]);
9602 goto out_err;
9603 }
9604 cur_arg++;
9605 global.last_checks |= LSTCHK_NETADM;
9606#else
9607 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9608 goto out_err;
9609#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009610 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009611 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009612 cur_arg = 1;
9613
9614 if (!*args[cur_arg] ||
9615 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9616 bad_log_level:
9617 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9618 file, linenum, args[0]);
9619 goto out_err;
9620 }
9621 if (strcmp(args[cur_arg], "silent") == 0)
9622 rule->arg.loglevel = -1;
9623 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9624 goto bad_log_level;
9625 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009626 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009627 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009628 cur_arg = 1;
9629
9630 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9631 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9632 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9633 file, linenum, args[0]);
9634 goto out_err;
9635 }
9636
9637 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9638 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9639 LIST_INIT(&rule->arg.hdr_add.fmt);
9640
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009641 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009642 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009643 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9644 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009645 free(proxy->conf.lfs_file);
9646 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9647 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009648 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009649 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009650 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009651 cur_arg = 1;
9652
9653 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009654 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9655 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009656 file, linenum, args[0]);
9657 goto out_err;
9658 }
9659
9660 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9661 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9662 LIST_INIT(&rule->arg.hdr_add.fmt);
9663
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009664 error = NULL;
9665 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9666 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9667 args[cur_arg + 1], error);
9668 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009669 goto out_err;
9670 }
9671
9672 proxy->conf.args.ctx = ARGC_HRQ;
9673 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9674 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9675 file, linenum);
9676
9677 free(proxy->conf.lfs_file);
9678 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9679 proxy->conf.lfs_line = proxy->conf.args.line;
9680 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009681 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009682 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009683 cur_arg = 1;
9684
9685 if (!*args[cur_arg] ||
9686 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9687 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9688 file, linenum, args[0]);
9689 goto out_err;
9690 }
9691
9692 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9693 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9694
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009695 proxy->conf.args.ctx = ARGC_HRS;
9696 free(proxy->conf.lfs_file);
9697 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9698 proxy->conf.lfs_line = proxy->conf.args.line;
9699 cur_arg += 1;
9700 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9701 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009702 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009703 /*
9704 * '+ 8' for 'add-acl('
9705 * '- 9' for 'add-acl(' + trailing ')'
9706 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009707 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009708
9709 cur_arg = 1;
9710
9711 if (!*args[cur_arg] ||
9712 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9713 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9714 file, linenum, args[0]);
9715 goto out_err;
9716 }
9717
9718 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009719 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009720 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9721 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9722 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009723 free(proxy->conf.lfs_file);
9724 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9725 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009726
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009727 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009728 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9729 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009730 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009731 /*
9732 * '+ 8' for 'del-acl('
9733 * '- 9' for 'del-acl(' + trailing ')'
9734 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009735 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009736
9737 cur_arg = 1;
9738
9739 if (!*args[cur_arg] ||
9740 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9741 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9742 file, linenum, args[0]);
9743 goto out_err;
9744 }
9745
9746 LIST_INIT(&rule->arg.map.key);
9747 proxy->conf.args.ctx = ARGC_HRS;
9748 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9749 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9750 file, linenum);
9751 free(proxy->conf.lfs_file);
9752 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9753 proxy->conf.lfs_line = proxy->conf.args.line;
9754 cur_arg += 1;
9755 } else if (strncmp(args[0], "del-map", 7) == 0) {
9756 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009757 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009758 /*
9759 * '+ 8' for 'del-map('
9760 * '- 9' for 'del-map(' + trailing ')'
9761 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009762 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009763
9764 cur_arg = 1;
9765
9766 if (!*args[cur_arg] ||
9767 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9768 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9769 file, linenum, args[0]);
9770 goto out_err;
9771 }
9772
9773 LIST_INIT(&rule->arg.map.key);
9774 proxy->conf.args.ctx = ARGC_HRS;
9775 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9776 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9777 file, linenum);
9778 free(proxy->conf.lfs_file);
9779 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9780 proxy->conf.lfs_line = proxy->conf.args.line;
9781 cur_arg += 1;
9782 } else if (strncmp(args[0], "set-map", 7) == 0) {
9783 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009784 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009785 /*
9786 * '+ 8' for 'set-map('
9787 * '- 9' for 'set-map(' + trailing ')'
9788 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009789 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009790
9791 cur_arg = 1;
9792
9793 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9794 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9795 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9796 file, linenum, args[0]);
9797 goto out_err;
9798 }
9799
9800 LIST_INIT(&rule->arg.map.key);
9801 LIST_INIT(&rule->arg.map.value);
9802
9803 proxy->conf.args.ctx = ARGC_HRS;
9804
9805 /* key pattern */
9806 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9807 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9808 file, linenum);
9809
9810 /* value pattern */
9811 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9812 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9813 file, linenum);
9814
9815 free(proxy->conf.lfs_file);
9816 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9817 proxy->conf.lfs_line = proxy->conf.args.line;
9818
9819 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009820 } else if (strcmp(args[0], "redirect") == 0) {
9821 struct redirect_rule *redir;
9822 char *errmsg = NULL;
9823
9824 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9825 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9826 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9827 goto out_err;
9828 }
9829
9830 /* this redirect rule might already contain a parsed condition which
9831 * we'll pass to the http-request rule.
9832 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009833 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009834 rule->arg.redir = redir;
9835 rule->cond = redir->cond;
9836 redir->cond = NULL;
9837 cur_arg = 2;
9838 return rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009839 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9840 char *errmsg = NULL;
9841 cur_arg = 1;
9842 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009843 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009844 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009845 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009846 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9847 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9848 free(errmsg);
9849 goto out_err;
9850 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009851 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009852 action_build_list(&http_res_keywords.list, &trash);
9853 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9854 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9855 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', "
9856 "'set-src'%s%s, but got '%s'%s.\n",
9857 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009858 goto out_err;
9859 }
9860
9861 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9862 struct acl_cond *cond;
9863 char *errmsg = NULL;
9864
9865 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9866 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9867 file, linenum, args[0], errmsg);
9868 free(errmsg);
9869 goto out_err;
9870 }
9871 rule->cond = cond;
9872 }
9873 else if (*args[cur_arg]) {
9874 Alert("parsing [%s:%d]: 'http-response %s' expects"
9875 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9876 file, linenum, args[0], args[cur_arg]);
9877 goto out_err;
9878 }
9879
9880 return rule;
9881 out_err:
9882 free(rule);
9883 return NULL;
9884}
9885
Willy Tarreau4baae242012-12-27 12:00:31 +01009886/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009887 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009888 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9889 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009890 */
9891struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009892 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009893{
9894 struct redirect_rule *rule;
9895 int cur_arg;
9896 int type = REDIRECT_TYPE_NONE;
9897 int code = 302;
9898 const char *destination = NULL;
9899 const char *cookie = NULL;
9900 int cookie_set = 0;
9901 unsigned int flags = REDIRECT_FLAG_NONE;
9902 struct acl_cond *cond = NULL;
9903
9904 cur_arg = 0;
9905 while (*(args[cur_arg])) {
9906 if (strcmp(args[cur_arg], "location") == 0) {
9907 if (!*args[cur_arg + 1])
9908 goto missing_arg;
9909
9910 type = REDIRECT_TYPE_LOCATION;
9911 cur_arg++;
9912 destination = args[cur_arg];
9913 }
9914 else if (strcmp(args[cur_arg], "prefix") == 0) {
9915 if (!*args[cur_arg + 1])
9916 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009917 type = REDIRECT_TYPE_PREFIX;
9918 cur_arg++;
9919 destination = args[cur_arg];
9920 }
9921 else if (strcmp(args[cur_arg], "scheme") == 0) {
9922 if (!*args[cur_arg + 1])
9923 goto missing_arg;
9924
9925 type = REDIRECT_TYPE_SCHEME;
9926 cur_arg++;
9927 destination = args[cur_arg];
9928 }
9929 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9930 if (!*args[cur_arg + 1])
9931 goto missing_arg;
9932
9933 cur_arg++;
9934 cookie = args[cur_arg];
9935 cookie_set = 1;
9936 }
9937 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9938 if (!*args[cur_arg + 1])
9939 goto missing_arg;
9940
9941 cur_arg++;
9942 cookie = args[cur_arg];
9943 cookie_set = 0;
9944 }
9945 else if (strcmp(args[cur_arg], "code") == 0) {
9946 if (!*args[cur_arg + 1])
9947 goto missing_arg;
9948
9949 cur_arg++;
9950 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009951 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009952 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009953 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009954 args[cur_arg - 1], args[cur_arg]);
9955 return NULL;
9956 }
9957 }
9958 else if (!strcmp(args[cur_arg],"drop-query")) {
9959 flags |= REDIRECT_FLAG_DROP_QS;
9960 }
9961 else if (!strcmp(args[cur_arg],"append-slash")) {
9962 flags |= REDIRECT_FLAG_APPEND_SLASH;
9963 }
9964 else if (strcmp(args[cur_arg], "if") == 0 ||
9965 strcmp(args[cur_arg], "unless") == 0) {
9966 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9967 if (!cond) {
9968 memprintf(errmsg, "error in condition: %s", *errmsg);
9969 return NULL;
9970 }
9971 break;
9972 }
9973 else {
9974 memprintf(errmsg,
9975 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9976 args[cur_arg]);
9977 return NULL;
9978 }
9979 cur_arg++;
9980 }
9981
9982 if (type == REDIRECT_TYPE_NONE) {
9983 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9984 return NULL;
9985 }
9986
Willy Tarreaube4653b2015-05-28 15:26:58 +02009987 if (dir && type != REDIRECT_TYPE_LOCATION) {
9988 memprintf(errmsg, "response only supports redirect type 'location'");
9989 return NULL;
9990 }
9991
Willy Tarreau4baae242012-12-27 12:00:31 +01009992 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9993 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009994 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009995
9996 if (!use_fmt) {
9997 /* old-style static redirect rule */
9998 rule->rdr_str = strdup(destination);
9999 rule->rdr_len = strlen(destination);
10000 }
10001 else {
10002 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010003
10004 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
10005 * if prefix == "/", we don't want to add anything, otherwise it
10006 * makes it hard for the user to configure a self-redirection.
10007 */
Godbachd9722032014-12-18 15:44:58 +080010008 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010009 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +010010010 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +020010011 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
10012 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +010010013 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010014 free(curproxy->conf.lfs_file);
10015 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10016 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010017 }
10018 }
10019
Willy Tarreau4baae242012-12-27 12:00:31 +010010020 if (cookie) {
10021 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10022 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10023 */
10024 rule->cookie_len = strlen(cookie);
10025 if (cookie_set) {
10026 rule->cookie_str = malloc(rule->cookie_len + 10);
10027 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10028 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10029 rule->cookie_len += 9;
10030 } else {
10031 rule->cookie_str = malloc(rule->cookie_len + 21);
10032 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10033 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10034 rule->cookie_len += 20;
10035 }
10036 }
10037 rule->type = type;
10038 rule->code = code;
10039 rule->flags = flags;
10040 LIST_INIT(&rule->list);
10041 return rule;
10042
10043 missing_arg:
10044 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10045 return NULL;
10046}
10047
Willy Tarreau8797c062007-05-07 00:55:35 +020010048/************************************************************************/
10049/* The code below is dedicated to ACL parsing and matching */
10050/************************************************************************/
10051
10052
Willy Tarreau14174bc2012-04-16 14:34:04 +020010053/* This function ensures that the prerequisites for an L7 fetch are ready,
10054 * which means that a request or response is ready. If some data is missing,
10055 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010056 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10057 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010058 *
10059 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010060 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10061 * decide whether or not an HTTP message is present ;
10062 * 0 if the requested data cannot be fetched or if it is certain that
10063 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010064 * 1 if an HTTP message is ready
10065 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010066int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010067 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010068{
Willy Tarreau192252e2015-04-04 01:47:55 +020010069 struct http_txn *txn;
10070 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010071
Willy Tarreaueee5b512015-04-03 23:46:31 +020010072 /* Note: this function may only be used from places where
10073 * http_init_txn() has already been done, and implies that <s>,
10074 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
10075 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010076 */
10077
Willy Tarreau192252e2015-04-04 01:47:55 +020010078 if (!s)
10079 return 0;
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010080 if (!s->txn) {
10081 if (unlikely(!http_alloc_txn(s)))
10082 return 0; /* not enough memory */
10083 http_init_txn(s);
10084 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010085 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010086 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010087
10088 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010089 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010090
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010091 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010092 /* If the buffer does not leave enough free space at the end,
10093 * we must first realign it.
10094 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010095 if (s->req.buf->p > s->req.buf->data &&
10096 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10097 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010098
Willy Tarreau14174bc2012-04-16 14:34:04 +020010099 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010100 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010101 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010102
10103 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010104 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010105 http_msg_analyzer(msg, &txn->hdr_idx);
10106
10107 /* Still no valid request ? */
10108 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010109 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010110 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010111 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010112 }
10113 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010114 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010115 return 0;
10116 }
10117
10118 /* OK we just got a valid HTTP request. We have some minor
10119 * preparation to perform so that further checks can rely
10120 * on HTTP tests.
10121 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010122
10123 /* If the request was parsed but was too large, we must absolutely
10124 * return an error so that it is not processed. At the moment this
10125 * cannot happen, but if the parsers are to change in the future,
10126 * we want this check to be maintained.
10127 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010128 if (unlikely(s->req.buf->i + s->req.buf->p >
10129 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010130 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010131 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010132 return 1;
10133 }
10134
Willy Tarreau9b28e032012-10-12 23:49:43 +020010135 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010136 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010137 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010138
Willy Tarreau506d0502013-07-06 13:29:24 +020010139 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10140 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010141 }
10142
Willy Tarreau506d0502013-07-06 13:29:24 +020010143 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010144 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010145 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010146
10147 /* otherwise everything's ready for the request */
10148 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010149 else {
10150 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010151 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10152 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010153 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010154 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010155 }
10156
10157 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010158 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010159 return 1;
10160}
Willy Tarreau8797c062007-05-07 00:55:35 +020010161
Willy Tarreau8797c062007-05-07 00:55:35 +020010162/* 1. Check on METHOD
10163 * We use the pre-parsed method if it is known, and store its number as an
10164 * integer. If it is unknown, we use the pointer and the length.
10165 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010166static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010167{
10168 int len, meth;
10169
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010170 len = strlen(text);
10171 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010172
10173 pattern->val.i = meth;
10174 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010175 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010176 pattern->len = len;
10177 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010178 else {
10179 pattern->ptr.str = NULL;
10180 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010181 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010182 return 1;
10183}
10184
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010185/* This function fetches the method of current HTTP request and stores
10186 * it in the global pattern struct as a chunk. There are two possibilities :
10187 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10188 * in <len> and <ptr> is NULL ;
10189 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10190 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010191 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010192 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010193static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010194smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010195{
10196 int meth;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010197 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010198
Willy Tarreau24e32d82012-04-23 23:55:44 +020010199 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010200
Willy Tarreau8797c062007-05-07 00:55:35 +020010201 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010202 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010203 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010204 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010205 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10206 /* ensure the indexes are not affected */
10207 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010208 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010209 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10210 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010211 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010212 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010213 return 1;
10214}
10215
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010216/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010217static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010218{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010219 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010220 struct pattern_list *lst;
10221 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010222
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010223 list_for_each_entry(lst, &expr->patterns, list) {
10224 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010225
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010226 /* well-known method */
10227 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010228 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010229 return pattern;
10230 else
10231 continue;
10232 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010233
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010234 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010235 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010236 continue;
10237
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010238 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010239 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10240 (!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 +010010241 return pattern;
10242 }
10243 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010244}
10245
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010246static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010247smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010248{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010249 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010250 char *ptr;
10251 int len;
10252
Willy Tarreauc0239e02012-04-16 14:42:55 +020010253 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010254
Willy Tarreau8797c062007-05-07 00:55:35 +020010255 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010256 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010257
10258 while ((len-- > 0) && (*ptr++ != '/'));
10259 if (len <= 0)
10260 return 0;
10261
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010262 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010263 smp->data.u.str.str = ptr;
10264 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010265
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010266 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010267 return 1;
10268}
10269
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010270static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010271smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010272{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010273 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010274 char *ptr;
10275 int len;
10276
Willy Tarreauc0239e02012-04-16 14:42:55 +020010277 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010278
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010279 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010280 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10281 return 0;
10282
Willy Tarreau8797c062007-05-07 00:55:35 +020010283 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010284 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010285
10286 while ((len-- > 0) && (*ptr++ != '/'));
10287 if (len <= 0)
10288 return 0;
10289
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010290 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010291 smp->data.u.str.str = ptr;
10292 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010293
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010294 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010295 return 1;
10296}
10297
10298/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010299static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010300smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010301{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010302 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010303 char *ptr;
10304 int len;
10305
Willy Tarreauc0239e02012-04-16 14:42:55 +020010306 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010307
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010308 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010309 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10310 return 0;
10311
Willy Tarreau8797c062007-05-07 00:55:35 +020010312 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010313 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010314
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010315 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010316 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010317 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010318 return 1;
10319}
10320
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010321/* returns the longest available part of the body. This requires that the body
10322 * has been waited for using http-buffer-request.
10323 */
10324static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010325smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010326{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010327 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010328 struct http_msg *msg;
10329 unsigned long len;
10330 unsigned long block1;
10331 char *body;
10332 struct chunk *temp;
10333
10334 CHECK_HTTP_MESSAGE_FIRST();
10335
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010336 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010337 msg = &txn->req;
10338 else
10339 msg = &txn->rsp;
10340
10341 len = http_body_bytes(msg);
10342 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10343
10344 block1 = len;
10345 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10346 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10347
10348 if (block1 == len) {
10349 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010350 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010351 smp->data.u.str.str = body;
10352 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010353 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10354 }
10355 else {
10356 /* buffer is wrapped, we need to defragment it */
10357 temp = get_trash_chunk();
10358 memcpy(temp->str, body, block1);
10359 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010360 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010361 smp->data.u.str.str = temp->str;
10362 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010363 smp->flags = SMP_F_VOL_TEST;
10364 }
10365 return 1;
10366}
10367
10368
10369/* returns the available length of the body. This requires that the body
10370 * has been waited for using http-buffer-request.
10371 */
10372static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010373smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010374{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010375 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010376 struct http_msg *msg;
10377
10378 CHECK_HTTP_MESSAGE_FIRST();
10379
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010380 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010381 msg = &txn->req;
10382 else
10383 msg = &txn->rsp;
10384
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010385 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010386 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010387
10388 smp->flags = SMP_F_VOL_TEST;
10389 return 1;
10390}
10391
10392
10393/* returns the advertised length of the body, or the advertised size of the
10394 * chunks available in the buffer. This requires that the body has been waited
10395 * for using http-buffer-request.
10396 */
10397static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010398smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010399{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010400 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010401 struct http_msg *msg;
10402
10403 CHECK_HTTP_MESSAGE_FIRST();
10404
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010405 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010406 msg = &txn->req;
10407 else
10408 msg = &txn->rsp;
10409
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010410 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010411 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010412
10413 smp->flags = SMP_F_VOL_TEST;
10414 return 1;
10415}
10416
10417
Willy Tarreau8797c062007-05-07 00:55:35 +020010418/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010419static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010420smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010421{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010422 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010423
Willy Tarreauc0239e02012-04-16 14:42:55 +020010424 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010425
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010426 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010427 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010428 smp->data.u.str.len = txn->req.sl.rq.u_l;
10429 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010430 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010431 return 1;
10432}
10433
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010434static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010435smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010436{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010437 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010438 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010439
Willy Tarreauc0239e02012-04-16 14:42:55 +020010440 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010441
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010442 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010443 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 +020010444 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010445 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010446
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010447 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010448 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010449 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010450 return 1;
10451}
10452
10453static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010454smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010455{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010456 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010457 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010458
Willy Tarreauc0239e02012-04-16 14:42:55 +020010459 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010460
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010461 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010462 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 +020010463 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10464 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010465
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010466 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010467 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010468 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010469 return 1;
10470}
10471
Willy Tarreau185b5c42012-04-26 15:11:51 +020010472/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10473 * Accepts an optional argument of type string containing the header field name,
10474 * and an optional argument of type signed or unsigned integer to request an
10475 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010476 * headers are considered from the first one. It does not stop on commas and
10477 * returns full lines instead (useful for User-Agent or Date for example).
10478 */
10479static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010480smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010481{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010482 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010483 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010484 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010485 int occ = 0;
10486 const char *name_str = NULL;
10487 int name_len = 0;
10488
10489 if (!ctx) {
10490 /* first call */
10491 ctx = &static_hdr_ctx;
10492 ctx->idx = 0;
10493 smp->ctx.a[0] = ctx;
10494 }
10495
10496 if (args) {
10497 if (args[0].type != ARGT_STR)
10498 return 0;
10499 name_str = args[0].data.str.str;
10500 name_len = args[0].data.str.len;
10501
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010502 if (args[1].type == ARGT_SINT)
10503 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010504 }
10505
10506 CHECK_HTTP_MESSAGE_FIRST();
10507
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010508 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010509 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 +020010510
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010511 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10512 /* search for header from the beginning */
10513 ctx->idx = 0;
10514
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010515 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010516 /* no explicit occurrence and single fetch => last header by default */
10517 occ = -1;
10518
10519 if (!occ)
10520 /* prepare to report multiple occurrences for ACL fetches */
10521 smp->flags |= SMP_F_NOT_LAST;
10522
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010523 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010524 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010525 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 +020010526 return 1;
10527
10528 smp->flags &= ~SMP_F_NOT_LAST;
10529 return 0;
10530}
10531
10532/* 6. Check on HTTP header count. The number of occurrences is returned.
10533 * Accepts exactly 1 argument of type string. It does not stop on commas and
10534 * returns full lines instead (useful for User-Agent or Date for example).
10535 */
10536static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010537smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010538{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010539 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010540 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010541 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010542 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010543 const char *name = NULL;
10544 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010545
Willy Tarreau601a4d12015-04-01 19:16:09 +020010546 if (args && args->type == ARGT_STR) {
10547 name = args->data.str.str;
10548 len = args->data.str.len;
10549 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010550
10551 CHECK_HTTP_MESSAGE_FIRST();
10552
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010553 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010554 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010555
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010556 ctx.idx = 0;
10557 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010558 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010559 cnt++;
10560
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010561 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010562 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010563 smp->flags = SMP_F_VOL_HDR;
10564 return 1;
10565}
10566
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010567static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010568smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010569{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010570 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010571 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010572 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010573 struct chunk *temp;
10574 char del = ',';
10575
10576 if (args && args->type == ARGT_STR)
10577 del = *args[0].data.str.str;
10578
10579 CHECK_HTTP_MESSAGE_FIRST();
10580
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010581 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010582 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 +020010583
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010584 temp = get_trash_chunk();
10585
10586 ctx.idx = 0;
10587 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10588 if (temp->len)
10589 temp->str[temp->len++] = del;
10590 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10591 temp->len += ctx.del;
10592 }
10593
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010594 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010595 smp->data.u.str.str = temp->str;
10596 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010597 smp->flags = SMP_F_VOL_HDR;
10598 return 1;
10599}
10600
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010601/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10602 * Accepts an optional argument of type string containing the header field name,
10603 * and an optional argument of type signed or unsigned integer to request an
10604 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010605 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010606 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010607static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010608smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010609{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010610 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010611 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010612 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010613 int occ = 0;
10614 const char *name_str = NULL;
10615 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010616
Willy Tarreaua890d072013-04-02 12:01:06 +020010617 if (!ctx) {
10618 /* first call */
10619 ctx = &static_hdr_ctx;
10620 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010621 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010622 }
10623
Willy Tarreau185b5c42012-04-26 15:11:51 +020010624 if (args) {
10625 if (args[0].type != ARGT_STR)
10626 return 0;
10627 name_str = args[0].data.str.str;
10628 name_len = args[0].data.str.len;
10629
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010630 if (args[1].type == ARGT_SINT)
10631 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010632 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010633
Willy Tarreaue333ec92012-04-16 16:26:40 +020010634 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010635
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010636 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010637 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 +020010638
Willy Tarreau185b5c42012-04-26 15:11:51 +020010639 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010640 /* search for header from the beginning */
10641 ctx->idx = 0;
10642
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010643 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010644 /* no explicit occurrence and single fetch => last header by default */
10645 occ = -1;
10646
10647 if (!occ)
10648 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010649 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010650
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010651 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010652 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010653 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 +020010654 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010655
Willy Tarreau37406352012-04-23 16:16:37 +020010656 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010657 return 0;
10658}
10659
Willy Tarreauc11416f2007-06-17 16:58:38 +020010660/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010661 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010662 */
10663static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010664smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010665{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010666 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010667 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010668 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010669 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010670 const char *name = NULL;
10671 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010672
Willy Tarreau601a4d12015-04-01 19:16:09 +020010673 if (args && args->type == ARGT_STR) {
10674 name = args->data.str.str;
10675 len = args->data.str.len;
10676 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010677
Willy Tarreaue333ec92012-04-16 16:26:40 +020010678 CHECK_HTTP_MESSAGE_FIRST();
10679
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010680 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010681 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 +020010682
Willy Tarreau33a7e692007-06-10 19:45:56 +020010683 ctx.idx = 0;
10684 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010685 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010686 cnt++;
10687
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010688 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010689 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010690 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010691 return 1;
10692}
10693
Willy Tarreau185b5c42012-04-26 15:11:51 +020010694/* Fetch an HTTP header's integer value. The integer value is returned. It
10695 * takes a mandatory argument of type string and an optional one of type int
10696 * to designate a specific occurrence. It returns an unsigned integer, which
10697 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010698 */
10699static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010700smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010701{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010702 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010703
Willy Tarreauf853c462012-04-23 18:53:56 +020010704 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010705 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010706 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010707 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010708
Willy Tarreaud53e2422012-04-16 17:21:11 +020010709 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010710}
10711
Cyril Bonté69fa9922012-10-25 00:01:06 +020010712/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10713 * and an optional one of type int to designate a specific occurrence.
10714 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010715 */
10716static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010717smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010718{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010719 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010720
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010721 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010722 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010723 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010724 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010725 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010726 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010727 if (smp->data.u.str.len < temp->size - 1) {
10728 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10729 temp->str[smp->data.u.str.len] = '\0';
10730 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010731 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010732 break;
10733 }
10734 }
10735 }
10736
Willy Tarreaud53e2422012-04-16 17:21:11 +020010737 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010738 if (!(smp->flags & SMP_F_NOT_LAST))
10739 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010740 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010741 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010742}
10743
Willy Tarreau737b0c12007-06-10 21:28:46 +020010744/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10745 * the first '/' after the possible hostname, and ends before the possible '?'.
10746 */
10747static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010748smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010749{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010750 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010751 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010752
Willy Tarreauc0239e02012-04-16 14:42:55 +020010753 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010754
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010755 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010756 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010757 ptr = http_get_path(txn);
10758 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010759 return 0;
10760
10761 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010762 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010763 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010764
10765 while (ptr < end && *ptr != '?')
10766 ptr++;
10767
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010768 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010769 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010770 return 1;
10771}
10772
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010773/* This produces a concatenation of the first occurrence of the Host header
10774 * followed by the path component if it begins with a slash ('/'). This means
10775 * that '*' will not be added, resulting in exactly the first Host entry.
10776 * If no Host header is found, then the path is returned as-is. The returned
10777 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010778 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010779 */
10780static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010781smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010782{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010783 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010784 char *ptr, *end, *beg;
10785 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010786 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010787
10788 CHECK_HTTP_MESSAGE_FIRST();
10789
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010790 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010791 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010792 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010793 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010794
10795 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010796 temp = get_trash_chunk();
10797 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010798 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010799 smp->data.u.str.str = temp->str;
10800 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010801
10802 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010803 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010804 beg = http_get_path(txn);
10805 if (!beg)
10806 beg = end;
10807
10808 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10809
10810 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010811 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10812 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010813 }
10814
10815 smp->flags = SMP_F_VOL_1ST;
10816 return 1;
10817}
10818
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010819/* This produces a 32-bit hash of the concatenation of the first occurrence of
10820 * the Host header followed by the path component if it begins with a slash ('/').
10821 * This means that '*' will not be added, resulting in exactly the first Host
10822 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010823 * is hashed using the path hash followed by a full avalanche hash and provides a
10824 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010825 * high-traffic sites without having to store whole paths.
10826 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010827int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010828smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010829{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010830 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010831 struct hdr_ctx ctx;
10832 unsigned int hash = 0;
10833 char *ptr, *beg, *end;
10834 int len;
10835
10836 CHECK_HTTP_MESSAGE_FIRST();
10837
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010838 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010839 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010840 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010841 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10842 ptr = ctx.line + ctx.val;
10843 len = ctx.vlen;
10844 while (len--)
10845 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10846 }
10847
10848 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010849 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010850 beg = http_get_path(txn);
10851 if (!beg)
10852 beg = end;
10853
10854 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10855
10856 if (beg < ptr && *beg == '/') {
10857 while (beg < ptr)
10858 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10859 }
10860 hash = full_hash(hash);
10861
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010862 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010863 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010864 smp->flags = SMP_F_VOL_1ST;
10865 return 1;
10866}
10867
Willy Tarreau4a550602012-12-09 14:53:32 +010010868/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010869 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10870 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10871 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010872 * that in environments where IPv6 is insignificant, truncating the output to
10873 * 8 bytes would still work.
10874 */
10875static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010876smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010877{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010878 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010879 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010880
10881 if (!cli_conn)
10882 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010883
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010884 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010885 return 0;
10886
Willy Tarreau47ca5452012-12-23 20:22:19 +010010887 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010888 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010889 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010890
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010891 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010892 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010893 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010894 temp->len += 4;
10895 break;
10896 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010897 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010898 temp->len += 16;
10899 break;
10900 default:
10901 return 0;
10902 }
10903
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010904 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010905 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010906 return 1;
10907}
10908
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010909/* Extracts the query string, which comes after the question mark '?'. If no
10910 * question mark is found, nothing is returned. Otherwise it returns a sample
10911 * of type string carrying the whole query string.
10912 */
10913static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010914smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010915{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010916 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010917 char *ptr, *end;
10918
10919 CHECK_HTTP_MESSAGE_FIRST();
10920
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010921 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010922 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10923 end = ptr + txn->req.sl.rq.u_l;
10924
10925 /* look up the '?' */
10926 do {
10927 if (ptr == end)
10928 return 0;
10929 } while (*ptr++ != '?');
10930
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010931 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010932 smp->data.u.str.str = ptr;
10933 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010934 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10935 return 1;
10936}
10937
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010938static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010939smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010940{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010941 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10942 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10943 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010944
Willy Tarreau24e32d82012-04-23 23:55:44 +020010945 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +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 = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010949 return 1;
10950}
10951
Willy Tarreau7f18e522010-10-22 20:04:13 +020010952/* return a valid test if the current request is the first one on the connection */
10953static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010954smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010955{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010956 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010957 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010958 return 1;
10959}
10960
Willy Tarreau34db1082012-04-19 17:16:54 +020010961/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010962static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010963smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010964{
10965
Willy Tarreau24e32d82012-04-23 23:55:44 +020010966 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010967 return 0;
10968
Willy Tarreauc0239e02012-04-16 14:42:55 +020010969 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010970
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010971 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010972 return 0;
10973
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010974 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010975 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010976 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010977 return 1;
10978}
Willy Tarreau8797c062007-05-07 00:55:35 +020010979
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010980/* Accepts exactly 1 argument of type userlist */
10981static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010982smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010983{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010984 if (!args || args->type != ARGT_USR)
10985 return 0;
10986
10987 CHECK_HTTP_MESSAGE_FIRST();
10988
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010989 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010990 return 0;
10991
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010992 /* if the user does not belong to the userlist or has a wrong password,
10993 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010994 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010995 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010996 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10997 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010998 return 0;
10999
11000 /* pat_match_auth() will need the user list */
11001 smp->ctx.a[0] = args->data.usr;
11002
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011003 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011004 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011005 smp->data.u.str.str = smp->strm->txn->auth.user;
11006 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011007
11008 return 1;
11009}
11010
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011011/* Try to find the next occurrence of a cookie name in a cookie header value.
11012 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11013 * the cookie value is returned into *value and *value_l, and the function
11014 * returns a pointer to the next pointer to search from if the value was found.
11015 * Otherwise if the cookie was not found, NULL is returned and neither value
11016 * nor value_l are touched. The input <hdr> string should first point to the
11017 * header's value, and the <hdr_end> pointer must point to the first character
11018 * not part of the value. <list> must be non-zero if value may represent a list
11019 * of values (cookie headers). This makes it faster to abort parsing when no
11020 * list is expected.
11021 */
David Carlier4686f792015-09-25 14:10:50 +010011022char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011023extract_cookie_value(char *hdr, const char *hdr_end,
11024 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011025 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011026{
11027 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11028 char *next;
11029
11030 /* we search at least a cookie name followed by an equal, and more
11031 * generally something like this :
11032 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11033 */
11034 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11035 /* Iterate through all cookies on this line */
11036
11037 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
11038 att_beg++;
11039
11040 /* find att_end : this is the first character after the last non
11041 * space before the equal. It may be equal to hdr_end.
11042 */
11043 equal = att_end = att_beg;
11044
11045 while (equal < hdr_end) {
11046 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11047 break;
11048 if (http_is_spht[(unsigned char)*equal++])
11049 continue;
11050 att_end = equal;
11051 }
11052
11053 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11054 * is between <att_beg> and <equal>, both may be identical.
11055 */
11056
11057 /* look for end of cookie if there is an equal sign */
11058 if (equal < hdr_end && *equal == '=') {
11059 /* look for the beginning of the value */
11060 val_beg = equal + 1;
11061 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
11062 val_beg++;
11063
11064 /* find the end of the value, respecting quotes */
11065 next = find_cookie_value_end(val_beg, hdr_end);
11066
11067 /* make val_end point to the first white space or delimitor after the value */
11068 val_end = next;
11069 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
11070 val_end--;
11071 } else {
11072 val_beg = val_end = next = equal;
11073 }
11074
11075 /* We have nothing to do with attributes beginning with '$'. However,
11076 * they will automatically be removed if a header before them is removed,
11077 * since they're supposed to be linked together.
11078 */
11079 if (*att_beg == '$')
11080 continue;
11081
11082 /* Ignore cookies with no equal sign */
11083 if (equal == next)
11084 continue;
11085
11086 /* Now we have the cookie name between att_beg and att_end, and
11087 * its value between val_beg and val_end.
11088 */
11089
11090 if (att_end - att_beg == cookie_name_l &&
11091 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11092 /* let's return this value and indicate where to go on from */
11093 *value = val_beg;
11094 *value_l = val_end - val_beg;
11095 return next + 1;
11096 }
11097
11098 /* Set-Cookie headers only have the name in the first attr=value part */
11099 if (!list)
11100 break;
11101 }
11102
11103 return NULL;
11104}
11105
William Lallemanda43ba4e2014-01-28 18:14:25 +010011106/* Fetch a captured HTTP request header. The index is the position of
11107 * the "capture" option in the configuration file
11108 */
11109static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011110smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011111{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011112 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011113 int idx;
11114
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011115 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011116 return 0;
11117
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011118 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011119
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011120 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 +010011121 return 0;
11122
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011123 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011124 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011125 smp->data.u.str.str = smp->strm->req_cap[idx];
11126 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011127
11128 return 1;
11129}
11130
11131/* Fetch a captured HTTP response header. The index is the position of
11132 * the "capture" option in the configuration file
11133 */
11134static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011135smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011136{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011137 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011138 int idx;
11139
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011140 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011141 return 0;
11142
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011143 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011144
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011145 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 +010011146 return 0;
11147
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011148 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011149 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011150 smp->data.u.str.str = smp->strm->res_cap[idx];
11151 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011152
11153 return 1;
11154}
11155
William Lallemand65ad6e12014-01-31 15:08:02 +010011156/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11157static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011158smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011159{
11160 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011161 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011162 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011163
Willy Tarreau15e91e12015-04-04 00:52:09 +020011164 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011165 return 0;
11166
William Lallemand96a77852014-02-05 00:30:02 +010011167 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011168
William Lallemand96a77852014-02-05 00:30:02 +010011169 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11170 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011171
William Lallemand96a77852014-02-05 00:30:02 +010011172 temp = get_trash_chunk();
11173 temp->str = txn->uri;
11174 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011175 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011176 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011177 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011178
11179 return 1;
11180
11181}
11182
11183/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11184static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011185smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011186{
11187 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011188 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011189 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011190
Willy Tarreau15e91e12015-04-04 00:52:09 +020011191 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011192 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011193
William Lallemand65ad6e12014-01-31 15:08:02 +010011194 ptr = txn->uri;
11195
11196 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11197 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011198
William Lallemand65ad6e12014-01-31 15:08:02 +010011199 if (!*ptr)
11200 return 0;
11201
11202 ptr++; /* skip the space */
11203
11204 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011205 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011206 if (!ptr)
11207 return 0;
11208 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11209 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011210
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011211 smp->data.u.str = *temp;
11212 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011213 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011214 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011215
11216 return 1;
11217}
11218
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011219/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11220 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11221 */
11222static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011223smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011224{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011225 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011226
Willy Tarreau15e91e12015-04-04 00:52:09 +020011227 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011228 return 0;
11229
11230 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011231 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011232 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011233 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011234
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011235 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011236 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011237 smp->flags = SMP_F_CONST;
11238 return 1;
11239
11240}
11241
11242/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11243 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11244 */
11245static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011246smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011247{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011248 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011249
Willy Tarreau15e91e12015-04-04 00:52:09 +020011250 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011251 return 0;
11252
11253 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011254 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011255 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011256 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011257
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011258 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011259 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011260 smp->flags = SMP_F_CONST;
11261 return 1;
11262
11263}
11264
William Lallemand65ad6e12014-01-31 15:08:02 +010011265
Willy Tarreaue333ec92012-04-16 16:26:40 +020011266/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011267 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011268 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011269 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011270 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011271 * Accepts exactly 1 argument of type string. If the input options indicate
11272 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011273 * The returned sample is of type CSTR. Can be used to parse cookies in other
11274 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011275 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011276int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011277{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011278 struct http_txn *txn;
11279 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011280 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011281 const struct http_msg *msg;
11282 const char *hdr_name;
11283 int hdr_name_len;
11284 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011285 int occ = 0;
11286 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011287
Willy Tarreau24e32d82012-04-23 23:55:44 +020011288 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011289 return 0;
11290
Willy Tarreaua890d072013-04-02 12:01:06 +020011291 if (!ctx) {
11292 /* first call */
11293 ctx = &static_hdr_ctx;
11294 ctx->idx = 0;
11295 smp->ctx.a[2] = ctx;
11296 }
11297
Willy Tarreaue333ec92012-04-16 16:26:40 +020011298 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011299
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011300 txn = smp->strm->txn;
11301 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011302
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011303 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011304 msg = &txn->req;
11305 hdr_name = "Cookie";
11306 hdr_name_len = 6;
11307 } else {
11308 msg = &txn->rsp;
11309 hdr_name = "Set-Cookie";
11310 hdr_name_len = 10;
11311 }
11312
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011313 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011314 /* no explicit occurrence and single fetch => last cookie by default */
11315 occ = -1;
11316
11317 /* OK so basically here, either we want only one value and it's the
11318 * last one, or we want to iterate over all of them and we fetch the
11319 * next one.
11320 */
11321
Willy Tarreau9b28e032012-10-12 23:49:43 +020011322 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011323 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011324 /* search for the header from the beginning, we must first initialize
11325 * the search parameters.
11326 */
Willy Tarreau37406352012-04-23 16:16:37 +020011327 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011328 ctx->idx = 0;
11329 }
11330
Willy Tarreau28376d62012-04-26 21:26:10 +020011331 smp->flags |= SMP_F_VOL_HDR;
11332
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011333 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011334 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11335 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011336 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11337 goto out;
11338
Willy Tarreau24e32d82012-04-23 23:55:44 +020011339 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011340 continue;
11341
Willy Tarreau37406352012-04-23 16:16:37 +020011342 smp->ctx.a[0] = ctx->line + ctx->val;
11343 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011344 }
11345
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011346 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011347 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011348 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011349 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011350 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011351 &smp->data.u.str.str,
11352 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011353 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011354 found = 1;
11355 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011356 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011357 smp->flags |= SMP_F_NOT_LAST;
11358 return 1;
11359 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011360 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011361 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011362 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011363 /* all cookie headers and values were scanned. If we're looking for the
11364 * last occurrence, we may return it now.
11365 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011366 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011367 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011368 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011369}
11370
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011371/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011372 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011373 * multiple cookies may be parsed on the same line. The returned sample is of
11374 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011375 */
11376static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011377smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011378{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011379 struct http_txn *txn;
11380 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011381 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011382 const struct http_msg *msg;
11383 const char *hdr_name;
11384 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011385 int cnt;
11386 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011387 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011388
Willy Tarreau24e32d82012-04-23 23:55:44 +020011389 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011390 return 0;
11391
Willy Tarreaue333ec92012-04-16 16:26:40 +020011392 CHECK_HTTP_MESSAGE_FIRST();
11393
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011394 txn = smp->strm->txn;
11395 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011396
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011397 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011398 msg = &txn->req;
11399 hdr_name = "Cookie";
11400 hdr_name_len = 6;
11401 } else {
11402 msg = &txn->rsp;
11403 hdr_name = "Set-Cookie";
11404 hdr_name_len = 10;
11405 }
11406
Willy Tarreau9b28e032012-10-12 23:49:43 +020011407 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011408 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011409 ctx.idx = 0;
11410 cnt = 0;
11411
11412 while (1) {
11413 /* Note: val_beg == NULL every time we need to fetch a new header */
11414 if (!val_beg) {
11415 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11416 break;
11417
Willy Tarreau24e32d82012-04-23 23:55:44 +020011418 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011419 continue;
11420
11421 val_beg = ctx.line + ctx.val;
11422 val_end = val_beg + ctx.vlen;
11423 }
11424
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011425 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011426 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011427 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011428 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011429 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011430 &smp->data.u.str.str,
11431 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011432 cnt++;
11433 }
11434 }
11435
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011436 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011437 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011438 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011439 return 1;
11440}
11441
Willy Tarreau51539362012-05-08 12:46:28 +020011442/* Fetch an cookie's integer value. The integer value is returned. It
11443 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11444 */
11445static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011446smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011447{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011448 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011449
11450 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011451 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011452 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011453 }
11454
11455 return ret;
11456}
11457
Willy Tarreau8797c062007-05-07 00:55:35 +020011458/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011459/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011460/************************************************************************/
11461
David Cournapeau16023ee2010-12-23 20:55:41 +090011462/*
11463 * Given a path string and its length, find the position of beginning of the
11464 * query string. Returns NULL if no query string is found in the path.
11465 *
11466 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11467 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011468 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011469 */
bedis4c75cca2012-10-05 08:38:24 +020011470static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011471{
11472 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011473
bedis4c75cca2012-10-05 08:38:24 +020011474 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011475 return p ? p + 1 : NULL;
11476}
11477
bedis4c75cca2012-10-05 08:38:24 +020011478static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011479{
bedis4c75cca2012-10-05 08:38:24 +020011480 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011481}
11482
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011483/* after increasing a pointer value, it can exceed the first buffer
11484 * size. This function transform the value of <ptr> according with
11485 * the expected position. <chunks> is an array of the one or two
11486 * avalaible chunks. The first value is the start of the first chunk,
11487 * the second value if the end+1 of the first chunks. The third value
11488 * is NULL or the start of the second chunk and the fourth value is
11489 * the end+1 of the second chunk. The function returns 1 if does a
11490 * wrap, else returns 0.
11491 */
11492static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11493{
11494 if (*ptr < chunks[1])
11495 return 0;
11496 if (!chunks[2])
11497 return 0;
11498 *ptr = chunks[2] + ( *ptr - chunks[1] );
11499 return 1;
11500}
11501
David Cournapeau16023ee2010-12-23 20:55:41 +090011502/*
11503 * Given a url parameter, find the starting position of the first occurence,
11504 * or NULL if the parameter is not found.
11505 *
11506 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11507 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011508 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011509 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011510 * or the second chunk. The caller must be check the position before using the
11511 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011512 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011513static const char *
11514find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011515 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011516 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011517{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011518 const char *pos, *last, *equal;
11519 const char **bufs = chunks;
11520 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011521
David Cournapeau16023ee2010-12-23 20:55:41 +090011522
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011523 pos = bufs[0];
11524 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011525 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011526 /* Check the equal. */
11527 equal = pos + url_param_name_l;
11528 if (fix_pointer_if_wrap(chunks, &equal)) {
11529 if (equal >= chunks[3])
11530 return NULL;
11531 } else {
11532 if (equal >= chunks[1])
11533 return NULL;
11534 }
11535 if (*equal == '=') {
11536 if (pos + url_param_name_l > last) {
11537 /* process wrap case, we detect a wrap. In this case, the
11538 * comparison is performed in two parts.
11539 */
11540
11541 /* This is the end, we dont have any other chunk. */
11542 if (bufs != chunks || !bufs[2])
11543 return NULL;
11544
11545 /* Compute the length of each part of the comparison. */
11546 l1 = last - pos;
11547 l2 = url_param_name_l - l1;
11548
11549 /* The second buffer is too short to contain the compared string. */
11550 if (bufs[2] + l2 > bufs[3])
11551 return NULL;
11552
11553 if (memcmp(pos, url_param_name, l1) == 0 &&
11554 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11555 return pos;
11556
11557 /* Perform wrapping and jump the string who fail the comparison. */
11558 bufs += 2;
11559 pos = bufs[0] + l2;
11560 last = bufs[1];
11561
11562 } else {
11563 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011564 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11565 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011566 pos += url_param_name_l + 1;
11567 if (fix_pointer_if_wrap(chunks, &pos))
11568 last = bufs[2];
11569 }
11570 }
11571
11572 while (1) {
11573 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011574 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011575 pos++;
11576 if (pos < last)
11577 break;
11578 /* process buffer wrapping. */
11579 if (bufs != chunks || !bufs[2])
11580 return NULL;
11581 bufs += 2;
11582 pos = bufs[0];
11583 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011584 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011585 pos++;
11586 }
11587 return NULL;
11588}
11589
11590/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011591 * Given a url parameter name and a query string, find the next value.
11592 * An empty url_param_name matches the first available parameter.
11593 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11594 * respectively provide a pointer to the value and its end.
11595 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011596 */
11597static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011598find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011599 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011600 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011601{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011602 const char *arg_start, *qs_end;
11603 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011604
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011605 arg_start = chunks[0];
11606 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011607 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011608 /* Looks for an argument name. */
11609 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011610 url_param_name, url_param_name_l,
11611 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011612 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011613 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011614 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011615 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011616 if (!arg_start)
11617 return 0;
11618
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011619 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011620 while (1) {
11621 /* looks for the first argument. */
11622 value_start = memchr(arg_start, '=', qs_end - arg_start);
11623 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011624 /* Check for wrapping. */
11625 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011626 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011627 chunks[2]) {
11628 arg_start = chunks[2];
11629 qs_end = chunks[3];
11630 continue;
11631 }
11632 return 0;
11633 }
11634 break;
11635 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011636 value_start++;
11637 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011638 else {
11639 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011640 value_start = arg_start + url_param_name_l + 1;
11641
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011642 /* Check for pointer wrapping. */
11643 if (fix_pointer_if_wrap(chunks, &value_start)) {
11644 /* Update the end pointer. */
11645 qs_end = chunks[3];
11646
11647 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011648 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011649 return 0;
11650 }
11651 }
11652
David Cournapeau16023ee2010-12-23 20:55:41 +090011653 value_end = value_start;
11654
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011655 while (1) {
11656 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11657 value_end++;
11658 if (value_end < qs_end)
11659 break;
11660 /* process buffer wrapping. */
11661 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011662 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011663 chunks[2]) {
11664 value_end = chunks[2];
11665 qs_end = chunks[3];
11666 continue;
11667 }
11668 break;
11669 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011670
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011671 *vstart = value_start;
11672 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011673 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011674}
11675
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011676/* This scans a URL-encoded query string. It takes an optionally wrapping
11677 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11678 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11679 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011680 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011681static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011682smp_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 +090011683{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011684 const char *vstart, *vend;
11685 struct chunk *temp;
11686 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011687
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011688 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011689 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011690 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011691 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011692 return 0;
11693
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011694 /* Create sample. If the value is contiguous, return the pointer as CONST,
11695 * if the value is wrapped, copy-it in a buffer.
11696 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011697 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011698 if (chunks[2] &&
11699 vstart >= chunks[0] && vstart <= chunks[1] &&
11700 vend >= chunks[2] && vend <= chunks[3]) {
11701 /* Wrapped case. */
11702 temp = get_trash_chunk();
11703 memcpy(temp->str, vstart, chunks[1] - vstart);
11704 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011705 smp->data.u.str.str = temp->str;
11706 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011707 } else {
11708 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011709 smp->data.u.str.str = (char *)vstart;
11710 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011711 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11712 }
11713
11714 /* Update context, check wrapping. */
11715 chunks[0] = vend;
11716 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11717 chunks[1] = chunks[3];
11718 chunks[2] = NULL;
11719 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011720
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011721 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011722 smp->flags |= SMP_F_NOT_LAST;
11723
David Cournapeau16023ee2010-12-23 20:55:41 +090011724 return 1;
11725}
11726
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011727/* This function iterates over each parameter of the query string. It uses
11728 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011729 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11730 * An optional parameter name is passed in args[0], otherwise any parameter is
11731 * considered. It supports an optional delimiter argument for the beginning of
11732 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011733 */
11734static int
11735smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11736{
11737 struct http_msg *msg;
11738 char delim = '?';
11739 const char *name;
11740 int name_len;
11741
Dragan Dosen26f77e52015-05-25 10:02:11 +020011742 if (!args ||
11743 (args[0].type && args[0].type != ARGT_STR) ||
11744 (args[1].type && args[1].type != ARGT_STR))
11745 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011746
Dragan Dosen26f77e52015-05-25 10:02:11 +020011747 name = "";
11748 name_len = 0;
11749 if (args->type == ARGT_STR) {
11750 name = args->data.str.str;
11751 name_len = args->data.str.len;
11752 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011753
Dragan Dosen26f77e52015-05-25 10:02:11 +020011754 if (args[1].type)
11755 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011756
Dragan Dosen26f77e52015-05-25 10:02:11 +020011757 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011758 CHECK_HTTP_MESSAGE_FIRST();
11759
11760 msg = &smp->strm->txn->req;
11761
11762 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11763 msg->sl.rq.u_l, delim);
11764 if (!smp->ctx.a[0])
11765 return 0;
11766
11767 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011768
11769 /* Assume that the context is filled with NULL pointer
11770 * before the first call.
11771 * smp->ctx.a[2] = NULL;
11772 * smp->ctx.a[3] = NULL;
11773 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011774 }
11775
11776 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11777}
11778
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011779/* This function iterates over each parameter of the body. This requires
11780 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011781 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11782 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11783 * optional second part if the body wraps at the end of the buffer. An optional
11784 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011785 */
11786static int
11787smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11788{
11789 struct http_txn *txn = smp->strm->txn;
11790 struct http_msg *msg;
11791 unsigned long len;
11792 unsigned long block1;
11793 char *body;
11794 const char *name;
11795 int name_len;
11796
11797 if (!args || (args[0].type && args[0].type != ARGT_STR))
11798 return 0;
11799
11800 name = "";
11801 name_len = 0;
11802 if (args[0].type == ARGT_STR) {
11803 name = args[0].data.str.str;
11804 name_len = args[0].data.str.len;
11805 }
11806
11807 if (!smp->ctx.a[0]) { // first call, find the query string
11808 CHECK_HTTP_MESSAGE_FIRST();
11809
11810 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
11811 msg = &txn->req;
11812 else
11813 msg = &txn->rsp;
11814
11815 len = http_body_bytes(msg);
11816 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11817
11818 block1 = len;
11819 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11820 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11821
11822 if (block1 == len) {
11823 /* buffer is not wrapped (or empty) */
11824 smp->ctx.a[0] = body;
11825 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011826
11827 /* Assume that the context is filled with NULL pointer
11828 * before the first call.
11829 * smp->ctx.a[2] = NULL;
11830 * smp->ctx.a[3] = NULL;
11831 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011832 }
11833 else {
11834 /* buffer is wrapped, we need to defragment it */
11835 smp->ctx.a[0] = body;
11836 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011837 smp->ctx.a[2] = msg->chn->buf->data;
11838 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011839 }
11840 }
11841 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11842}
11843
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011844/* Return the signed integer value for the specified url parameter (see url_param
11845 * above).
11846 */
11847static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011848smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011849{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011850 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011851
11852 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011853 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011854 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011855 }
11856
11857 return ret;
11858}
11859
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011860/* This produces a 32-bit hash of the concatenation of the first occurrence of
11861 * the Host header followed by the path component if it begins with a slash ('/').
11862 * This means that '*' will not be added, resulting in exactly the first Host
11863 * entry. If no Host header is found, then the path is used. The resulting value
11864 * is hashed using the url hash followed by a full avalanche hash and provides a
11865 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11866 * high-traffic sites without having to store whole paths.
11867 * this differs from the base32 functions in that it includes the url parameters
11868 * as well as the path
11869 */
11870static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011871smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011872{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011873 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011874 struct hdr_ctx ctx;
11875 unsigned int hash = 0;
11876 char *ptr, *beg, *end;
11877 int len;
11878
11879 CHECK_HTTP_MESSAGE_FIRST();
11880
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011881 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011882 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011883 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011884 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11885 ptr = ctx.line + ctx.val;
11886 len = ctx.vlen;
11887 while (len--)
11888 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11889 }
11890
11891 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011892 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 +000011893 beg = http_get_path(txn);
11894 if (!beg)
11895 beg = end;
11896
11897 for (ptr = beg; ptr < end ; ptr++);
11898
11899 if (beg < ptr && *beg == '/') {
11900 while (beg < ptr)
11901 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11902 }
11903 hash = full_hash(hash);
11904
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011905 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011906 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011907 smp->flags = SMP_F_VOL_1ST;
11908 return 1;
11909}
11910
11911/* This concatenates the source address with the 32-bit hash of the Host and
11912 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11913 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11914 * on the source address length. The URL hash is stored before the address so
11915 * that in environments where IPv6 is insignificant, truncating the output to
11916 * 8 bytes would still work.
11917 */
11918static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011919smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011920{
11921 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011922 struct connection *cli_conn = objt_conn(smp->sess->origin);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011923 unsigned int hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011924
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011925 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011926 return 0;
11927
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011928 /* The returned hash is a 32 bytes integer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011929 hash = smp->data.u.sint;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011930
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011931 temp = get_trash_chunk();
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011932 memcpy(temp->str + temp->len, &hash, sizeof(hash));
11933 temp->len += sizeof(hash);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011934
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011935 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011936 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011937 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011938 temp->len += 4;
11939 break;
11940 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011941 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011942 temp->len += 16;
11943 break;
11944 default:
11945 return 0;
11946 }
11947
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011948 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011949 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011950 return 1;
11951}
11952
Willy Tarreau185b5c42012-04-26 15:11:51 +020011953/* This function is used to validate the arguments passed to any "hdr" fetch
11954 * keyword. These keywords support an optional positive or negative occurrence
11955 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11956 * is assumed that the types are already the correct ones. Returns 0 on error,
11957 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11958 * error message in case of error, that the caller is responsible for freeing.
11959 * The initial location must either be freeable or NULL.
11960 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011961int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011962{
11963 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011964 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011965 return 0;
11966 }
11967 return 1;
11968}
11969
Willy Tarreau276fae92013-07-25 14:36:01 +020011970/* takes an UINT value on input supposed to represent the time since EPOCH,
11971 * adds an optional offset found in args[0] and emits a string representing
11972 * the date in RFC-1123/5322 format.
11973 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011974static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011975{
11976 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11977 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11978 struct chunk *temp;
11979 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011980 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011981 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011982
11983 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011984 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011985 curr_date += args[0].data.sint;
11986
11987 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011988 if (!tm)
11989 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011990
11991 temp = get_trash_chunk();
11992 temp->len = snprintf(temp->str, temp->size - temp->len,
11993 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11994 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11995 tm->tm_hour, tm->tm_min, tm->tm_sec);
11996
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011997 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011998 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011999 return 1;
12000}
12001
Thierry FOURNIERad903512014-04-11 17:51:01 +020012002/* Match language range with language tag. RFC2616 14.4:
12003 *
12004 * A language-range matches a language-tag if it exactly equals
12005 * the tag, or if it exactly equals a prefix of the tag such
12006 * that the first tag character following the prefix is "-".
12007 *
12008 * Return 1 if the strings match, else return 0.
12009 */
12010static inline int language_range_match(const char *range, int range_len,
12011 const char *tag, int tag_len)
12012{
12013 const char *end = range + range_len;
12014 const char *tend = tag + tag_len;
12015 while (range < end) {
12016 if (*range == '-' && tag == tend)
12017 return 1;
12018 if (*range != *tag || tag == tend)
12019 return 0;
12020 range++;
12021 tag++;
12022 }
12023 /* Return true only if the last char of the tag is matched. */
12024 return tag == tend;
12025}
12026
12027/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012028static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012029{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012030 const char *al = smp->data.u.str.str;
12031 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012032 const char *token;
12033 int toklen;
12034 int qvalue;
12035 const char *str;
12036 const char *w;
12037 int best_q = 0;
12038
12039 /* Set the constant to the sample, because the output of the
12040 * function will be peek in the constant configuration string.
12041 */
12042 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012043 smp->data.u.str.size = 0;
12044 smp->data.u.str.str = "";
12045 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012046
12047 /* Parse the accept language */
12048 while (1) {
12049
12050 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012051 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012052 al++;
12053 if (al >= end)
12054 break;
12055
12056 /* Start of the fisrt word. */
12057 token = al;
12058
12059 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012060 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012061 al++;
12062 if (al == token)
12063 goto expect_comma;
12064
12065 /* Length of the token. */
12066 toklen = al - token;
12067 qvalue = 1000;
12068
12069 /* Check if the token exists in the list. If the token not exists,
12070 * jump to the next token.
12071 */
12072 str = args[0].data.str.str;
12073 w = str;
12074 while (1) {
12075 if (*str == ';' || *str == '\0') {
12076 if (language_range_match(token, toklen, w, str-w))
12077 goto look_for_q;
12078 if (*str == '\0')
12079 goto expect_comma;
12080 w = str + 1;
12081 }
12082 str++;
12083 }
12084 goto expect_comma;
12085
12086look_for_q:
12087
12088 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012089 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012090 al++;
12091 if (al >= end)
12092 goto process_value;
12093
12094 /* If ',' is found, process the result */
12095 if (*al == ',')
12096 goto process_value;
12097
12098 /* If the character is different from ';', look
12099 * for the end of the header part in best effort.
12100 */
12101 if (*al != ';')
12102 goto expect_comma;
12103
12104 /* Assumes that the char is ';', now expect "q=". */
12105 al++;
12106
12107 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012108 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012109 al++;
12110 if (al >= end)
12111 goto process_value;
12112
12113 /* Expect 'q'. If no 'q', continue in best effort */
12114 if (*al != 'q')
12115 goto process_value;
12116 al++;
12117
12118 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012119 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012120 al++;
12121 if (al >= end)
12122 goto process_value;
12123
12124 /* Expect '='. If no '=', continue in best effort */
12125 if (*al != '=')
12126 goto process_value;
12127 al++;
12128
12129 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012130 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012131 al++;
12132 if (al >= end)
12133 goto process_value;
12134
12135 /* Parse the q value. */
12136 qvalue = parse_qvalue(al, &al);
12137
12138process_value:
12139
12140 /* If the new q value is the best q value, then store the associated
12141 * language in the response. If qvalue is the biggest value (1000),
12142 * break the process.
12143 */
12144 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012145 smp->data.u.str.str = (char *)w;
12146 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012147 if (qvalue >= 1000)
12148 break;
12149 best_q = qvalue;
12150 }
12151
12152expect_comma:
12153
12154 /* Expect comma or end. If the end is detected, quit the loop. */
12155 while (al < end && *al != ',')
12156 al++;
12157 if (al >= end)
12158 break;
12159
12160 /* Comma is found, jump it and restart the analyzer. */
12161 al++;
12162 }
12163
12164 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012165 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12166 smp->data.u.str.str = args[1].data.str.str;
12167 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012168 }
12169
12170 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012171 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012172}
12173
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012174/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012175static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012176{
12177 /* If the constant flag is set or if not size is avalaible at
12178 * the end of the buffer, copy the string in other buffer
12179 * before decoding.
12180 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012181 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012182 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012183 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12184 smp->data.u.str.str = str->str;
12185 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012186 smp->flags &= ~SMP_F_CONST;
12187 }
12188
12189 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012190 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12191 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012192 return 1;
12193}
12194
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012195static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12196{
12197 struct proxy *fe = strm_fe(smp->strm);
12198 int idx, i;
12199 struct cap_hdr *hdr;
12200 int len;
12201
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012202 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012203 return 0;
12204
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012205 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012206
12207 /* Check the availibity of the capture id. */
12208 if (idx > fe->nb_req_cap - 1)
12209 return 0;
12210
12211 /* Look for the original configuration. */
12212 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12213 hdr != NULL && i != idx ;
12214 i--, hdr = hdr->next);
12215 if (!hdr)
12216 return 0;
12217
12218 /* check for the memory allocation */
12219 if (smp->strm->req_cap[hdr->index] == NULL)
12220 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12221 if (smp->strm->req_cap[hdr->index] == NULL)
12222 return 0;
12223
12224 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012225 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012226 if (len > hdr->len)
12227 len = hdr->len;
12228
12229 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012230 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012231 smp->strm->req_cap[idx][len] = '\0';
12232
12233 return 1;
12234}
12235
12236static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12237{
12238 struct proxy *fe = strm_fe(smp->strm);
12239 int idx, i;
12240 struct cap_hdr *hdr;
12241 int len;
12242
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012243 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012244 return 0;
12245
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012246 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012247
12248 /* Check the availibity of the capture id. */
12249 if (idx > fe->nb_rsp_cap - 1)
12250 return 0;
12251
12252 /* Look for the original configuration. */
12253 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12254 hdr != NULL && i != idx ;
12255 i--, hdr = hdr->next);
12256 if (!hdr)
12257 return 0;
12258
12259 /* check for the memory allocation */
12260 if (smp->strm->res_cap[hdr->index] == NULL)
12261 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12262 if (smp->strm->res_cap[hdr->index] == NULL)
12263 return 0;
12264
12265 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012266 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012267 if (len > hdr->len)
12268 len = hdr->len;
12269
12270 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012271 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012272 smp->strm->res_cap[idx][len] = '\0';
12273
12274 return 1;
12275}
12276
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012277/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012278 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012279 * the relevant part of the request line accordingly. Then it updates various
12280 * pointers to the next elements which were moved, and the total buffer length.
12281 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012282 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12283 * error, though this can be revisited when this code is finally exploited.
12284 *
12285 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12286 * query string and 3 to replace uri.
12287 *
12288 * In query string case, the mark question '?' must be set at the start of the
12289 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012290 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012291int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012292 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012293{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012294 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012295 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012296 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012297 int delta;
12298
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012299 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012300 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012301 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012302 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12303
12304 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012305 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012306 txn->req.sl.rq.m_l += delta;
12307 txn->req.sl.rq.u += delta;
12308 txn->req.sl.rq.v += delta;
12309 break;
12310
12311 case 1: // path
12312 cur_ptr = http_get_path(txn);
12313 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012314 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012315
12316 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012317 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 +010012318 cur_end++;
12319
12320 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012321 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012322 txn->req.sl.rq.u_l += delta;
12323 txn->req.sl.rq.v += delta;
12324 break;
12325
12326 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012327 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012328 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012329 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12330 while (cur_ptr < cur_end && *cur_ptr != '?')
12331 cur_ptr++;
12332
12333 /* skip the question mark or indicate that we must insert it
12334 * (but only if the format string is not empty then).
12335 */
12336 if (cur_ptr < cur_end)
12337 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012338 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012339 offset = 0;
12340
12341 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012342 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012343 txn->req.sl.rq.u_l += delta;
12344 txn->req.sl.rq.v += delta;
12345 break;
12346
12347 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012348 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012349 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12350
12351 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012352 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012353 txn->req.sl.rq.u_l += delta;
12354 txn->req.sl.rq.v += delta;
12355 break;
12356
12357 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012358 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012359 }
12360
12361 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012362 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012363 txn->req.sl.rq.l += delta;
12364 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012365 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012366 return 0;
12367}
12368
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012369/* This function replace the HTTP status code and the associated message. The
12370 * variable <status> contains the new status code. This function never fails.
12371 */
12372void http_set_status(unsigned int status, struct stream *s)
12373{
12374 struct http_txn *txn = s->txn;
12375 char *cur_ptr, *cur_end;
12376 int delta;
12377 char *res;
12378 int c_l;
12379 const char *msg;
12380 int msg_len;
12381
12382 chunk_reset(&trash);
12383
12384 res = ultoa_o(status, trash.str, trash.size);
12385 c_l = res - trash.str;
12386
12387 trash.str[c_l] = ' ';
12388 trash.len = c_l + 1;
12389
12390 msg = get_reason(status);
12391 msg_len = strlen(msg);
12392
12393 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12394 trash.len += msg_len;
12395
12396 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12397 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12398
12399 /* commit changes and adjust message */
12400 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12401
12402 /* adjust res line offsets and lengths */
12403 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12404 txn->rsp.sl.st.c_l = c_l;
12405 txn->rsp.sl.st.r_l = msg_len;
12406
12407 delta = trash.len - (cur_end - cur_ptr);
12408 txn->rsp.sl.st.l += delta;
12409 txn->hdr_idx.v[0].len += delta;
12410 http_msg_move_end(&txn->rsp, delta);
12411}
12412
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012413/* This function executes one of the set-{method,path,query,uri} actions. It
12414 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012415 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012416 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012417 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12418 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012419 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012420enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012421 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012422{
12423 chunk_reset(&trash);
12424
12425 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012426 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012427 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012428 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012429
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012430 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012431 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012432}
12433
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012434/* This function is just a compliant action wrapper for "set-status". */
12435enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012436 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012437{
12438 http_set_status(rule->arg.status.code, s);
12439 return ACT_RET_CONT;
12440}
12441
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012442/* parse an http-request action among :
12443 * set-method
12444 * set-path
12445 * set-query
12446 * set-uri
12447 *
12448 * All of them accept a single argument of type string representing a log-format.
12449 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12450 * 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 +020012451 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012452 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012453enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12454 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012455{
12456 int cur_arg = *orig_arg;
12457
Thierry FOURNIER42148732015-09-02 17:17:33 +020012458 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012459
12460 switch (args[0][4]) {
12461 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012462 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012463 rule->action_ptr = http_action_set_req_line;
12464 break;
12465 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012466 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012467 rule->action_ptr = http_action_set_req_line;
12468 break;
12469 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012470 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012471 rule->action_ptr = http_action_set_req_line;
12472 break;
12473 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012474 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012475 rule->action_ptr = http_action_set_req_line;
12476 break;
12477 default:
12478 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012479 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012480 }
12481
12482 if (!*args[cur_arg] ||
12483 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12484 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012485 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012486 }
12487
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012488 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012489 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012490 parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012491 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12492 proxy->conf.args.file, proxy->conf.args.line);
12493
12494 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012495 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012496}
12497
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012498/* parse set-status action:
12499 * This action accepts a single argument of type int representing
12500 * an http status code. It returns ACT_RET_PRS_OK on success,
12501 * ACT_RET_PRS_ERR on error.
12502 */
12503enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12504 struct act_rule *rule, char **err)
12505{
12506 char *error;
12507
Thierry FOURNIER42148732015-09-02 17:17:33 +020012508 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012509 rule->action_ptr = action_http_set_status;
12510
12511 /* Check if an argument is available */
12512 if (!*args[*orig_arg]) {
12513 memprintf(err, "expects exactly 1 argument <status>");
12514 return ACT_RET_PRS_ERR;
12515 }
12516
12517 /* convert status code as integer */
12518 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12519 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12520 memprintf(err, "expects an integer status code between 100 and 999");
12521 return ACT_RET_PRS_ERR;
12522 }
12523
12524 (*orig_arg)++;
12525 return ACT_RET_PRS_OK;
12526}
12527
Willy Tarreaua9083d02015-05-08 15:27:59 +020012528/* This function executes the "capture" action. It executes a fetch expression,
12529 * turns the result into a string and puts it in a capture slot. It always
12530 * returns 1. If an error occurs the action is cancelled, but the rule
12531 * processing continues.
12532 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012533enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012534 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012535{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012536 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012537 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012538 char **cap = s->req_cap;
12539 int len;
12540
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012541 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 +020012542 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012543 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012544
12545 if (cap[h->index] == NULL)
12546 cap[h->index] = pool_alloc2(h->pool);
12547
12548 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012549 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012550
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012551 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012552 if (len > h->len)
12553 len = h->len;
12554
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012555 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012556 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012557 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012558}
12559
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012560/* This function executes the "capture" action and store the result in a
12561 * capture slot if exists. It executes a fetch expression, turns the result
12562 * into a string and puts it in a capture slot. It always returns 1. If an
12563 * error occurs the action is cancelled, but the rule processing continues.
12564 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012565enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012566 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012567{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012568 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012569 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012570 char **cap = s->req_cap;
12571 struct proxy *fe = strm_fe(s);
12572 int len;
12573 int i;
12574
12575 /* Look for the original configuration. */
12576 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012577 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012578 i--, h = h->next);
12579 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012580 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012581
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012582 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 +020012583 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012584 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012585
12586 if (cap[h->index] == NULL)
12587 cap[h->index] = pool_alloc2(h->pool);
12588
12589 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012590 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012591
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012592 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012593 if (len > h->len)
12594 len = h->len;
12595
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012596 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012597 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012598 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012599}
12600
Willy Tarreaua9083d02015-05-08 15:27:59 +020012601/* parse an "http-request capture" action. It takes a single argument which is
12602 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012603 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012604 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012605 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012606enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12607 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012608{
12609 struct sample_expr *expr;
12610 struct cap_hdr *hdr;
12611 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012612 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012613
12614 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12615 if (strcmp(args[cur_arg], "if") == 0 ||
12616 strcmp(args[cur_arg], "unless") == 0)
12617 break;
12618
12619 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012620 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012621 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012622 }
12623
Willy Tarreaua9083d02015-05-08 15:27:59 +020012624 cur_arg = *orig_arg;
12625 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12626 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012627 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012628
12629 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12630 memprintf(err,
12631 "fetch method '%s' extracts information from '%s', none of which is available here",
12632 args[cur_arg-1], sample_src_names(expr->fetch->use));
12633 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012634 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012635 }
12636
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012637 if (!args[cur_arg] || !*args[cur_arg]) {
12638 memprintf(err, "expects 'len or 'id'");
12639 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012640 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012641 }
12642
Willy Tarreaua9083d02015-05-08 15:27:59 +020012643 if (strcmp(args[cur_arg], "len") == 0) {
12644 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012645
12646 if (!(px->cap & PR_CAP_FE)) {
12647 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012648 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012649 }
12650
12651 proxy->conf.args.ctx = ARGC_CAP;
12652
Willy Tarreaua9083d02015-05-08 15:27:59 +020012653 if (!args[cur_arg]) {
12654 memprintf(err, "missing length value");
12655 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012656 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012657 }
12658 /* we copy the table name for now, it will be resolved later */
12659 len = atoi(args[cur_arg]);
12660 if (len <= 0) {
12661 memprintf(err, "length must be > 0");
12662 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012663 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012664 }
12665 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012666
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012667 if (!len) {
12668 memprintf(err, "a positive 'len' argument is mandatory");
12669 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012670 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012671 }
12672
12673 hdr = calloc(sizeof(struct cap_hdr), 1);
12674 hdr->next = px->req_cap;
12675 hdr->name = NULL; /* not a header capture */
12676 hdr->namelen = 0;
12677 hdr->len = len;
12678 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12679 hdr->index = px->nb_req_cap++;
12680
12681 px->req_cap = hdr;
12682 px->to_log |= LW_REQHDR;
12683
Thierry FOURNIER42148732015-09-02 17:17:33 +020012684 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012685 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012686 rule->arg.cap.expr = expr;
12687 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012688 }
12689
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012690 else if (strcmp(args[cur_arg], "id") == 0) {
12691 int id;
12692 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012693
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012694 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012695
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012696 if (!args[cur_arg]) {
12697 memprintf(err, "missing id value");
12698 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012699 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012700 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012701
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012702 id = strtol(args[cur_arg], &error, 10);
12703 if (*error != '\0') {
12704 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12705 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012706 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012707 }
12708 cur_arg++;
12709
12710 proxy->conf.args.ctx = ARGC_CAP;
12711
Thierry FOURNIER42148732015-09-02 17:17:33 +020012712 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012713 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012714 rule->arg.capid.expr = expr;
12715 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012716 }
12717
12718 else {
12719 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12720 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012721 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012722 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012723
12724 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012725 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012726}
12727
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012728/* This function executes the "capture" action and store the result in a
12729 * capture slot if exists. It executes a fetch expression, turns the result
12730 * into a string and puts it in a capture slot. It always returns 1. If an
12731 * error occurs the action is cancelled, but the rule processing continues.
12732 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012733enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012734 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012735{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012736 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012737 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012738 char **cap = s->res_cap;
12739 struct proxy *fe = strm_fe(s);
12740 int len;
12741 int i;
12742
12743 /* Look for the original configuration. */
12744 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012745 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012746 i--, h = h->next);
12747 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012748 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012749
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012750 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 +020012751 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012752 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012753
12754 if (cap[h->index] == NULL)
12755 cap[h->index] = pool_alloc2(h->pool);
12756
12757 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012758 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012759
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012760 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012761 if (len > h->len)
12762 len = h->len;
12763
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012764 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012765 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012766 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012767}
12768
12769/* parse an "http-response capture" action. It takes a single argument which is
12770 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12771 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012772 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012773 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012774enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12775 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012776{
12777 struct sample_expr *expr;
12778 int cur_arg;
12779 int id;
12780 char *error;
12781
12782 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12783 if (strcmp(args[cur_arg], "if") == 0 ||
12784 strcmp(args[cur_arg], "unless") == 0)
12785 break;
12786
12787 if (cur_arg < *orig_arg + 3) {
12788 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012789 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012790 }
12791
12792 cur_arg = *orig_arg;
12793 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12794 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012795 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012796
12797 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12798 memprintf(err,
12799 "fetch method '%s' extracts information from '%s', none of which is available here",
12800 args[cur_arg-1], sample_src_names(expr->fetch->use));
12801 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012802 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012803 }
12804
12805 if (!args[cur_arg] || !*args[cur_arg]) {
12806 memprintf(err, "expects 'len or 'id'");
12807 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012808 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012809 }
12810
12811 if (strcmp(args[cur_arg], "id") != 0) {
12812 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12813 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012814 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012815 }
12816
12817 cur_arg++;
12818
12819 if (!args[cur_arg]) {
12820 memprintf(err, "missing id value");
12821 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012822 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012823 }
12824
12825 id = strtol(args[cur_arg], &error, 10);
12826 if (*error != '\0') {
12827 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12828 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012829 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012830 }
12831 cur_arg++;
12832
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012833 proxy->conf.args.ctx = ARGC_CAP;
12834
Thierry FOURNIER42148732015-09-02 17:17:33 +020012835 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012836 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012837 rule->arg.capid.expr = expr;
12838 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012839
12840 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012841 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012842}
12843
William Lallemand73025dd2014-04-24 14:38:37 +020012844/*
12845 * Return the struct http_req_action_kw associated to a keyword.
12846 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012847struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012848{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012849 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012850}
12851
12852/*
12853 * Return the struct http_res_action_kw associated to a keyword.
12854 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012855struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012856{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012857 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012858}
12859
Willy Tarreau4a568972010-05-12 08:08:50 +020012860/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012861/* All supported ACL keywords must be declared here. */
12862/************************************************************************/
12863
12864/* Note: must not be declared <const> as its list will be overwritten.
12865 * Please take care of keeping this list alphabetically sorted.
12866 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012867static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012868 { "base", "base", PAT_MATCH_STR },
12869 { "base_beg", "base", PAT_MATCH_BEG },
12870 { "base_dir", "base", PAT_MATCH_DIR },
12871 { "base_dom", "base", PAT_MATCH_DOM },
12872 { "base_end", "base", PAT_MATCH_END },
12873 { "base_len", "base", PAT_MATCH_LEN },
12874 { "base_reg", "base", PAT_MATCH_REG },
12875 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012876
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012877 { "cook", "req.cook", PAT_MATCH_STR },
12878 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12879 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12880 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12881 { "cook_end", "req.cook", PAT_MATCH_END },
12882 { "cook_len", "req.cook", PAT_MATCH_LEN },
12883 { "cook_reg", "req.cook", PAT_MATCH_REG },
12884 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012885
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012886 { "hdr", "req.hdr", PAT_MATCH_STR },
12887 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12888 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12889 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12890 { "hdr_end", "req.hdr", PAT_MATCH_END },
12891 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12892 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12893 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012894
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012895 /* these two declarations uses strings with list storage (in place
12896 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12897 * and delete functions are relative to the list management. The parse
12898 * and match method are related to the corresponding fetch methods. This
12899 * is very particular ACL declaration mode.
12900 */
12901 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12902 { "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 +020012903
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012904 { "path", "path", PAT_MATCH_STR },
12905 { "path_beg", "path", PAT_MATCH_BEG },
12906 { "path_dir", "path", PAT_MATCH_DIR },
12907 { "path_dom", "path", PAT_MATCH_DOM },
12908 { "path_end", "path", PAT_MATCH_END },
12909 { "path_len", "path", PAT_MATCH_LEN },
12910 { "path_reg", "path", PAT_MATCH_REG },
12911 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012912
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012913 { "req_ver", "req.ver", PAT_MATCH_STR },
12914 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012915
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012916 { "scook", "res.cook", PAT_MATCH_STR },
12917 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12918 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12919 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12920 { "scook_end", "res.cook", PAT_MATCH_END },
12921 { "scook_len", "res.cook", PAT_MATCH_LEN },
12922 { "scook_reg", "res.cook", PAT_MATCH_REG },
12923 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012924
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012925 { "shdr", "res.hdr", PAT_MATCH_STR },
12926 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12927 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12928 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12929 { "shdr_end", "res.hdr", PAT_MATCH_END },
12930 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12931 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12932 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012933
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012934 { "url", "url", PAT_MATCH_STR },
12935 { "url_beg", "url", PAT_MATCH_BEG },
12936 { "url_dir", "url", PAT_MATCH_DIR },
12937 { "url_dom", "url", PAT_MATCH_DOM },
12938 { "url_end", "url", PAT_MATCH_END },
12939 { "url_len", "url", PAT_MATCH_LEN },
12940 { "url_reg", "url", PAT_MATCH_REG },
12941 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012942
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012943 { "urlp", "urlp", PAT_MATCH_STR },
12944 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12945 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12946 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12947 { "urlp_end", "urlp", PAT_MATCH_END },
12948 { "urlp_len", "urlp", PAT_MATCH_LEN },
12949 { "urlp_reg", "urlp", PAT_MATCH_REG },
12950 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012951
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012952 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012953}};
12954
12955/************************************************************************/
12956/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012957/************************************************************************/
12958/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012959static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012960 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012961 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012962 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12963
Willy Tarreau87b09662015-04-03 00:22:06 +020012964 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012965 { "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 +020012966
12967 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012968 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12969 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12970 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012971
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012972 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12973 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012974
Willy Tarreau409bcde2013-01-08 00:31:00 +010012975 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12976 * are only here to match the ACL's name, are request-only and are used
12977 * for ACL compatibility only.
12978 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012979 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12980 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012981 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12982 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012983
12984 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12985 * only here to match the ACL's name, are request-only and are used for
12986 * ACL compatibility only.
12987 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012988 { "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 +020012989 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012990 { "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 +020012991 { "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 +010012992
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012993 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012994 { "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 +010012995 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012996 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012997 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012998 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012999
13000 /* HTTP protocol on the request path */
13001 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013002 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013003
13004 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013005 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
13006 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013007
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013008 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013009 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13010 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013011 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013012
Willy Tarreau18ed2562013-01-14 15:56:36 +010013013 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013014 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13015 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013016
Willy Tarreau18ed2562013-01-14 15:56:36 +010013017 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013018 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013019 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13020 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013021
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013022 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013023 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013024 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013025 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013026 { "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 +010013027 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013028 { "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 +010013029
13030 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013031 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013032 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13033 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013034
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013035 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013036 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013037 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013038 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013039 { "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 +010013040 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013041 { "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 +010013042
Willy Tarreau409bcde2013-01-08 00:31:00 +010013043 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013044 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013045 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13046 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013047 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013048
13049 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013050 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013051 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013052 { "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 +020013053 { "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 +010013054
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013055 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013056 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013057 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013058 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013059 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013060 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013061 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13062 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013063 { "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 +010013064 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013065}};
13066
Willy Tarreau8797c062007-05-07 00:55:35 +020013067
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013068/************************************************************************/
13069/* All supported converter keywords must be declared here. */
13070/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013071/* Note: must not be declared <const> as its list will be overwritten */
13072static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013073 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013074 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013075 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13076 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013077 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013078 { NULL, NULL, 0, 0, 0 },
13079}};
13080
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013081
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013082/************************************************************************/
13083/* All supported http-request action keywords must be declared here. */
13084/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013085struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013086 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013087 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013088 { "set-method", parse_set_req_line },
13089 { "set-path", parse_set_req_line },
13090 { "set-query", parse_set_req_line },
13091 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013092 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013093 }
13094};
13095
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013096struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013097 .kw = {
13098 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013099 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013100 { NULL, NULL }
13101 }
13102};
13103
Willy Tarreau8797c062007-05-07 00:55:35 +020013104__attribute__((constructor))
13105static void __http_protocol_init(void)
13106{
13107 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013108 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013109 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013110 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013111 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020013112}
13113
13114
Willy Tarreau58f10d72006-12-04 02:26:12 +010013115/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013116 * Local variables:
13117 * c-indent-level: 8
13118 * c-basic-offset: 8
13119 * End:
13120 */