blob: bb1ef698a3679c57910572b322177068e568f42e [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreau8797c062007-05-07 00:55:35 +020044#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020045#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020064#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020068#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020069
Willy Tarreau522d6c02009-12-06 18:49:18 +010070const char HTTP_100[] =
71 "HTTP/1.1 100 Continue\r\n\r\n";
72
73const struct chunk http_100_chunk = {
74 .str = (char *)&HTTP_100,
75 .len = sizeof(HTTP_100)-1
76};
77
Willy Tarreaua9679ac2010-01-03 17:32:57 +010078/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020079const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010081 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020082 "Location: "; /* not terminated since it will be concatenated with the URL */
83
Willy Tarreau0f772532006-12-23 20:51:41 +010084const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010088 "Location: "; /* not terminated since it will be concatenated with the URL */
89
90/* same as 302 except that the browser MUST retry with the GET method */
91const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010094 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010095 "Location: "; /* not terminated since it will be concatenated with the URL */
96
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040097
98/* same as 302 except that the browser MUST retry with the same method */
99const char *HTTP_307 =
100 "HTTP/1.1 307 Temporary Redirect\r\n"
101 "Cache-Control: no-cache\r\n"
102 "Content-length: 0\r\n"
103 "Location: "; /* not terminated since it will be concatenated with the URL */
104
105/* same as 301 except that the browser MUST retry with the same method */
106const char *HTTP_308 =
107 "HTTP/1.1 308 Permanent Redirect\r\n"
108 "Content-length: 0\r\n"
109 "Location: "; /* not terminated since it will be concatenated with the URL */
110
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
112const char *HTTP_401_fmt =
113 "HTTP/1.0 401 Unauthorized\r\n"
114 "Cache-Control: no-cache\r\n"
115 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200116 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
118 "\r\n"
119 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
120
Willy Tarreau844a7e72010-01-31 21:46:18 +0100121const char *HTTP_407_fmt =
122 "HTTP/1.0 407 Unauthorized\r\n"
123 "Cache-Control: no-cache\r\n"
124 "Connection: close\r\n"
125 "Content-Type: text/html\r\n"
126 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
127 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800128 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100129
Willy Tarreau0f772532006-12-23 20:51:41 +0100130
131const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200132 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100133 [HTTP_ERR_400] = 400,
134 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400135 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100136 [HTTP_ERR_408] = 408,
CJ Ess108b1dd2015-04-07 12:03:37 -0400137 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 [HTTP_ERR_500] = 500,
139 [HTTP_ERR_502] = 502,
140 [HTTP_ERR_503] = 503,
141 [HTTP_ERR_504] = 504,
142};
143
Willy Tarreau80587432006-12-24 17:47:20 +0100144static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200145 [HTTP_ERR_200] =
146 "HTTP/1.0 200 OK\r\n"
147 "Cache-Control: no-cache\r\n"
148 "Connection: close\r\n"
149 "Content-Type: text/html\r\n"
150 "\r\n"
151 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
152
Willy Tarreau0f772532006-12-23 20:51:41 +0100153 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100154 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100155 "Cache-Control: no-cache\r\n"
156 "Connection: close\r\n"
157 "Content-Type: text/html\r\n"
158 "\r\n"
159 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
160
161 [HTTP_ERR_403] =
162 "HTTP/1.0 403 Forbidden\r\n"
163 "Cache-Control: no-cache\r\n"
164 "Connection: close\r\n"
165 "Content-Type: text/html\r\n"
166 "\r\n"
167 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
168
CJ Ess108b1dd2015-04-07 12:03:37 -0400169 [HTTP_ERR_405] =
170 "HTTP/1.0 405 Method Not Allowed\r\n"
171 "Cache-Control: no-cache\r\n"
172 "Connection: close\r\n"
173 "Content-Type: text/html\r\n"
174 "\r\n"
175 "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n",
176
Willy Tarreau0f772532006-12-23 20:51:41 +0100177 [HTTP_ERR_408] =
178 "HTTP/1.0 408 Request Time-out\r\n"
179 "Cache-Control: no-cache\r\n"
180 "Connection: close\r\n"
181 "Content-Type: text/html\r\n"
182 "\r\n"
183 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
184
CJ Ess108b1dd2015-04-07 12:03:37 -0400185 [HTTP_ERR_429] =
186 "HTTP/1.0 429 Too Many Requests\r\n"
187 "Cache-Control: no-cache\r\n"
188 "Connection: close\r\n"
189 "Content-Type: text/html\r\n"
190 "\r\n"
191 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
192
Willy Tarreau0f772532006-12-23 20:51:41 +0100193 [HTTP_ERR_500] =
194 "HTTP/1.0 500 Server Error\r\n"
195 "Cache-Control: no-cache\r\n"
196 "Connection: close\r\n"
197 "Content-Type: text/html\r\n"
198 "\r\n"
199 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
200
201 [HTTP_ERR_502] =
202 "HTTP/1.0 502 Bad Gateway\r\n"
203 "Cache-Control: no-cache\r\n"
204 "Connection: close\r\n"
205 "Content-Type: text/html\r\n"
206 "\r\n"
207 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
208
209 [HTTP_ERR_503] =
210 "HTTP/1.0 503 Service Unavailable\r\n"
211 "Cache-Control: no-cache\r\n"
212 "Connection: close\r\n"
213 "Content-Type: text/html\r\n"
214 "\r\n"
215 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
216
217 [HTTP_ERR_504] =
218 "HTTP/1.0 504 Gateway Time-out\r\n"
219 "Cache-Control: no-cache\r\n"
220 "Connection: close\r\n"
221 "Content-Type: text/html\r\n"
222 "\r\n"
223 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
224
225};
226
Cyril Bonté19979e12012-04-04 12:57:21 +0200227/* status codes available for the stats admin page (strictly 4 chars length) */
228const char *stat_status_codes[STAT_STATUS_SIZE] = {
229 [STAT_STATUS_DENY] = "DENY",
230 [STAT_STATUS_DONE] = "DONE",
231 [STAT_STATUS_ERRP] = "ERRP",
232 [STAT_STATUS_EXCD] = "EXCD",
233 [STAT_STATUS_NONE] = "NONE",
234 [STAT_STATUS_PART] = "PART",
235 [STAT_STATUS_UNKN] = "UNKN",
236};
237
238
William Lallemand73025dd2014-04-24 14:38:37 +0200239/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200240struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200241 .list = LIST_HEAD_INIT(http_req_keywords.list)
242};
243
244/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200245struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200246 .list = LIST_HEAD_INIT(http_res_keywords.list)
247};
248
Willy Tarreau80587432006-12-24 17:47:20 +0100249/* We must put the messages here since GCC cannot initialize consts depending
250 * on strlen().
251 */
252struct chunk http_err_chunks[HTTP_ERR_SIZE];
253
Willy Tarreaua890d072013-04-02 12:01:06 +0200254/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
255static struct hdr_ctx static_hdr_ctx;
256
Willy Tarreau42250582007-04-01 01:30:43 +0200257#define FD_SETS_ARE_BITFIELDS
258#ifdef FD_SETS_ARE_BITFIELDS
259/*
260 * This map is used with all the FD_* macros to check whether a particular bit
261 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
262 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
263 * byte should be encoded. Be careful to always pass bytes from 0 to 255
264 * exclusively to the macros.
265 */
266fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
267fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100268fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200269
270#else
271#error "Check if your OS uses bitfields for fd_sets"
272#endif
273
Willy Tarreau87b09662015-04-03 00:22:06 +0200274static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200275
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200276/* This function returns a reason associated with the HTTP status.
277 * This function never fails, a message is always returned.
278 */
279const char *get_reason(unsigned int status)
280{
281 switch (status) {
282 case 100: return "Continue";
283 case 101: return "Switching Protocols";
284 case 102: return "Processing";
285 case 200: return "OK";
286 case 201: return "Created";
287 case 202: return "Accepted";
288 case 203: return "Non-Authoritative Information";
289 case 204: return "No Content";
290 case 205: return "Reset Content";
291 case 206: return "Partial Content";
292 case 207: return "Multi-Status";
293 case 210: return "Content Different";
294 case 226: return "IM Used";
295 case 300: return "Multiple Choices";
296 case 301: return "Moved Permanently";
297 case 302: return "Moved Temporarily";
298 case 303: return "See Other";
299 case 304: return "Not Modified";
300 case 305: return "Use Proxy";
301 case 307: return "Temporary Redirect";
302 case 308: return "Permanent Redirect";
303 case 310: return "Too many Redirects";
304 case 400: return "Bad Request";
305 case 401: return "Unauthorized";
306 case 402: return "Payment Required";
307 case 403: return "Forbidden";
308 case 404: return "Not Found";
309 case 405: return "Method Not Allowed";
310 case 406: return "Not Acceptable";
311 case 407: return "Proxy Authentication Required";
312 case 408: return "Request Time-out";
313 case 409: return "Conflict";
314 case 410: return "Gone";
315 case 411: return "Length Required";
316 case 412: return "Precondition Failed";
317 case 413: return "Request Entity Too Large";
318 case 414: return "Request-URI Too Long";
319 case 415: return "Unsupported Media Type";
320 case 416: return "Requested range unsatisfiable";
321 case 417: return "Expectation failed";
322 case 418: return "I'm a teapot";
323 case 422: return "Unprocessable entity";
324 case 423: return "Locked";
325 case 424: return "Method failure";
326 case 425: return "Unordered Collection";
327 case 426: return "Upgrade Required";
328 case 428: return "Precondition Required";
329 case 429: return "Too Many Requests";
330 case 431: return "Request Header Fields Too Large";
331 case 449: return "Retry With";
332 case 450: return "Blocked by Windows Parental Controls";
333 case 451: return "Unavailable For Legal Reasons";
334 case 456: return "Unrecoverable Error";
335 case 499: return "client has closed connection";
336 case 500: return "Internal Server Error";
337 case 501: return "Not Implemented";
338 case 502: return "Bad Gateway ou Proxy Error";
339 case 503: return "Service Unavailable";
340 case 504: return "Gateway Time-out";
341 case 505: return "HTTP Version not supported";
342 case 506: return "Variant also negociate";
343 case 507: return "Insufficient storage";
344 case 508: return "Loop detected";
345 case 509: return "Bandwidth Limit Exceeded";
346 case 510: return "Not extended";
347 case 511: return "Network authentication required";
348 case 520: return "Web server is returning an unknown error";
349 default:
350 switch (status) {
351 case 100 ... 199: return "Informational";
352 case 200 ... 299: return "Success";
353 case 300 ... 399: return "Redirection";
354 case 400 ... 499: return "Client Error";
355 case 500 ... 599: return "Server Error";
356 default: return "Other";
357 }
358 }
359}
360
Willy Tarreau80587432006-12-24 17:47:20 +0100361void init_proto_http()
362{
Willy Tarreau42250582007-04-01 01:30:43 +0200363 int i;
364 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100365 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200366
Willy Tarreau80587432006-12-24 17:47:20 +0100367 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
368 if (!http_err_msgs[msg]) {
369 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
370 abort();
371 }
372
373 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
374 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
375 }
Willy Tarreau42250582007-04-01 01:30:43 +0200376
377 /* initialize the log header encoding map : '{|}"#' should be encoded with
378 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
379 * URL encoding only requires '"', '#' to be encoded as well as non-
380 * printable characters above.
381 */
382 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
383 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100384 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200385 for (i = 0; i < 32; i++) {
386 FD_SET(i, hdr_encode_map);
387 FD_SET(i, url_encode_map);
388 }
389 for (i = 127; i < 256; i++) {
390 FD_SET(i, hdr_encode_map);
391 FD_SET(i, url_encode_map);
392 }
393
394 tmp = "\"#{|}";
395 while (*tmp) {
396 FD_SET(*tmp, hdr_encode_map);
397 tmp++;
398 }
399
400 tmp = "\"#";
401 while (*tmp) {
402 FD_SET(*tmp, url_encode_map);
403 tmp++;
404 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200405
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100406 /* initialize the http header encoding map. The draft httpbis define the
407 * header content as:
408 *
409 * HTTP-message = start-line
410 * *( header-field CRLF )
411 * CRLF
412 * [ message-body ]
413 * header-field = field-name ":" OWS field-value OWS
414 * field-value = *( field-content / obs-fold )
415 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
416 * obs-fold = CRLF 1*( SP / HTAB )
417 * field-vchar = VCHAR / obs-text
418 * VCHAR = %x21-7E
419 * obs-text = %x80-FF
420 *
421 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
422 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
423 * "obs-fold" is volontary forgotten because haproxy remove this.
424 */
425 memset(http_encode_map, 0, sizeof(http_encode_map));
426 for (i = 0x00; i <= 0x08; i++)
427 FD_SET(i, http_encode_map);
428 for (i = 0x0a; i <= 0x1f; i++)
429 FD_SET(i, http_encode_map);
430 FD_SET(0x7f, http_encode_map);
431
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200432 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200433 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200434 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100435 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100436}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200437
Willy Tarreau53b6c742006-12-17 13:37:46 +0100438/*
439 * We have 26 list of methods (1 per first letter), each of which can have
440 * up to 3 entries (2 valid, 1 null).
441 */
442struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100443 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100444 int len;
445 const char text[8];
446};
447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100448const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100449 ['C' - 'A'] = {
450 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
451 },
452 ['D' - 'A'] = {
453 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
454 },
455 ['G' - 'A'] = {
456 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
457 },
458 ['H' - 'A'] = {
459 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
460 },
461 ['P' - 'A'] = {
462 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
463 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
464 },
465 ['T' - 'A'] = {
466 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
467 },
468 /* rest is empty like this :
469 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
470 */
471};
472
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100473const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
474 [HTTP_METH_NONE] = { "", 0 },
475 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
476 [HTTP_METH_GET] = { "GET", 3 },
477 [HTTP_METH_HEAD] = { "HEAD", 4 },
478 [HTTP_METH_POST] = { "POST", 4 },
479 [HTTP_METH_PUT] = { "PUT", 3 },
480 [HTTP_METH_DELETE] = { "DELETE", 6 },
481 [HTTP_METH_TRACE] = { "TRACE", 5 },
482 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
483};
484
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100485/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200486 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100487 * RFC2616 for those chars.
488 */
489
490const char http_is_spht[256] = {
491 [' '] = 1, ['\t'] = 1,
492};
493
494const char http_is_crlf[256] = {
495 ['\r'] = 1, ['\n'] = 1,
496};
497
498const char http_is_lws[256] = {
499 [' '] = 1, ['\t'] = 1,
500 ['\r'] = 1, ['\n'] = 1,
501};
502
503const char http_is_sep[256] = {
504 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
505 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
506 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
507 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
508 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
509};
510
511const char http_is_ctl[256] = {
512 [0 ... 31] = 1,
513 [127] = 1,
514};
515
516/*
517 * A token is any ASCII char that is neither a separator nor a CTL char.
518 * Do not overwrite values in assignment since gcc-2.95 will not handle
519 * them correctly. Instead, define every non-CTL char's status.
520 */
521const char http_is_token[256] = {
522 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
523 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
524 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
525 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
526 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
527 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
528 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
529 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
530 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
531 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
532 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
533 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
534 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
535 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
536 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
537 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
538 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
539 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
540 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
541 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
542 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
543 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
544 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
545 ['|'] = 1, ['}'] = 0, ['~'] = 1,
546};
547
548
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100549/*
550 * An http ver_token is any ASCII which can be found in an HTTP version,
551 * which includes 'H', 'T', 'P', '/', '.' and any digit.
552 */
553const char http_is_ver_token[256] = {
554 ['.'] = 1, ['/'] = 1,
555 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
556 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
Thierry FOURNIER63d692c2015-02-28 19:03:56 +0100557 ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1,
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100558};
559
560
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100561/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100562 * Adds a header and its CRLF at the tail of the message's buffer, just before
563 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100564 * The header is also automatically added to the index <hdr_idx>, and the end
565 * of headers is automatically adjusted. The number of bytes added is returned
566 * on success, otherwise <0 is returned indicating an error.
567 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100568int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100569{
570 int bytes, len;
571
572 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200573 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100574 if (!bytes)
575 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100576 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100577 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
578}
579
580/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100581 * Adds a header and its CRLF at the tail of the message's buffer, just before
582 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100583 * the buffer is only opened and the space reserved, but nothing is copied.
584 * The header is also automatically added to the index <hdr_idx>, and the end
585 * of headers is automatically adjusted. The number of bytes added is returned
586 * on success, otherwise <0 is returned indicating an error.
587 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100588int http_header_add_tail2(struct http_msg *msg,
589 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100590{
591 int bytes;
592
Willy Tarreau9b28e032012-10-12 23:49:43 +0200593 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100594 if (!bytes)
595 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100596 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100597 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
598}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200599
600/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100601 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
602 * If so, returns the position of the first non-space character relative to
603 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
604 * to return a pointer to the place after the first space. Returns 0 if the
605 * header name does not match. Checks are case-insensitive.
606 */
607int http_header_match2(const char *hdr, const char *end,
608 const char *name, int len)
609{
610 const char *val;
611
612 if (hdr + len >= end)
613 return 0;
614 if (hdr[len] != ':')
615 return 0;
616 if (strncasecmp(hdr, name, len) != 0)
617 return 0;
618 val = hdr + len + 1;
619 while (val < end && HTTP_IS_SPHT(*val))
620 val++;
621 if ((val >= end) && (len + 2 <= end - hdr))
622 return len + 2; /* we may replace starting from second space */
623 return val - hdr;
624}
625
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200626/* Find the first or next occurrence of header <name> in message buffer <sol>
627 * using headers index <idx>, and return it in the <ctx> structure. This
628 * structure holds everything necessary to use the header and find next
629 * occurrence. If its <idx> member is 0, the header is searched from the
630 * beginning. Otherwise, the next occurrence is returned. The function returns
631 * 1 when it finds a value, and 0 when there is no more. It is very similar to
632 * http_find_header2() except that it is designed to work with full-line headers
633 * whose comma is not a delimiter but is part of the syntax. As a special case,
634 * if ctx->val is NULL when searching for a new values of a header, the current
635 * header is rescanned. This allows rescanning after a header deletion.
636 */
637int http_find_full_header2(const char *name, int len,
638 char *sol, struct hdr_idx *idx,
639 struct hdr_ctx *ctx)
640{
641 char *eol, *sov;
642 int cur_idx, old_idx;
643
644 cur_idx = ctx->idx;
645 if (cur_idx) {
646 /* We have previously returned a header, let's search another one */
647 sol = ctx->line;
648 eol = sol + idx->v[cur_idx].len;
649 goto next_hdr;
650 }
651
652 /* first request for this header */
653 sol += hdr_idx_first_pos(idx);
654 old_idx = 0;
655 cur_idx = hdr_idx_first_idx(idx);
656 while (cur_idx) {
657 eol = sol + idx->v[cur_idx].len;
658
659 if (len == 0) {
660 /* No argument was passed, we want any header.
661 * To achieve this, we simply build a fake request. */
662 while (sol + len < eol && sol[len] != ':')
663 len++;
664 name = sol;
665 }
666
667 if ((len < eol - sol) &&
668 (sol[len] == ':') &&
669 (strncasecmp(sol, name, len) == 0)) {
670 ctx->del = len;
671 sov = sol + len + 1;
672 while (sov < eol && http_is_lws[(unsigned char)*sov])
673 sov++;
674
675 ctx->line = sol;
676 ctx->prev = old_idx;
677 ctx->idx = cur_idx;
678 ctx->val = sov - sol;
679 ctx->tws = 0;
680 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
681 eol--;
682 ctx->tws++;
683 }
684 ctx->vlen = eol - sov;
685 return 1;
686 }
687 next_hdr:
688 sol = eol + idx->v[cur_idx].cr + 1;
689 old_idx = cur_idx;
690 cur_idx = idx->v[cur_idx].next;
691 }
692 return 0;
693}
694
Willy Tarreauc90dc232015-02-20 13:51:36 +0100695/* Find the first or next header field in message buffer <sol> using headers
696 * index <idx>, and return it in the <ctx> structure. This structure holds
697 * everything necessary to use the header and find next occurrence. If its
698 * <idx> member is 0, the first header is retrieved. Otherwise, the next
699 * occurrence is returned. The function returns 1 when it finds a value, and
700 * 0 when there is no more. It is equivalent to http_find_full_header2() with
701 * no header name.
702 */
703int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
704{
705 char *eol, *sov;
706 int cur_idx, old_idx;
707 int len;
708
709 cur_idx = ctx->idx;
710 if (cur_idx) {
711 /* We have previously returned a header, let's search another one */
712 sol = ctx->line;
713 eol = sol + idx->v[cur_idx].len;
714 goto next_hdr;
715 }
716
717 /* first request for this header */
718 sol += hdr_idx_first_pos(idx);
719 old_idx = 0;
720 cur_idx = hdr_idx_first_idx(idx);
721 while (cur_idx) {
722 eol = sol + idx->v[cur_idx].len;
723
724 len = 0;
725 while (1) {
726 if (len >= eol - sol)
727 goto next_hdr;
728 if (sol[len] == ':')
729 break;
730 len++;
731 }
732
733 ctx->del = len;
734 sov = sol + len + 1;
735 while (sov < eol && http_is_lws[(unsigned char)*sov])
736 sov++;
737
738 ctx->line = sol;
739 ctx->prev = old_idx;
740 ctx->idx = cur_idx;
741 ctx->val = sov - sol;
742 ctx->tws = 0;
743
744 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
745 eol--;
746 ctx->tws++;
747 }
748 ctx->vlen = eol - sov;
749 return 1;
750
751 next_hdr:
752 sol = eol + idx->v[cur_idx].cr + 1;
753 old_idx = cur_idx;
754 cur_idx = idx->v[cur_idx].next;
755 }
756 return 0;
757}
758
Willy Tarreau68085d82010-01-18 14:54:04 +0100759/* Find the end of the header value contained between <s> and <e>. See RFC2616,
760 * par 2.2 for more information. Note that it requires a valid header to return
761 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200762 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100763char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200764{
765 int quoted, qdpair;
766
767 quoted = qdpair = 0;
768 for (; s < e; s++) {
769 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200770 else if (quoted) {
771 if (*s == '\\') qdpair = 1;
772 else if (*s == '"') quoted = 0;
773 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200774 else if (*s == '"') quoted = 1;
775 else if (*s == ',') return s;
776 }
777 return s;
778}
779
780/* Find the first or next occurrence of header <name> in message buffer <sol>
781 * using headers index <idx>, and return it in the <ctx> structure. This
782 * structure holds everything necessary to use the header and find next
783 * occurrence. If its <idx> member is 0, the header is searched from the
784 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100785 * 1 when it finds a value, and 0 when there is no more. It is designed to work
786 * with headers defined as comma-separated lists. As a special case, if ctx->val
787 * is NULL when searching for a new values of a header, the current header is
788 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200789 */
790int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100791 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200792 struct hdr_ctx *ctx)
793{
Willy Tarreau68085d82010-01-18 14:54:04 +0100794 char *eol, *sov;
795 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200796
Willy Tarreau68085d82010-01-18 14:54:04 +0100797 cur_idx = ctx->idx;
798 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200799 /* We have previously returned a value, let's search
800 * another one on the same line.
801 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200802 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200803 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100804 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200805 eol = sol + idx->v[cur_idx].len;
806
807 if (sov >= eol)
808 /* no more values in this header */
809 goto next_hdr;
810
Willy Tarreau68085d82010-01-18 14:54:04 +0100811 /* values remaining for this header, skip the comma but save it
812 * for later use (eg: for header deletion).
813 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200814 sov++;
815 while (sov < eol && http_is_lws[(unsigned char)*sov])
816 sov++;
817
818 goto return_hdr;
819 }
820
821 /* first request for this header */
822 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100823 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200824 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200825 while (cur_idx) {
826 eol = sol + idx->v[cur_idx].len;
827
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200828 if (len == 0) {
829 /* No argument was passed, we want any header.
830 * To achieve this, we simply build a fake request. */
831 while (sol + len < eol && sol[len] != ':')
832 len++;
833 name = sol;
834 }
835
Willy Tarreau33a7e692007-06-10 19:45:56 +0200836 if ((len < eol - sol) &&
837 (sol[len] == ':') &&
838 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100839 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200840 sov = sol + len + 1;
841 while (sov < eol && http_is_lws[(unsigned char)*sov])
842 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100843
Willy Tarreau33a7e692007-06-10 19:45:56 +0200844 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100845 ctx->prev = old_idx;
846 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200847 ctx->idx = cur_idx;
848 ctx->val = sov - sol;
849
850 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200851 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200852 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200853 eol--;
854 ctx->tws++;
855 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200856 ctx->vlen = eol - sov;
857 return 1;
858 }
859 next_hdr:
860 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100861 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200862 cur_idx = idx->v[cur_idx].next;
863 }
864 return 0;
865}
866
867int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100868 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200869 struct hdr_ctx *ctx)
870{
871 return http_find_header2(name, strlen(name), sol, idx, ctx);
872}
873
Willy Tarreau68085d82010-01-18 14:54:04 +0100874/* Remove one value of a header. This only works on a <ctx> returned by one of
875 * the http_find_header functions. The value is removed, as well as surrounding
876 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100877 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100878 * message <msg>. The new index is returned. If it is zero, it means there is
879 * no more header, so any processing may stop. The ctx is always left in a form
880 * that can be handled by http_find_header2() to find next occurrence.
881 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100882int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100883{
884 int cur_idx = ctx->idx;
885 char *sol = ctx->line;
886 struct hdr_idx_elem *hdr;
887 int delta, skip_comma;
888
889 if (!cur_idx)
890 return 0;
891
892 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200893 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100894 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200895 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100896 http_msg_move_end(msg, delta);
897 idx->used--;
898 hdr->len = 0; /* unused entry */
899 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100900 if (idx->tail == ctx->idx)
901 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100902 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100903 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100904 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200905 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100906 return ctx->idx;
907 }
908
909 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200910 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
911 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100912 */
913
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200914 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200915 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200916 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100917 NULL, 0);
918 hdr->len += delta;
919 http_msg_move_end(msg, delta);
920 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200921 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100922 return ctx->idx;
923}
924
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100925/* This function handles a server error at the stream interface level. The
926 * stream interface is assumed to be already in a closed state. An optional
927 * message is copied into the input buffer, and an HTTP status code stored.
928 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100929 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200930 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200931static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100932 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200933{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100934 channel_auto_read(si_oc(si));
935 channel_abort(si_oc(si));
936 channel_auto_close(si_oc(si));
937 channel_erase(si_oc(si));
938 channel_auto_close(si_ic(si));
939 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100940 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200941 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100942 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200943 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200944 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200945 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200946 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200947 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200948}
949
Willy Tarreau87b09662015-04-03 00:22:06 +0200950/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100951 * and message.
952 */
953
Willy Tarreau87b09662015-04-03 00:22:06 +0200954struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100955{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200956 if (s->be->errmsg[msgnum].str)
957 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200958 else if (strm_fe(s)->errmsg[msgnum].str)
959 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100960 else
961 return &http_err_chunks[msgnum];
962}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200963
Willy Tarreau53b6c742006-12-17 13:37:46 +0100964/*
965 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
966 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
967 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100968enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100969{
970 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100971 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100972
973 m = ((unsigned)*str - 'A');
974
975 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100976 for (h = http_methods[m]; h->len > 0; h++) {
977 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100978 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100979 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100980 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100981 };
982 return HTTP_METH_OTHER;
983 }
984 return HTTP_METH_NONE;
985
986}
987
Willy Tarreau21d2af32008-02-14 20:25:24 +0100988/* Parse the URI from the given transaction (which is assumed to be in request
989 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
990 * It is returned otherwise.
991 */
992static char *
993http_get_path(struct http_txn *txn)
994{
995 char *ptr, *end;
996
Willy Tarreau9b28e032012-10-12 23:49:43 +0200997 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100998 end = ptr + txn->req.sl.rq.u_l;
999
1000 if (ptr >= end)
1001 return NULL;
1002
1003 /* RFC2616, par. 5.1.2 :
1004 * Request-URI = "*" | absuri | abspath | authority
1005 */
1006
1007 if (*ptr == '*')
1008 return NULL;
1009
1010 if (isalpha((unsigned char)*ptr)) {
1011 /* this is a scheme as described by RFC3986, par. 3.1 */
1012 ptr++;
1013 while (ptr < end &&
1014 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1015 ptr++;
1016 /* skip '://' */
1017 if (ptr == end || *ptr++ != ':')
1018 return NULL;
1019 if (ptr == end || *ptr++ != '/')
1020 return NULL;
1021 if (ptr == end || *ptr++ != '/')
1022 return NULL;
1023 }
1024 /* skip [user[:passwd]@]host[:[port]] */
1025
1026 while (ptr < end && *ptr != '/')
1027 ptr++;
1028
1029 if (ptr == end)
1030 return NULL;
1031
1032 /* OK, we got the '/' ! */
1033 return ptr;
1034}
1035
William Lallemand65ad6e12014-01-31 15:08:02 +01001036/* Parse the URI from the given string and look for the "/" beginning the PATH.
1037 * If not found, return NULL. It is returned otherwise.
1038 */
1039static char *
1040http_get_path_from_string(char *str)
1041{
1042 char *ptr = str;
1043
1044 /* RFC2616, par. 5.1.2 :
1045 * Request-URI = "*" | absuri | abspath | authority
1046 */
1047
1048 if (*ptr == '*')
1049 return NULL;
1050
1051 if (isalpha((unsigned char)*ptr)) {
1052 /* this is a scheme as described by RFC3986, par. 3.1 */
1053 ptr++;
1054 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1055 ptr++;
1056 /* skip '://' */
1057 if (*ptr == '\0' || *ptr++ != ':')
1058 return NULL;
1059 if (*ptr == '\0' || *ptr++ != '/')
1060 return NULL;
1061 if (*ptr == '\0' || *ptr++ != '/')
1062 return NULL;
1063 }
1064 /* skip [user[:passwd]@]host[:[port]] */
1065
1066 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1067 ptr++;
1068
1069 if (*ptr == '\0' || *ptr == ' ')
1070 return NULL;
1071
1072 /* OK, we got the '/' ! */
1073 return ptr;
1074}
1075
Willy Tarreau71241ab2012-12-27 11:30:54 +01001076/* Returns a 302 for a redirectable request that reaches a server working in
1077 * in redirect mode. This may only be called just after the stream interface
1078 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1079 * follow normal proxy processing. NOTE: this function is designed to support
1080 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001081 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001082void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001083{
1084 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001085 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001086 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001087 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001088
1089 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001090 trash.len = strlen(HTTP_302);
1091 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001092
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001093 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001094
Willy Tarreauefb453c2008-10-26 20:49:47 +01001095 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001096 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001097 return;
1098
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001099 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001100 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001101 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1102 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001103 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001104
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001105 /* 3: add the request URI. Since it was already forwarded, we need
1106 * to temporarily rewind the buffer.
1107 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001108 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001109 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001110
Willy Tarreauefb453c2008-10-26 20:49:47 +01001111 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001112 len = buffer_count(s->req.buf, path, b_ptr(s->req.buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001113
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001114 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001115
Willy Tarreauefb453c2008-10-26 20:49:47 +01001116 if (!path)
1117 return;
1118
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001119 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001120 return;
1121
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001122 memcpy(trash.str + trash.len, path, len);
1123 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001124
1125 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001126 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1127 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001128 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001129 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1130 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001131 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001132
1133 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001134 si_shutr(si);
1135 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001136 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001137 si->state = SI_ST_CLO;
1138
1139 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001140 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001141
1142 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001143 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001144 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001145}
1146
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001147/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001148 * that the server side is closed. Note that err_type is actually a
1149 * bitmask, where almost only aborts may be cumulated with other
1150 * values. We consider that aborted operations are more important
1151 * than timeouts or errors due to the fact that nobody else in the
1152 * logs might explain incomplete retries. All others should avoid
1153 * being cumulated. It should normally not be possible to have multiple
1154 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001155 * Note that connection errors appearing on the second request of a keep-alive
1156 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001157 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001158void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001159{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001160 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001161
1162 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001163 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001164 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001165 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001166 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001167 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001168 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001169 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001170 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001171 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001172 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001173 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001174 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001175 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001176 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001177 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001178 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001179 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001180 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1181 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001182 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001183 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001184 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001185 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001186 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001187 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001188 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001189 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001190}
1191
Willy Tarreau42250582007-04-01 01:30:43 +02001192extern const char sess_term_cond[8];
1193extern const char sess_fin_state[8];
1194extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001195struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001196struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001197struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001198struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001199
Willy Tarreau117f59e2007-03-04 18:17:17 +01001200/*
1201 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001202 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001203 */
1204void capture_headers(char *som, struct hdr_idx *idx,
1205 char **cap, struct cap_hdr *cap_hdr)
1206{
1207 char *eol, *sol, *col, *sov;
1208 int cur_idx;
1209 struct cap_hdr *h;
1210 int len;
1211
1212 sol = som + hdr_idx_first_pos(idx);
1213 cur_idx = hdr_idx_first_idx(idx);
1214
1215 while (cur_idx) {
1216 eol = sol + idx->v[cur_idx].len;
1217
1218 col = sol;
1219 while (col < eol && *col != ':')
1220 col++;
1221
1222 sov = col + 1;
1223 while (sov < eol && http_is_lws[(unsigned char)*sov])
1224 sov++;
1225
1226 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001227 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001228 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1229 if (cap[h->index] == NULL)
1230 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001231 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001232
1233 if (cap[h->index] == NULL) {
1234 Alert("HTTP capture : out of memory.\n");
1235 continue;
1236 }
1237
1238 len = eol - sov;
1239 if (len > h->len)
1240 len = h->len;
1241
1242 memcpy(cap[h->index], sov, len);
1243 cap[h->index][len]=0;
1244 }
1245 }
1246 sol = eol + idx->v[cur_idx].cr + 1;
1247 cur_idx = idx->v[cur_idx].next;
1248 }
1249}
1250
1251
Willy Tarreau42250582007-04-01 01:30:43 +02001252/* either we find an LF at <ptr> or we jump to <bad>.
1253 */
1254#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1255
1256/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1257 * otherwise to <http_msg_ood> with <state> set to <st>.
1258 */
1259#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1260 ptr++; \
1261 if (likely(ptr < end)) \
1262 goto good; \
1263 else { \
1264 state = (st); \
1265 goto http_msg_ood; \
1266 } \
1267 } while (0)
1268
1269
Willy Tarreaubaaee002006-06-26 02:48:02 +02001270/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001271 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001272 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1273 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1274 * will give undefined results.
1275 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1276 * and that msg->sol points to the beginning of the response.
1277 * If a complete line is found (which implies that at least one CR or LF is
1278 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1279 * returned indicating an incomplete line (which does not mean that parts have
1280 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1281 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1282 * upon next call.
1283 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001284 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001285 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1286 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001287 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001288 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001289const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001290 enum ht_state state, const char *ptr, const char *end,
1291 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001292{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001293 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001294
Willy Tarreau8973c702007-01-21 23:58:29 +01001295 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001296 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001297 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001298 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001299 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1300
1301 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001302 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001303 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1304 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001305 state = HTTP_MSG_ERROR;
1306 break;
1307
Willy Tarreau8973c702007-01-21 23:58:29 +01001308 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001309 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001310 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001311 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001312 goto http_msg_rpcode;
1313 }
1314 if (likely(HTTP_IS_SPHT(*ptr)))
1315 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1316 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001317 state = HTTP_MSG_ERROR;
1318 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001319
Willy Tarreau8973c702007-01-21 23:58:29 +01001320 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001321 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001322 if (likely(!HTTP_IS_LWS(*ptr)))
1323 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1324
1325 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001326 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001327 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1328 }
1329
1330 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001331 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001332 http_msg_rsp_reason:
1333 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001334 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001335 msg->sl.st.r_l = 0;
1336 goto http_msg_rpline_eol;
1337
Willy Tarreau8973c702007-01-21 23:58:29 +01001338 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001339 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001340 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001341 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001342 goto http_msg_rpreason;
1343 }
1344 if (likely(HTTP_IS_SPHT(*ptr)))
1345 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1346 /* so it's a CR/LF, so there is no reason phrase */
1347 goto http_msg_rsp_reason;
1348
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001350 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001351 if (likely(!HTTP_IS_CRLF(*ptr)))
1352 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001353 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001354 http_msg_rpline_eol:
1355 /* We have seen the end of line. Note that we do not
1356 * necessarily have the \n yet, but at least we know that we
1357 * have EITHER \r OR \n, otherwise the response would not be
1358 * complete. We can then record the response length and return
1359 * to the caller which will be able to register it.
1360 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001361 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001362 return ptr;
1363
Willy Tarreau8973c702007-01-21 23:58:29 +01001364 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001365#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001366 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1367 exit(1);
1368#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001369 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001370 }
1371
1372 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001373 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001374 if (ret_state)
1375 *ret_state = state;
1376 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001377 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001378 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001379}
1380
Willy Tarreau8973c702007-01-21 23:58:29 +01001381/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001382 * This function parses a request line between <ptr> and <end>, starting with
1383 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1384 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1385 * will give undefined results.
1386 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1387 * and that msg->sol points to the beginning of the request.
1388 * If a complete line is found (which implies that at least one CR or LF is
1389 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1390 * returned indicating an incomplete line (which does not mean that parts have
1391 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1392 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1393 * upon next call.
1394 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001395 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1397 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001398 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001399 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001400const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001401 enum ht_state state, const char *ptr, const char *end,
1402 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001403{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001404 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001405
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001408 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 if (likely(HTTP_IS_TOKEN(*ptr)))
1410 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001411
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001413 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1415 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001416
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 if (likely(HTTP_IS_CRLF(*ptr))) {
1418 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001419 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001421 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001423 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001425 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 msg->sl.rq.v_l = 0;
1427 goto http_msg_rqline_eol;
1428 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001429 state = HTTP_MSG_ERROR;
1430 break;
1431
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001433 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001435 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001436 goto http_msg_rquri;
1437 }
1438 if (likely(HTTP_IS_SPHT(*ptr)))
1439 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1440 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1441 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001442
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001444 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001445 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001446 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001449 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1451 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001452
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001453 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001454 /* non-ASCII chars are forbidden unless option
1455 * accept-invalid-http-request is enabled in the frontend.
1456 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001457 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001458 if (msg->err_pos < -1)
1459 goto invalid_char;
1460 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001461 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001462 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1463 }
1464
1465 if (likely(HTTP_IS_CRLF(*ptr))) {
1466 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1467 goto http_msg_req09_uri_e;
1468 }
1469
1470 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001471 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001472 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001473 state = HTTP_MSG_ERROR;
1474 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001477 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001479 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001480 goto http_msg_rqver;
1481 }
1482 if (likely(HTTP_IS_SPHT(*ptr)))
1483 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1484 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1485 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001488 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001489 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001491
1492 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001493 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001494 http_msg_rqline_eol:
1495 /* We have seen the end of line. Note that we do not
1496 * necessarily have the \n yet, but at least we know that we
1497 * have EITHER \r OR \n, otherwise the request would not be
1498 * complete. We can then record the request length and return
1499 * to the caller which will be able to register it.
1500 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001501 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001502 return ptr;
1503 }
1504
1505 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001506 state = HTTP_MSG_ERROR;
1507 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001508
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001510#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1512 exit(1);
1513#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001514 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001515 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001516
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001518 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 if (ret_state)
1520 *ret_state = state;
1521 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001522 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001525
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001526/*
1527 * Returns the data from Authorization header. Function may be called more
1528 * than once so data is stored in txn->auth_data. When no header is found
1529 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001530 * searching again for something we are unable to find anyway. However, if
1531 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001532 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001533 */
1534
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001535/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1536 * set according to global.tune.bufsize.
1537 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001538char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001539
1540int
Willy Tarreau87b09662015-04-03 00:22:06 +02001541get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001542{
1543
Willy Tarreaueee5b512015-04-03 23:46:31 +02001544 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001545 struct chunk auth_method;
1546 struct hdr_ctx ctx;
1547 char *h, *p;
1548 int len;
1549
1550#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001551 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001552#endif
1553
1554 if (txn->auth.method == HTTP_AUTH_WRONG)
1555 return 0;
1556
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001557 txn->auth.method = HTTP_AUTH_WRONG;
1558
1559 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001560
1561 if (txn->flags & TX_USE_PX_CONN) {
1562 h = "Proxy-Authorization";
1563 len = strlen(h);
1564 } else {
1565 h = "Authorization";
1566 len = strlen(h);
1567 }
1568
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001569 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001570 return 0;
1571
1572 h = ctx.line + ctx.val;
1573
1574 p = memchr(h, ' ', ctx.vlen);
1575 if (!p || p == h)
1576 return 0;
1577
1578 chunk_initlen(&auth_method, h, 0, p-h);
1579 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1580
1581 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1582
1583 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001584 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001585
1586 if (len < 0)
1587 return 0;
1588
1589
1590 get_http_auth_buff[len] = '\0';
1591
1592 p = strchr(get_http_auth_buff, ':');
1593
1594 if (!p)
1595 return 0;
1596
1597 txn->auth.user = get_http_auth_buff;
1598 *p = '\0';
1599 txn->auth.pass = p+1;
1600
1601 txn->auth.method = HTTP_AUTH_BASIC;
1602 return 1;
1603 }
1604
1605 return 0;
1606}
1607
Willy Tarreau58f10d72006-12-04 02:26:12 +01001608
Willy Tarreau8973c702007-01-21 23:58:29 +01001609/*
1610 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001611 * depending on the initial msg->msg_state. The caller is responsible for
1612 * ensuring that the message does not wrap. The function can be preempted
1613 * everywhere when data are missing and recalled at the exact same location
1614 * with no information loss. The message may even be realigned between two
1615 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001616 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001617 * fields. Note that msg->sol will be initialized after completing the first
1618 * state, so that none of the msg pointers has to be initialized prior to the
1619 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001620 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001621void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001622{
Willy Tarreau3770f232013-12-07 00:01:53 +01001623 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001624 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001625 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001626
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001627 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001628 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001629 ptr = buf->p + msg->next;
1630 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001631
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 if (unlikely(ptr >= end))
1633 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001634
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001635 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001636 /*
1637 * First, states that are specific to the response only.
1638 * We check them first so that request and headers are
1639 * closer to each other (accessed more often).
1640 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001641 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001642 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001643 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001644 /* we have a start of message, but we have to check
1645 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001646 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001647 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001648 if (unlikely(ptr != buf->p)) {
1649 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001650 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001651 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001652 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001653 }
Willy Tarreau26927362012-05-18 23:22:52 +02001654 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001655 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001656 hdr_idx_init(idx);
1657 state = HTTP_MSG_RPVER;
1658 goto http_msg_rpver;
1659 }
1660
1661 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1662 goto http_msg_invalid;
1663
1664 if (unlikely(*ptr == '\n'))
1665 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1666 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1667 /* stop here */
1668
Willy Tarreau8973c702007-01-21 23:58:29 +01001669 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001670 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001671 EXPECT_LF_HERE(ptr, http_msg_invalid);
1672 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1673 /* stop here */
1674
Willy Tarreau8973c702007-01-21 23:58:29 +01001675 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001676 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001677 case HTTP_MSG_RPVER_SP:
1678 case HTTP_MSG_RPCODE:
1679 case HTTP_MSG_RPCODE_SP:
1680 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001681 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001682 state, ptr, end,
1683 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001684 if (unlikely(!ptr))
1685 return;
1686
1687 /* we have a full response and we know that we have either a CR
1688 * or an LF at <ptr>.
1689 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001690 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1691
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001692 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001693 if (likely(*ptr == '\r'))
1694 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1695 goto http_msg_rpline_end;
1696
Willy Tarreau8973c702007-01-21 23:58:29 +01001697 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001698 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001699 /* msg->sol must point to the first of CR or LF. */
1700 EXPECT_LF_HERE(ptr, http_msg_invalid);
1701 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1702 /* stop here */
1703
1704 /*
1705 * Second, states that are specific to the request only
1706 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001707 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001708 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001710 /* we have a start of message, but we have to check
1711 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001712 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001713 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001714 if (likely(ptr != buf->p)) {
1715 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001716 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001717 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001718 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719 }
Willy Tarreau26927362012-05-18 23:22:52 +02001720 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001721 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001722 state = HTTP_MSG_RQMETH;
1723 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001725
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001726 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1727 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001728
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 if (unlikely(*ptr == '\n'))
1730 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1731 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001732 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001733
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001735 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001736 EXPECT_LF_HERE(ptr, http_msg_invalid);
1737 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001738 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001739
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001741 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 case HTTP_MSG_RQMETH_SP:
1743 case HTTP_MSG_RQURI:
1744 case HTTP_MSG_RQURI_SP:
1745 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001746 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001747 state, ptr, end,
1748 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001749 if (unlikely(!ptr))
1750 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001751
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001752 /* we have a full request and we know that we have either a CR
1753 * or an LF at <ptr>.
1754 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001755 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001756
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001757 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 if (likely(*ptr == '\r'))
1759 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001760 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001761
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001763 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001764 /* check for HTTP/0.9 request : no version information available.
1765 * msg->sol must point to the first of CR or LF.
1766 */
1767 if (unlikely(msg->sl.rq.v_l == 0))
1768 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001769
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001770 EXPECT_LF_HERE(ptr, http_msg_invalid);
1771 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001772 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001773
Willy Tarreau8973c702007-01-21 23:58:29 +01001774 /*
1775 * Common states below
1776 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001777 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001778 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001779 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001780 if (likely(!HTTP_IS_CRLF(*ptr))) {
1781 goto http_msg_hdr_name;
1782 }
1783
1784 if (likely(*ptr == '\r'))
1785 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1786 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001787
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001788 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001789 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 /* assumes msg->sol points to the first char */
1791 if (likely(HTTP_IS_TOKEN(*ptr)))
1792 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001793
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001794 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001795 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001796
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001797 if (likely(msg->err_pos < -1) || *ptr == '\n')
1798 goto http_msg_invalid;
1799
1800 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001801 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001802
1803 /* and we still accept this non-token character */
1804 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001805
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001807 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001808 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001809 if (likely(HTTP_IS_SPHT(*ptr)))
1810 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001811
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001812 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001813 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001814
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001815 if (likely(!HTTP_IS_CRLF(*ptr))) {
1816 goto http_msg_hdr_val;
1817 }
1818
1819 if (likely(*ptr == '\r'))
1820 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1821 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001822
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001823 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001824 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001825 EXPECT_LF_HERE(ptr, http_msg_invalid);
1826 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001827
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001828 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001829 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001830 if (likely(HTTP_IS_SPHT(*ptr))) {
1831 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001832 for (; buf->p + msg->sov < ptr; msg->sov++)
1833 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001834 goto http_msg_hdr_l1_sp;
1835 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001836 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001837 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001838 goto http_msg_complete_header;
1839
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001840 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001841 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001842 /* assumes msg->sol points to the first char, and msg->sov
1843 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001844 */
1845 if (likely(!HTTP_IS_CRLF(*ptr)))
1846 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001847
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001848 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001849 /* Note: we could also copy eol into ->eoh so that we have the
1850 * real header end in case it ends with lots of LWS, but is this
1851 * really needed ?
1852 */
1853 if (likely(*ptr == '\r'))
1854 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1855 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001856
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001857 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001858 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001859 EXPECT_LF_HERE(ptr, http_msg_invalid);
1860 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001861
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001862 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001863 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001864 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1865 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001866 for (; buf->p + msg->eol < ptr; msg->eol++)
1867 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001868 goto http_msg_hdr_val;
1869 }
1870 http_msg_complete_header:
1871 /*
1872 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001873 * Assumes msg->sol points to the first char, msg->sov points
1874 * to the first character of the value and msg->eol to the
1875 * first CR or LF so we know how the line ends. We insert last
1876 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001877 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001878 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001879 idx, idx->tail) < 0))
1880 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001881
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001882 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 if (likely(!HTTP_IS_CRLF(*ptr))) {
1884 goto http_msg_hdr_name;
1885 }
1886
1887 if (likely(*ptr == '\r'))
1888 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1889 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001890
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001891 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001892 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001893 /* Assumes msg->sol points to the first of either CR or LF.
1894 * Sets ->sov and ->next to the total header length, ->eoh to
1895 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1896 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001897 EXPECT_LF_HERE(ptr, http_msg_invalid);
1898 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001899 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001900 msg->eoh = msg->sol;
1901 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001902 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001903 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001904 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001905
1906 case HTTP_MSG_ERROR:
1907 /* this may only happen if we call http_msg_analyser() twice with an error */
1908 break;
1909
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001910 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001911#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001912 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1913 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001914#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001915 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001916 }
1917 http_msg_ood:
1918 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001919 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001920 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001921 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001922
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001923 http_msg_invalid:
1924 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001925 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001926 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001927 return;
1928}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001929
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001930/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1931 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1932 * nothing is done and 1 is returned.
1933 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001934static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001935{
1936 int delta;
1937 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001938 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001939
1940 if (msg->sl.rq.v_l != 0)
1941 return 1;
1942
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001943 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1944 if (txn->meth != HTTP_METH_GET)
1945 return 0;
1946
Willy Tarreau9b28e032012-10-12 23:49:43 +02001947 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001948 delta = 0;
1949
1950 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001951 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1952 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001953 }
1954 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001955 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001956 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001957 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001958 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001959 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001960 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001961 NULL, NULL);
1962 if (unlikely(!cur_end))
1963 return 0;
1964
1965 /* we have a full HTTP/1.0 request now and we know that
1966 * we have either a CR or an LF at <ptr>.
1967 */
1968 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1969 return 1;
1970}
1971
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001972/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001973 * and "keep-alive" values. If we already know that some headers may safely
1974 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001975 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1976 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001977 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001978 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1979 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1980 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001981 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001982 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001983void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001984{
Willy Tarreau5b154472009-12-21 20:11:07 +01001985 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001986 const char *hdr_val = "Connection";
1987 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001988
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001989 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001990 return;
1991
Willy Tarreau88d349d2010-01-25 12:15:43 +01001992 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1993 hdr_val = "Proxy-Connection";
1994 hdr_len = 16;
1995 }
1996
Willy Tarreau5b154472009-12-21 20:11:07 +01001997 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001998 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001999 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002000 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2001 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002002 if (to_del & 2)
2003 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002004 else
2005 txn->flags |= TX_CON_KAL_SET;
2006 }
2007 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2008 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002009 if (to_del & 1)
2010 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002011 else
2012 txn->flags |= TX_CON_CLO_SET;
2013 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002014 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2015 txn->flags |= TX_HDR_CONN_UPG;
2016 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002017 }
2018
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002019 txn->flags |= TX_HDR_CONN_PRS;
2020 return;
2021}
Willy Tarreau5b154472009-12-21 20:11:07 +01002022
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002023/* Apply desired changes on the Connection: header. Values may be removed and/or
2024 * added depending on the <wanted> flags, which are exclusively composed of
2025 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2026 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2027 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002028void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002029{
2030 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002031 const char *hdr_val = "Connection";
2032 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002033
2034 ctx.idx = 0;
2035
Willy Tarreau88d349d2010-01-25 12:15:43 +01002036
2037 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2038 hdr_val = "Proxy-Connection";
2039 hdr_len = 16;
2040 }
2041
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002042 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002043 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002044 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2045 if (wanted & TX_CON_KAL_SET)
2046 txn->flags |= TX_CON_KAL_SET;
2047 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002048 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002049 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002050 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2051 if (wanted & TX_CON_CLO_SET)
2052 txn->flags |= TX_CON_CLO_SET;
2053 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002054 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002055 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002056 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002057
2058 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2059 return;
2060
2061 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2062 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002063 hdr_val = "Connection: close";
2064 hdr_len = 17;
2065 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2066 hdr_val = "Proxy-Connection: close";
2067 hdr_len = 23;
2068 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002069 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002070 }
2071
2072 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2073 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002074 hdr_val = "Connection: keep-alive";
2075 hdr_len = 22;
2076 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2077 hdr_val = "Proxy-Connection: keep-alive";
2078 hdr_len = 28;
2079 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002080 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002081 }
2082 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002083}
2084
Willy Tarreauc24715e2014-04-17 15:21:20 +02002085/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
2086 * the first byte of data after the chunk size, so that we know we can forward
2087 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
2088 * the chunk size. That way it is always possible to differentiate between the
2089 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01002090 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002091 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002092 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002093static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002094{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002095 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002096 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002097 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002098 const char *end = buf->data + buf->size;
2099 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002100 unsigned int chunk = 0;
2101
2102 /* The chunk size is in the following form, though we are only
2103 * interested in the size and CRLF :
2104 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2105 */
2106 while (1) {
2107 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002108 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002109 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002110 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002111 if (c < 0) /* not a hex digit anymore */
2112 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002113 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002114 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002115 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002116 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002117 chunk = (chunk << 4) + c;
2118 }
2119
Willy Tarreaud98cf932009-12-27 22:54:55 +01002120 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002121 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002122 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002123
2124 while (http_is_spht[(unsigned char)*ptr]) {
2125 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002126 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002127 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002128 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002129 }
2130
Willy Tarreaud98cf932009-12-27 22:54:55 +01002131 /* Up to there, we know that at least one byte is present at *ptr. Check
2132 * for the end of chunk size.
2133 */
2134 while (1) {
2135 if (likely(HTTP_IS_CRLF(*ptr))) {
2136 /* we now have a CR or an LF at ptr */
2137 if (likely(*ptr == '\r')) {
2138 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002139 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002140 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002141 return 0;
2142 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002143
Willy Tarreaud98cf932009-12-27 22:54:55 +01002144 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002145 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002146 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002147 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002148 /* done */
2149 break;
2150 }
2151 else if (*ptr == ';') {
2152 /* chunk extension, ends at next CRLF */
2153 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002154 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002155 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002156 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002157
2158 while (!HTTP_IS_CRLF(*ptr)) {
2159 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002160 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002161 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002162 return 0;
2163 }
2164 /* we have a CRLF now, loop above */
2165 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002166 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002167 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002168 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002169 }
2170
Willy Tarreaud98cf932009-12-27 22:54:55 +01002171 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002172 * which may or may not be present. We save that into ->next,
2173 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002174 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002175 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002176 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002177 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002178 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002179 msg->chunk_len = chunk;
2180 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002181 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002182 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002183 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002184 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002185 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002186}
2187
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002188/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002189 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002190 * the trailers is found, it is automatically scheduled to be forwarded,
2191 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2192 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002193 * except maybe msg->next if it could parse some lines, and returns zero.
2194 * If a parse error is encountered, the function returns < 0 and does not
2195 * change anything except maybe msg->next. Note that the message must
2196 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002197 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002198 * forwarding, and that msg->next exactly matches the length of trailers
2199 * already parsed and not forwarded. It is also important to note that this
2200 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002201 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002202static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002203{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002204 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002205
Willy Tarreaua458b672012-03-05 11:17:50 +01002206 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002207 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002208 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002209 const char *ptr = b_ptr(buf, msg->next);
2210 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002211 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002212
2213 /* scan current line and stop at LF or CRLF */
2214 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002215 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002216 return 0;
2217
2218 if (*ptr == '\n') {
2219 if (!p1)
2220 p1 = ptr;
2221 p2 = ptr;
2222 break;
2223 }
2224
2225 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002226 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002227 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002228 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002229 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002230 p1 = ptr;
2231 }
2232
2233 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002234 if (ptr >= buf->data + buf->size)
2235 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002236 }
2237
2238 /* after LF; point to beginning of next line */
2239 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002240 if (p2 >= buf->data + buf->size)
2241 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002242
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002243 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002244 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002245 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002246
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002247 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002248 /* LF/CRLF at beginning of line => end of trailers at p2.
2249 * Everything was scheduled for forwarding, there's nothing
2250 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002251 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002252 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002253 msg->msg_state = HTTP_MSG_DONE;
2254 return 1;
2255 }
2256 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002257 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002258 }
2259}
2260
Willy Tarreauc24715e2014-04-17 15:21:20 +02002261/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2262 * or a possible LF alone at the end of a chunk. It automatically adjusts
2263 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002264 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002265 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2266 * not enough data are available, the function does not change anything and
2267 * returns zero. If a parse error is encountered, the function returns < 0 and
2268 * does not change anything. Note: this function is designed to parse wrapped
2269 * CRLF at the end of the buffer.
2270 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002271static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002272{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002273 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002274 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002275 int bytes;
2276
2277 /* NB: we'll check data availabilty at the end. It's not a
2278 * problem because whatever we match first will be checked
2279 * against the correct length.
2280 */
2281 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002282 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002283 if (*ptr == '\r') {
2284 bytes++;
2285 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002286 if (ptr >= buf->data + buf->size)
2287 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002288 }
2289
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002290 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002291 return 0;
2292
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002293 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002294 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002295 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002296 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002297
2298 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002299 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002300 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002301 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002302 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002303 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2304 return 1;
2305}
Willy Tarreau5b154472009-12-21 20:11:07 +01002306
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002307/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2308 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2309 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2310 * Unparsable qvalues return 1000 as "q=1.000".
2311 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002312int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002313{
2314 int q = 1000;
2315
Willy Tarreau506c69a2014-07-08 00:59:48 +02002316 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002317 goto out;
2318 q = (*qvalue++ - '0') * 1000;
2319
2320 if (*qvalue++ != '.')
2321 goto out;
2322
Willy Tarreau506c69a2014-07-08 00:59:48 +02002323 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002324 goto out;
2325 q += (*qvalue++ - '0') * 100;
2326
Willy Tarreau506c69a2014-07-08 00:59:48 +02002327 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002328 goto out;
2329 q += (*qvalue++ - '0') * 10;
2330
Willy Tarreau506c69a2014-07-08 00:59:48 +02002331 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002332 goto out;
2333 q += (*qvalue++ - '0') * 1;
2334 out:
2335 if (q > 1000)
2336 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002337 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002338 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002339 return q;
2340}
William Lallemand82fe75c2012-10-23 10:25:10 +02002341
2342/*
2343 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002344 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002345int select_compression_request_header(struct stream *s, struct buffer *req)
William Lallemand82fe75c2012-10-23 10:25:10 +02002346{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002347 struct http_txn *txn = s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002348 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002349 struct hdr_ctx ctx;
2350 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002351 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002352
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002353 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2354 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002355 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2356 */
2357 ctx.idx = 0;
2358 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2359 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002360 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2361 (ctx.vlen < 31 ||
2362 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2363 ctx.line[ctx.val + 30] < '6' ||
2364 (ctx.line[ctx.val + 30] == '6' &&
2365 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2366 s->comp_algo = NULL;
2367 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002368 }
2369
William Lallemand82fe75c2012-10-23 10:25:10 +02002370 /* search for the algo in the backend in priority or the frontend */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002371 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002372 int best_q = 0;
2373
William Lallemand82fe75c2012-10-23 10:25:10 +02002374 ctx.idx = 0;
2375 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002376 const char *qval;
2377 int q;
2378 int toklen;
2379
2380 /* try to isolate the token from the optional q-value */
2381 toklen = 0;
2382 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2383 toklen++;
2384
2385 qval = ctx.line + ctx.val + toklen;
2386 while (1) {
2387 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2388 qval++;
2389
2390 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2391 qval = NULL;
2392 break;
2393 }
2394 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002395
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002396 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2397 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002398
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002399 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2400 qval = NULL;
2401 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002402 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002403 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2404 break;
2405
2406 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2407 qval++;
2408 }
2409
2410 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002411 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002412
2413 if (q <= best_q)
2414 continue;
2415
2416 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2417 if (*(ctx.line + ctx.val) == '*' ||
Willy Tarreau615105e2015-03-28 16:40:46 +01002418 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002419 s->comp_algo = comp_algo;
2420 best_q = q;
2421 break;
2422 }
2423 }
2424 }
2425 }
2426
2427 /* remove all occurrences of the header when "compression offload" is set */
2428 if (s->comp_algo) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002429 if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002430 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2431 ctx.idx = 0;
2432 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2433 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002434 }
2435 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002436 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002437 }
2438
2439 /* identity is implicit does not require headers */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002440 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002441 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
Willy Tarreau615105e2015-03-28 16:40:46 +01002442 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002443 s->comp_algo = comp_algo;
2444 return 1;
2445 }
2446 }
2447 }
2448
2449 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002450 return 0;
2451}
2452
2453/*
2454 * Selects a comression algorithm depending of the server response.
2455 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002456int select_compression_response_header(struct stream *s, struct buffer *res)
William Lallemand82fe75c2012-10-23 10:25:10 +02002457{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002458 struct http_txn *txn = s->txn;
William Lallemand82fe75c2012-10-23 10:25:10 +02002459 struct http_msg *msg = &txn->rsp;
2460 struct hdr_ctx ctx;
2461 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002462
2463 /* no common compression algorithm was found in request header */
2464 if (s->comp_algo == NULL)
2465 goto fail;
2466
2467 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002468 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002469 goto fail;
2470
Jesse Hathaway2468d4e2015-03-06 20:16:15 +00002471 /* compress 200,201,202,203 responses only */
2472 if ((txn->status != 200) &&
2473 (txn->status != 201) &&
2474 (txn->status != 202) &&
2475 (txn->status != 203))
William Lallemandd3002612012-11-26 14:34:47 +01002476 goto fail;
2477
William Lallemand82fe75c2012-10-23 10:25:10 +02002478 /* Content-Length is null */
2479 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2480 goto fail;
2481
2482 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002483 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002484 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2485 goto fail;
2486
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002487 /* no compression when Cache-Control: no-transform is present in the message */
2488 ctx.idx = 0;
2489 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2490 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2491 goto fail;
2492 }
2493
William Lallemand82fe75c2012-10-23 10:25:10 +02002494 comp_type = NULL;
2495
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002496 /* we don't want to compress multipart content-types, nor content-types that are
2497 * not listed in the "compression type" directive if any. If no content-type was
2498 * found but configuration requires one, we don't compress either. Backend has
2499 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002500 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002501 ctx.idx = 0;
2502 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2503 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2504 goto fail;
2505
2506 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002507 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002508 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002509 if (ctx.vlen >= comp_type->name_len &&
2510 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002511 /* this Content-Type should be compressed */
2512 break;
2513 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002514 /* this Content-Type should not be compressed */
2515 if (comp_type == NULL)
2516 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002517 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002518 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002519 else { /* no content-type header */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002520 if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->comp->types))
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002521 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002522 }
2523
William Lallemandd85f9172012-11-09 17:05:39 +01002524 /* limit compression rate */
2525 if (global.comp_rate_lim > 0)
2526 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2527 goto fail;
2528
William Lallemand072a2bf2012-11-20 17:01:01 +01002529 /* limit cpu usage */
2530 if (idle_pct < compress_min_idle)
2531 goto fail;
2532
William Lallemand4c49fae2012-11-07 15:00:23 +01002533 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002534 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002535 goto fail;
2536
Willy Tarreaue7dff022015-04-03 01:14:29 +02002537 s->flags |= SF_COMP_READY;
William Lallemandec3e3892012-11-12 17:02:18 +01002538
William Lallemand82fe75c2012-10-23 10:25:10 +02002539 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002540 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002541 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2542 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2543
2544 /* add Transfer-Encoding header */
2545 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2546 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2547
2548 /*
2549 * Add Content-Encoding header when it's not identity encoding.
2550 * RFC 2616 : Identity encoding: This content-coding is used only in the
2551 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2552 * header.
2553 */
Willy Tarreau615105e2015-03-28 16:40:46 +01002554 if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002555 trash.len = 18;
2556 memcpy(trash.str, "Content-Encoding: ", trash.len);
Willy Tarreau615105e2015-03-28 16:40:46 +01002557 memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len);
2558 trash.len += s->comp_algo->ua_name_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002559 trash.str[trash.len] = '\0';
2560 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002561 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002562 return 1;
2563
2564fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002565 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002566 return 0;
2567}
2568
Willy Tarreau87b09662015-04-03 00:22:06 +02002569void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002570{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002571 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002572 int tmp = TX_CON_WANT_KAL;
2573
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002574 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2575 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002576 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2577 tmp = TX_CON_WANT_TUN;
2578
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002579 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002580 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2581 tmp = TX_CON_WANT_TUN;
2582 }
2583
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002584 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002585 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2586 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002587 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002588 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2589 tmp = TX_CON_WANT_CLO;
2590 else
2591 tmp = TX_CON_WANT_SCL;
2592 }
2593
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002594 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002595 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2596 tmp = TX_CON_WANT_CLO;
2597
2598 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2599 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2600
2601 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2602 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2603 /* parse the Connection header and possibly clean it */
2604 int to_del = 0;
2605 if ((msg->flags & HTTP_MSGF_VER_11) ||
2606 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002607 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002608 to_del |= 2; /* remove "keep-alive" */
2609 if (!(msg->flags & HTTP_MSGF_VER_11))
2610 to_del |= 1; /* remove "close" */
2611 http_parse_connection_header(txn, msg, to_del);
2612 }
2613
2614 /* check if client or config asks for explicit close in KAL/SCL */
2615 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2616 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2617 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2618 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2619 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002620 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002621 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2622}
William Lallemand82fe75c2012-10-23 10:25:10 +02002623
Willy Tarreaud787e662009-07-07 10:14:51 +02002624/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2625 * processing can continue on next analysers, or zero if it either needs more
2626 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002627 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002628 * when it has nothing left to do, and may remove any analyser when it wants to
2629 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002630 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002631int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002632{
Willy Tarreau59234e92008-11-30 23:51:27 +01002633 /*
2634 * We will parse the partial (or complete) lines.
2635 * We will check the request syntax, and also join multi-line
2636 * headers. An index of all the lines will be elaborated while
2637 * parsing.
2638 *
2639 * For the parsing, we use a 28 states FSM.
2640 *
2641 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002642 * req->buf->p = beginning of request
2643 * req->buf->p + msg->eoh = end of processed headers / start of current one
2644 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002645 * msg->eol = end of current header or line (LF or CRLF)
2646 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002647 *
2648 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002649 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002650 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2651 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002652 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002653
Willy Tarreau59234e92008-11-30 23:51:27 +01002654 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002655 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002656 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002658 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002659
Willy Tarreau87b09662015-04-03 00:22:06 +02002660 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002661 now_ms, __FUNCTION__,
2662 s,
2663 req,
2664 req->rex, req->wex,
2665 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002666 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002667 req->analysers);
2668
Willy Tarreau52a0c602009-08-16 22:45:38 +02002669 /* we're speaking HTTP here, so let's speak HTTP to the client */
2670 s->srv_error = http_return_srv_error;
2671
Willy Tarreau83e3af02009-12-28 17:39:57 +01002672 /* There's a protected area at the end of the buffer for rewriting
2673 * purposes. We don't want to start to parse the request if the
2674 * protected area is affected, because we may have to move processed
2675 * data later, which is much more complicated.
2676 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002677 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002678 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002679 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002680 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002681 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002682 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002683 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002684 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002685 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002686 return 0;
2687 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002688 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2689 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2690 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002691 }
2692
Willy Tarreau065e8332010-01-08 00:30:20 +01002693 /* Note that we have the same problem with the response ; we
2694 * may want to send a redirect, error or anything which requires
2695 * some spare space. So we'll ensure that we have at least
2696 * maxrewrite bytes available in the response buffer before
2697 * processing that one. This will only affect pipelined
2698 * keep-alive requests.
2699 */
2700 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002701 unlikely(!channel_is_rewritable(&s->res) ||
2702 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2703 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2704 if (s->res.buf->o) {
2705 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002706 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002707 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002708 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002709 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2710 s->res.flags |= CF_WAKE_WRITE;
2711 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002712 return 0;
2713 }
2714 }
2715
Willy Tarreau9b28e032012-10-12 23:49:43 +02002716 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002717 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002718 }
2719
Willy Tarreau59234e92008-11-30 23:51:27 +01002720 /* 1: we might have to print this header in debug mode */
2721 if (unlikely((global.mode & MODE_DEBUG) &&
2722 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002723 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002724 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002725
Willy Tarreau9b28e032012-10-12 23:49:43 +02002726 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002727 /* this is a bit complex : in case of error on the request line,
2728 * we know that rq.l is still zero, so we display only the part
2729 * up to the end of the line (truncated by debug_hdr).
2730 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002731 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002732 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002733
Willy Tarreau59234e92008-11-30 23:51:27 +01002734 sol += hdr_idx_first_pos(&txn->hdr_idx);
2735 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002736
Willy Tarreau59234e92008-11-30 23:51:27 +01002737 while (cur_idx) {
2738 eol = sol + txn->hdr_idx.v[cur_idx].len;
2739 debug_hdr("clihdr", s, sol, eol);
2740 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2741 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002742 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002743 }
2744
Willy Tarreau58f10d72006-12-04 02:26:12 +01002745
Willy Tarreau59234e92008-11-30 23:51:27 +01002746 /*
2747 * Now we quickly check if we have found a full valid request.
2748 * If not so, we check the FD and buffer states before leaving.
2749 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002750 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002751 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002752 * on a keep-alive stream, if we encounter and error, close, t/o,
2753 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002754 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002755 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002756 * Last, we may increase some tracked counters' http request errors on
2757 * the cases that are deliberately the client's fault. For instance,
2758 * a timeout or connection reset is not counted as an error. However
2759 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002760 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002761
Willy Tarreau655dce92009-11-08 13:10:58 +01002762 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002763 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002764 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002765 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002766 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002767 stream_inc_http_req_ctr(s);
2768 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002769 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002770 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002771 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002772
Willy Tarreau59234e92008-11-30 23:51:27 +01002773 /* 1: Since we are in header mode, if there's no space
2774 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002775 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002776 * must terminate it now.
2777 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002778 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002779 /* FIXME: check if URI is set and return Status
2780 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002781 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002782 stream_inc_http_req_ctr(s);
2783 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002784 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002785 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002786 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002787 goto return_bad_req;
2788 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002789
Willy Tarreau59234e92008-11-30 23:51:27 +01002790 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002791 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002792 if (!(s->flags & SF_ERR_MASK))
2793 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002794
Willy Tarreaufcffa692010-01-10 14:21:19 +01002795 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002796 goto failed_keep_alive;
2797
Willy Tarreau0f228a02015-05-01 15:37:53 +02002798 if (sess->fe->options & PR_O_IGNORE_PRB)
2799 goto failed_keep_alive;
2800
Willy Tarreau59234e92008-11-30 23:51:27 +01002801 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002802 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002803 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002804 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002805 }
2806
Willy Tarreaudc979f22012-12-04 10:39:01 +01002807 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002808 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002809 msg->msg_state = HTTP_MSG_ERROR;
2810 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002811
Willy Tarreau87b09662015-04-03 00:22:06 +02002812 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002813 proxy_inc_fe_req_ctr(sess->fe);
2814 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002815 if (sess->listener->counters)
2816 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002817
Willy Tarreaue7dff022015-04-03 01:14:29 +02002818 if (!(s->flags & SF_FINST_MASK))
2819 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002820 return 0;
2821 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002822
Willy Tarreau59234e92008-11-30 23:51:27 +01002823 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002824 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002825 if (!(s->flags & SF_ERR_MASK))
2826 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002827
Willy Tarreaufcffa692010-01-10 14:21:19 +01002828 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002829 goto failed_keep_alive;
2830
Willy Tarreau0f228a02015-05-01 15:37:53 +02002831 if (sess->fe->options & PR_O_IGNORE_PRB)
2832 goto failed_keep_alive;
2833
Willy Tarreau59234e92008-11-30 23:51:27 +01002834 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002835 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002836 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002837 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002838 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002839 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01002840 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002841 msg->msg_state = HTTP_MSG_ERROR;
2842 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002843
Willy Tarreau87b09662015-04-03 00:22:06 +02002844 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002845 proxy_inc_fe_req_ctr(sess->fe);
2846 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002847 if (sess->listener->counters)
2848 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002849
Willy Tarreaue7dff022015-04-03 01:14:29 +02002850 if (!(s->flags & SF_FINST_MASK))
2851 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002852 return 0;
2853 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002854
Willy Tarreau59234e92008-11-30 23:51:27 +01002855 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002856 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002857 if (!(s->flags & SF_ERR_MASK))
2858 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002859
Willy Tarreaufcffa692010-01-10 14:21:19 +01002860 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002861 goto failed_keep_alive;
2862
Willy Tarreau0f228a02015-05-01 15:37:53 +02002863 if (sess->fe->options & PR_O_IGNORE_PRB)
2864 goto failed_keep_alive;
2865
Willy Tarreau4076a152009-04-02 15:18:36 +02002866 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002867 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002868 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002869 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002870 msg->msg_state = HTTP_MSG_ERROR;
2871 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002872
Willy Tarreau87b09662015-04-03 00:22:06 +02002873 stream_inc_http_err_ctr(s);
2874 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002875 proxy_inc_fe_req_ctr(sess->fe);
2876 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002877 if (sess->listener->counters)
2878 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002879
Willy Tarreaue7dff022015-04-03 01:14:29 +02002880 if (!(s->flags & SF_FINST_MASK))
2881 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002882 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002883 }
2884
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002885 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002886 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002887 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002888#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002889 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2890 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002891 /* We need more data, we have to re-enable quick-ack in case we
2892 * previously disabled it, otherwise we might cause the client
2893 * to delay next data.
2894 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002895 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002896 }
2897#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002898
Willy Tarreaufcffa692010-01-10 14:21:19 +01002899 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2900 /* If the client starts to talk, let's fall back to
2901 * request timeout processing.
2902 */
2903 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002904 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002905 }
2906
Willy Tarreau59234e92008-11-30 23:51:27 +01002907 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002908 if (!tick_isset(req->analyse_exp)) {
2909 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2910 (txn->flags & TX_WAIT_NEXT_RQ) &&
2911 tick_isset(s->be->timeout.httpka))
2912 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2913 else
2914 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2915 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002916
Willy Tarreau59234e92008-11-30 23:51:27 +01002917 /* we're not ready yet */
2918 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002919
2920 failed_keep_alive:
2921 /* Here we process low-level errors for keep-alive requests. In
2922 * short, if the request is not the first one and it experiences
2923 * a timeout, read error or shutdown, we just silently close so
2924 * that the client can try again.
2925 */
2926 txn->status = 0;
2927 msg->msg_state = HTTP_MSG_RQBEFORE;
2928 req->analysers = 0;
2929 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002930 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002931 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau350f4872014-11-28 14:42:25 +01002932 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002933 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002934 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002935
Willy Tarreaud787e662009-07-07 10:14:51 +02002936 /* OK now we have a complete HTTP request with indexed headers. Let's
2937 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002938 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002939 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002940 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002941 * byte after the last LF. msg->sov points to the first byte of data.
2942 * msg->eol cannot be trusted because it may have been left uninitialized
2943 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002944 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002945
Willy Tarreau87b09662015-04-03 00:22:06 +02002946 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002947 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002948
Willy Tarreaub16a5742010-01-10 14:46:16 +01002949 if (txn->flags & TX_WAIT_NEXT_RQ) {
2950 /* kill the pending keep-alive timeout */
2951 txn->flags &= ~TX_WAIT_NEXT_RQ;
2952 req->analyse_exp = TICK_ETERNITY;
2953 }
2954
2955
Willy Tarreaud787e662009-07-07 10:14:51 +02002956 /* Maybe we found in invalid header name while we were configured not
2957 * to block on that, so we have to capture it now.
2958 */
2959 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002960 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002961
Willy Tarreau59234e92008-11-30 23:51:27 +01002962 /*
2963 * 1: identify the method
2964 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002965 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002966
2967 /* we can make use of server redirect on GET and HEAD */
2968 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002969 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002970
Willy Tarreau59234e92008-11-30 23:51:27 +01002971 /*
2972 * 2: check if the URI matches the monitor_uri.
2973 * We have to do this for every request which gets in, because
2974 * the monitor-uri is defined by the frontend.
2975 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002976 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2977 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002978 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002979 sess->fe->monitor_uri,
2980 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002981 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002982 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002983 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002984 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002985
Willy Tarreaue7dff022015-04-03 01:14:29 +02002986 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002987 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002988
Willy Tarreau59234e92008-11-30 23:51:27 +01002989 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002990 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002991 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002992
Willy Tarreau59234e92008-11-30 23:51:27 +01002993 ret = acl_pass(ret);
2994 if (cond->pol == ACL_COND_UNLESS)
2995 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002996
Willy Tarreau59234e92008-11-30 23:51:27 +01002997 if (ret) {
2998 /* we fail this request, let's return 503 service unavail */
2999 txn->status = 503;
Willy Tarreau350f4872014-11-28 14:42:25 +01003000 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02003001 if (!(s->flags & SF_ERR_MASK))
3002 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01003003 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01003004 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003005 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01003006
Willy Tarreau59234e92008-11-30 23:51:27 +01003007 /* nothing to fail, let's reply normaly */
3008 txn->status = 200;
Willy Tarreau350f4872014-11-28 14:42:25 +01003009 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02003010 if (!(s->flags & SF_ERR_MASK))
3011 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01003012 goto return_prx_cond;
3013 }
3014
3015 /*
3016 * 3: Maybe we have to copy the original REQURI for the logs ?
3017 * Note: we cannot log anymore if the request has been
3018 * classified as invalid.
3019 */
3020 if (unlikely(s->logs.logwait & LW_REQ)) {
3021 /* we have a complete HTTP request that we must log */
3022 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
3023 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003024
Willy Tarreau59234e92008-11-30 23:51:27 +01003025 if (urilen >= REQURI_LEN)
3026 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02003027 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01003028 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003029
Willy Tarreaud79a3b22012-12-28 09:40:16 +01003030 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01003031 s->do_log(s);
3032 } else {
3033 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003034 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003035 }
Willy Tarreau06619262006-12-17 08:37:22 +01003036
Willy Tarreau91852eb2015-05-01 13:26:00 +02003037 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
3038 * exactly one digit "." one digit. This check may be disabled using
3039 * option accept-invalid-http-request.
3040 */
3041 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
3042 if (msg->sl.rq.v_l != 8) {
3043 msg->err_pos = msg->sl.rq.v;
3044 goto return_bad_req;
3045 }
3046
3047 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
3048 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
3049 req->buf->p[msg->sl.rq.v + 6] != '.' ||
3050 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
3051 msg->err_pos = msg->sl.rq.v + 4;
3052 goto return_bad_req;
3053 }
3054 }
Willy Tarreau13317662015-05-01 13:47:08 +02003055 else {
3056 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
3057 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
3058 goto return_bad_req;
3059 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02003060
Willy Tarreau5b154472009-12-21 20:11:07 +01003061 /* ... and check if the request is HTTP/1.1 or above */
3062 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003063 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
3064 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
3065 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003066 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01003067
3068 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01003069 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01003070
Willy Tarreau88d349d2010-01-25 12:15:43 +01003071 /* if the frontend has "option http-use-proxy-header", we'll check if
3072 * we have what looks like a proxied connection instead of a connection,
3073 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
3074 * Note that this is *not* RFC-compliant, however browsers and proxies
3075 * happen to do that despite being non-standard :-(
3076 * We consider that a request not beginning with either '/' or '*' is
3077 * a proxied connection, which covers both "scheme://location" and
3078 * CONNECT ip:port.
3079 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003080 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003081 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01003082 txn->flags |= TX_USE_PX_CONN;
3083
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003084 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003085 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003086
Willy Tarreau59234e92008-11-30 23:51:27 +01003087 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003088 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003089 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003090 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003091
Willy Tarreau557f1992015-05-01 10:05:17 +02003092 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3093 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003094 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003095 * The length of a message body is determined by one of the following
3096 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003097 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003098 * 1. Any response to a HEAD request and any response with a 1xx
3099 * (Informational), 204 (No Content), or 304 (Not Modified) status
3100 * code is always terminated by the first empty line after the
3101 * header fields, regardless of the header fields present in the
3102 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003103 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003104 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3105 * the connection will become a tunnel immediately after the empty
3106 * line that concludes the header fields. A client MUST ignore any
3107 * Content-Length or Transfer-Encoding header fields received in
3108 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003109 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003110 * 3. If a Transfer-Encoding header field is present and the chunked
3111 * transfer coding (Section 4.1) is the final encoding, the message
3112 * body length is determined by reading and decoding the chunked
3113 * data until the transfer coding indicates the data is complete.
3114 *
3115 * If a Transfer-Encoding header field is present in a response and
3116 * the chunked transfer coding is not the final encoding, the
3117 * message body length is determined by reading the connection until
3118 * it is closed by the server. If a Transfer-Encoding header field
3119 * is present in a request and the chunked transfer coding is not
3120 * the final encoding, the message body length cannot be determined
3121 * reliably; the server MUST respond with the 400 (Bad Request)
3122 * status code and then close the connection.
3123 *
3124 * If a message is received with both a Transfer-Encoding and a
3125 * Content-Length header field, the Transfer-Encoding overrides the
3126 * Content-Length. Such a message might indicate an attempt to
3127 * perform request smuggling (Section 9.5) or response splitting
3128 * (Section 9.4) and ought to be handled as an error. A sender MUST
3129 * remove the received Content-Length field prior to forwarding such
3130 * a message downstream.
3131 *
3132 * 4. If a message is received without Transfer-Encoding and with
3133 * either multiple Content-Length header fields having differing
3134 * field-values or a single Content-Length header field having an
3135 * invalid value, then the message framing is invalid and the
3136 * recipient MUST treat it as an unrecoverable error. If this is a
3137 * request message, the server MUST respond with a 400 (Bad Request)
3138 * status code and then close the connection. If this is a response
3139 * message received by a proxy, the proxy MUST close the connection
3140 * to the server, discard the received response, and send a 502 (Bad
3141 * Gateway) response to the client. If this is a response message
3142 * received by a user agent, the user agent MUST close the
3143 * connection to the server and discard the received response.
3144 *
3145 * 5. If a valid Content-Length header field is present without
3146 * Transfer-Encoding, its decimal value defines the expected message
3147 * body length in octets. If the sender closes the connection or
3148 * the recipient times out before the indicated number of octets are
3149 * received, the recipient MUST consider the message to be
3150 * incomplete and close the connection.
3151 *
3152 * 6. If this is a request message and none of the above are true, then
3153 * the message body length is zero (no message body is present).
3154 *
3155 * 7. Otherwise, this is a response message without a declared message
3156 * body length, so the message body length is determined by the
3157 * number of octets received prior to the server closing the
3158 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003159 */
3160
Willy Tarreau32b47f42009-10-18 20:55:02 +02003161 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003162 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003163 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003164 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003165 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3166 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003167 /* chunked not last, return badreq */
3168 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003169 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003170 }
3171
Willy Tarreau1c913912015-04-30 10:57:51 +02003172 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003173 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003174 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3175 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3176 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3177 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003178 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003179 signed long long cl;
3180
Willy Tarreauad14f752011-09-02 20:33:27 +02003181 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003182 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003183 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003184 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003185
Willy Tarreauad14f752011-09-02 20:33:27 +02003186 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003187 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003188 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003189 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003190
Willy Tarreauad14f752011-09-02 20:33:27 +02003191 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003192 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003193 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003194 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003195
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003196 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003197 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003198 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003199 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003200
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003201 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003202 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003203 }
3204
Willy Tarreau34dfc602015-05-01 10:09:49 +02003205 /* even bodyless requests have a known length */
3206 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003207
Willy Tarreau179085c2014-04-28 16:48:56 +02003208 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3209 * only change if both the request and the config reference something else.
3210 * Option httpclose by itself sets tunnel mode where headers are mangled.
3211 * However, if another mode is set, it will affect it (eg: server-close/
3212 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3213 * if FE and BE have the same settings (common). The method consists in
3214 * checking if options changed between the two calls (implying that either
3215 * one is non-null, or one of them is non-null and we are there for the first
3216 * time.
3217 */
3218 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003219 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003220 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003221
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003222 /* we may have to wait for the request's body */
3223 if ((s->be->options & PR_O_WREQ_BODY) &&
3224 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3225 req->analysers |= AN_REQ_HTTP_BODY;
3226
Willy Tarreaud787e662009-07-07 10:14:51 +02003227 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003228 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003229 req->analyse_exp = TICK_ETERNITY;
3230 return 1;
3231
3232 return_bad_req:
3233 /* We centralize bad requests processing here */
3234 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3235 /* we detected a parsing error. We want to archive this request
3236 * in the dedicated proxy area for later troubleshooting.
3237 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003238 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003239 }
3240
3241 txn->req.msg_state = HTTP_MSG_ERROR;
3242 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01003243 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003244
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003245 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003246 if (sess->listener->counters)
3247 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003248
3249 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003250 if (!(s->flags & SF_ERR_MASK))
3251 s->flags |= SF_ERR_PRXCOND;
3252 if (!(s->flags & SF_FINST_MASK))
3253 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003254
3255 req->analysers = 0;
3256 req->analyse_exp = TICK_ETERNITY;
3257 return 0;
3258}
3259
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003260
Willy Tarreau347a35d2013-11-22 17:51:09 +01003261/* This function prepares an applet to handle the stats. It can deal with the
3262 * "100-continue" expectation, check that admin rules are met for POST requests,
3263 * and program a response message if something was unexpected. It cannot fail
3264 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003265 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003266 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003267 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003268 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003269int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003270{
3271 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003272 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003273 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003274 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003275 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003276 struct uri_auth *uri_auth = s->be->uri_auth;
3277 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003278 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003279
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003280 appctx = si_appctx(si);
3281 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3282 appctx->st1 = appctx->st2 = 0;
3283 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3284 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003285 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003286 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003287
3288 uri = msg->chn->buf->p + msg->sl.rq.u;
3289 lookup = uri + uri_auth->uri_len;
3290
3291 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3292 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003293 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003294 break;
3295 }
3296 }
3297
3298 if (uri_auth->refresh) {
3299 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3300 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003301 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003302 break;
3303 }
3304 }
3305 }
3306
3307 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3308 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003309 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003310 break;
3311 }
3312 }
3313
3314 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3315 if (memcmp(h, ";st=", 4) == 0) {
3316 int i;
3317 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003318 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003319 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3320 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003321 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003322 break;
3323 }
3324 }
3325 break;
3326 }
3327 }
3328
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003329 appctx->ctx.stats.scope_str = 0;
3330 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003331 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3332 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3333 int itx = 0;
3334 const char *h2;
3335 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3336 const char *err;
3337
3338 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3339 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003340 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003341 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3342 itx++;
3343 h++;
3344 }
3345
3346 if (itx > STAT_SCOPE_TXT_MAXLEN)
3347 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003348 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003349
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003350 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003351 memcpy(scope_txt, h2, itx);
3352 scope_txt[itx] = '\0';
3353 err = invalid_char(scope_txt);
3354 if (err) {
3355 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003356 appctx->ctx.stats.scope_str = 0;
3357 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003358 }
3359 break;
3360 }
3361 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003362
3363 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003364 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003365 int ret = 1;
3366
3367 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003368 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003369 ret = acl_pass(ret);
3370 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3371 ret = !ret;
3372 }
3373
3374 if (ret) {
3375 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003376 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003377 break;
3378 }
3379 }
3380
3381 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003382 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003383 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003384 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003385 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3386 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003387 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003388 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003389 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003390 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3391 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003392 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003393 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003394 else {
3395 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003396 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003397 }
3398
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003399 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003400 return 1;
3401}
3402
Lukas Tribus67db8df2013-06-23 17:37:13 +02003403/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3404 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3405 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003406void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003407{
3408#ifdef IP_TOS
3409 if (from.ss_family == AF_INET)
3410 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3411#endif
3412#ifdef IPV6_TCLASS
3413 if (from.ss_family == AF_INET6) {
3414 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3415 /* v4-mapped addresses need IP_TOS */
3416 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3417 else
3418 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3419 }
3420#endif
3421}
3422
Willy Tarreau87b09662015-04-03 00:22:06 +02003423int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003424 const char* name, unsigned int name_len,
3425 const char *str, struct my_regex *re,
3426 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003427{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003428 struct hdr_ctx ctx;
3429 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003430 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003431 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3432 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003433 struct chunk *output = get_trash_chunk();
3434
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003435 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003436
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003437 /* Choose the header browsing function. */
3438 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003439 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003440 http_find_hdr_func = http_find_header2;
3441 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003442 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003443 http_find_hdr_func = http_find_full_header2;
3444 break;
3445 default: /* impossible */
3446 return -1;
3447 }
3448
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003449 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3450 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003451 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003452 char *val = ctx.line + ctx.val;
3453 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003454
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003455 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3456 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003457
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003458 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003459 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003460 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003461
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003462 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003463
3464 hdr->len += delta;
3465 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003466
3467 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003468 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003469 }
3470
3471 return 0;
3472}
3473
Willy Tarreau87b09662015-04-03 00:22:06 +02003474static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003475 const char* name, unsigned int name_len,
3476 struct list *fmt, struct my_regex *re,
3477 int action)
3478{
3479 struct chunk *replace = get_trash_chunk();
3480
3481 replace->len = build_logline(s, replace->str, replace->size, fmt);
3482 if (replace->len >= replace->size - 1)
3483 return -1;
3484
3485 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3486}
3487
Willy Tarreau87b09662015-04-03 00:22:06 +02003488/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003489 * transaction <txn>. Returns the verdict of the first rule that prevents
3490 * further processing of the request (auth, deny, ...), and defaults to
3491 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3492 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3493 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003494 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003495enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003496http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003497{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003498 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003499 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003500 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003501 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003502 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003503 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003504
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003505 /* If "the current_rule_list" match the executed rule list, we are in
3506 * resume condition. If a resume is needed it is always in the action
3507 * and never in the ACL or converters. In this case, we initialise the
3508 * current rule, and go to the action execution point.
3509 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003510 if (s->current_rule) {
3511 rule = s->current_rule;
3512 s->current_rule = NULL;
3513 if (s->current_rule_list == rules)
3514 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003515 }
3516 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003517
Willy Tarreauff011f22011-01-06 17:51:27 +01003518 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003519
Willy Tarreau96257ec2012-12-27 10:46:37 +01003520 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003521 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003522 int ret;
3523
Willy Tarreau192252e2015-04-04 01:47:55 +02003524 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003525 ret = acl_pass(ret);
3526
Willy Tarreauff011f22011-01-06 17:51:27 +01003527 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003528 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003529
3530 if (!ret) /* condition not matched */
3531 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003532 }
3533
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003534resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003535 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003536 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003537 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003538
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003539 case ACT_ACTION_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003540 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003541 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003542
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003543 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003544 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003545 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003546 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003547
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003548 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003549 /* Auth might be performed on regular http-req rules as well as on stats */
3550 auth_realm = rule->arg.auth.realm;
3551 if (!auth_realm) {
3552 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3553 auth_realm = STATS_DEFAULT_REALM;
3554 else
3555 auth_realm = px->id;
3556 }
3557 /* send 401/407 depending on whether we use a proxy or not. We still
3558 * count one error, because normal browsing won't significantly
3559 * increase the counter but brute force attempts will.
3560 */
3561 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3562 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3563 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003564 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003565 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003566
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003567 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003568 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3569 return HTTP_RULE_RES_BADREQ;
3570 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003571
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003572 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003573 s->task->nice = rule->arg.nice;
3574 break;
3575
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003576 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003577 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003578 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003579 break;
3580
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003581 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003582#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003583 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003584 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003585#endif
3586 break;
3587
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003588 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003589 s->logs.level = rule->arg.loglevel;
3590 break;
3591
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003592 case ACT_HTTP_REPLACE_HDR:
3593 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003594 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3595 rule->arg.hdr_add.name_len,
3596 &rule->arg.hdr_add.fmt,
3597 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003598 return HTTP_RULE_RES_BADREQ;
3599 break;
3600
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003601 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003602 ctx.idx = 0;
3603 /* remove all occurrences of the header */
3604 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3605 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3606 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003607 }
Willy Tarreau85603282015-01-21 20:39:27 +01003608 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003609
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003610 case ACT_HTTP_SET_HDR:
3611 case ACT_HTTP_ADD_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003612 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3613 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3614 trash.len = rule->arg.hdr_add.name_len;
3615 trash.str[trash.len++] = ':';
3616 trash.str[trash.len++] = ' ';
3617 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01003618
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003619 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003620 /* remove all occurrences of the header */
3621 ctx.idx = 0;
3622 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3623 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3624 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3625 }
3626 }
3627
Willy Tarreau96257ec2012-12-27 10:46:37 +01003628 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3629 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003630
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003631 case ACT_HTTP_DEL_ACL:
3632 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003633 struct pat_ref *ref;
3634 char *key;
3635 int len;
3636
3637 /* collect reference */
3638 ref = pat_ref_lookup(rule->arg.map.ref);
3639 if (!ref)
3640 continue;
3641
3642 /* collect key */
3643 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3644 key = trash.str;
3645 key[len] = '\0';
3646
3647 /* perform update */
3648 /* returned code: 1=ok, 0=ko */
3649 pat_ref_delete(ref, key);
3650
3651 break;
3652 }
3653
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003654 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003655 struct pat_ref *ref;
3656 char *key;
3657 struct chunk *trash_key;
3658 int len;
3659
3660 trash_key = get_trash_chunk();
3661
3662 /* collect reference */
3663 ref = pat_ref_lookup(rule->arg.map.ref);
3664 if (!ref)
3665 continue;
3666
3667 /* collect key */
3668 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3669 key = trash_key->str;
3670 key[len] = '\0';
3671
3672 /* perform update */
3673 /* add entry only if it does not already exist */
3674 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003675 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003676
3677 break;
3678 }
3679
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003680 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003681 struct pat_ref *ref;
3682 char *key, *value;
3683 struct chunk *trash_key, *trash_value;
3684 int len;
3685
3686 trash_key = get_trash_chunk();
3687 trash_value = get_trash_chunk();
3688
3689 /* collect reference */
3690 ref = pat_ref_lookup(rule->arg.map.ref);
3691 if (!ref)
3692 continue;
3693
3694 /* collect key */
3695 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3696 key = trash_key->str;
3697 key[len] = '\0';
3698
3699 /* collect value */
3700 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3701 value = trash_value->str;
3702 value[len] = '\0';
3703
3704 /* perform update */
3705 if (pat_ref_find_elt(ref, key) != NULL)
3706 /* update entry if it exists */
3707 pat_ref_set(ref, key, value, NULL);
3708 else
3709 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003710 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003711
3712 break;
3713 }
William Lallemand73025dd2014-04-24 14:38:37 +02003714
Thierry FOURNIER42148732015-09-02 17:17:33 +02003715 case ACT_CUSTOM:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003716 switch (rule->action_ptr(rule, px, s->sess, s)) {
3717 case ACT_RET_ERR:
3718 case ACT_RET_CONT:
3719 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003720 case ACT_RET_STOP:
3721 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003722 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003723 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003724 return HTTP_RULE_RES_YIELD;
3725 }
William Lallemand73025dd2014-04-24 14:38:37 +02003726 break;
3727
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003728 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003729 /* Note: only the first valid tracking parameter of each
3730 * applies.
3731 */
3732
3733 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3734 struct stktable *t;
3735 struct stksess *ts;
3736 struct stktable_key *key;
3737 void *ptr;
3738
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003739 t = rule->arg.trk_ctr.table.t;
3740 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 +02003741
3742 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003743 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003744
3745 /* let's count a new HTTP request as it's the first time we do it */
3746 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3747 if (ptr)
3748 stktable_data_cast(ptr, http_req_cnt)++;
3749
3750 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3751 if (ptr)
3752 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3753 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3754
3755 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003756 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003757 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3758 }
3759 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003760 break;
3761
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003762 case ACT_HTTP_REQ_SET_SRC:
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003763 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
3764 struct sample *smp;
3765
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02003766 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 +02003767
3768 if (smp) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003769 if (smp->data.type == SMP_T_IPV4) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003770 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003771 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003772 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003773 } else if (smp->data.type == SMP_T_IPV6) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003774 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003775 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 +02003776 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0;
3777 }
3778 }
3779 }
3780 break;
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003781
3782 /* other flags exists, but normaly, they never be matched. */
3783 default:
3784 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003785 }
3786 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003787
3788 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003789 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003790}
3791
Willy Tarreau71241ab2012-12-27 11:30:54 +01003792
Willy Tarreau51d861a2015-05-22 17:30:48 +02003793/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3794 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3795 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3796 * is returned, the process can continue the evaluation of next rule list. If
3797 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3798 * is returned, it means the operation could not be processed and a server error
3799 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3800 * deny rule. If *YIELD is returned, the caller must call again the function
3801 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003802 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003803static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003804http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003805{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003806 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003807 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003808 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003809 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003810 struct hdr_ctx ctx;
3811
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003812 /* If "the current_rule_list" match the executed rule list, we are in
3813 * resume condition. If a resume is needed it is always in the action
3814 * and never in the ACL or converters. In this case, we initialise the
3815 * current rule, and go to the action execution point.
3816 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003817 if (s->current_rule) {
3818 rule = s->current_rule;
3819 s->current_rule = NULL;
3820 if (s->current_rule_list == rules)
3821 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003822 }
3823 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003824
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003825 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003826
3827 /* check optional condition */
3828 if (rule->cond) {
3829 int ret;
3830
Willy Tarreau192252e2015-04-04 01:47:55 +02003831 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003832 ret = acl_pass(ret);
3833
3834 if (rule->cond->pol == ACL_COND_UNLESS)
3835 ret = !ret;
3836
3837 if (!ret) /* condition not matched */
3838 continue;
3839 }
3840
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003841resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003842 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003843 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003844 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003845
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003846 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003847 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003848 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003849
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003850 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003851 s->task->nice = rule->arg.nice;
3852 break;
3853
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003854 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003855 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003856 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003857 break;
3858
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003859 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003860#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003861 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003862 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003863#endif
3864 break;
3865
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003866 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003867 s->logs.level = rule->arg.loglevel;
3868 break;
3869
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003870 case ACT_HTTP_REPLACE_HDR:
3871 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003872 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3873 rule->arg.hdr_add.name_len,
3874 &rule->arg.hdr_add.fmt,
3875 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003876 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003877 break;
3878
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003879 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003880 ctx.idx = 0;
3881 /* remove all occurrences of the header */
3882 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3883 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3884 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3885 }
Willy Tarreau85603282015-01-21 20:39:27 +01003886 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003887
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003888 case ACT_HTTP_SET_HDR:
3889 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003890 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3891 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3892 trash.len = rule->arg.hdr_add.name_len;
3893 trash.str[trash.len++] = ':';
3894 trash.str[trash.len++] = ' ';
3895 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 +01003896
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003897 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003898 /* remove all occurrences of the header */
3899 ctx.idx = 0;
3900 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3901 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3902 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3903 }
3904 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003905 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3906 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003907
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003908 case ACT_HTTP_DEL_ACL:
3909 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003910 struct pat_ref *ref;
3911 char *key;
3912 int len;
3913
3914 /* collect reference */
3915 ref = pat_ref_lookup(rule->arg.map.ref);
3916 if (!ref)
3917 continue;
3918
3919 /* collect key */
3920 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3921 key = trash.str;
3922 key[len] = '\0';
3923
3924 /* perform update */
3925 /* returned code: 1=ok, 0=ko */
3926 pat_ref_delete(ref, key);
3927
3928 break;
3929 }
3930
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003931 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003932 struct pat_ref *ref;
3933 char *key;
3934 struct chunk *trash_key;
3935 int len;
3936
3937 trash_key = get_trash_chunk();
3938
3939 /* collect reference */
3940 ref = pat_ref_lookup(rule->arg.map.ref);
3941 if (!ref)
3942 continue;
3943
3944 /* collect key */
3945 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3946 key = trash_key->str;
3947 key[len] = '\0';
3948
3949 /* perform update */
3950 /* check if the entry already exists */
3951 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003952 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003953
3954 break;
3955 }
3956
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003957 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003958 struct pat_ref *ref;
3959 char *key, *value;
3960 struct chunk *trash_key, *trash_value;
3961 int len;
3962
3963 trash_key = get_trash_chunk();
3964 trash_value = get_trash_chunk();
3965
3966 /* collect reference */
3967 ref = pat_ref_lookup(rule->arg.map.ref);
3968 if (!ref)
3969 continue;
3970
3971 /* collect key */
3972 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3973 key = trash_key->str;
3974 key[len] = '\0';
3975
3976 /* collect value */
3977 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3978 value = trash_value->str;
3979 value[len] = '\0';
3980
3981 /* perform update */
3982 if (pat_ref_find_elt(ref, key) != NULL)
3983 /* update entry if it exists */
3984 pat_ref_set(ref, key, value, NULL);
3985 else
3986 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003987 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003988
3989 break;
3990 }
William Lallemand73025dd2014-04-24 14:38:37 +02003991
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003992 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003993 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3994 return HTTP_RULE_RES_BADREQ;
3995 return HTTP_RULE_RES_DONE;
3996
Thierry FOURNIER42148732015-09-02 17:17:33 +02003997 case ACT_CUSTOM:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003998 switch (rule->action_ptr(rule, px, s->sess, s)) {
3999 case ACT_RET_ERR:
4000 case ACT_RET_CONT:
4001 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02004002 case ACT_RET_STOP:
4003 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004004 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02004005 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004006 return HTTP_RULE_RES_YIELD;
4007 }
William Lallemand73025dd2014-04-24 14:38:37 +02004008 break;
4009
Thierry FOURNIER22e49012015-08-05 19:13:48 +02004010 /* other flags exists, but normaly, they never be matched. */
4011 default:
4012 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004013 }
4014 }
4015
4016 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004017 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004018}
4019
4020
Willy Tarreau71241ab2012-12-27 11:30:54 +01004021/* Perform an HTTP redirect based on the information in <rule>. The function
4022 * returns non-zero on success, or zero in case of a, irrecoverable error such
4023 * as too large a request to build a valid response.
4024 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004025static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004026{
Willy Tarreaub329a312015-05-22 16:27:37 +02004027 struct http_msg *req = &txn->req;
4028 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004029 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004030 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004031
4032 /* build redirect message */
4033 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004034 case 308:
4035 msg_fmt = HTTP_308;
4036 break;
4037 case 307:
4038 msg_fmt = HTTP_307;
4039 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004040 case 303:
4041 msg_fmt = HTTP_303;
4042 break;
4043 case 301:
4044 msg_fmt = HTTP_301;
4045 break;
4046 case 302:
4047 default:
4048 msg_fmt = HTTP_302;
4049 break;
4050 }
4051
4052 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
4053 return 0;
4054
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004055 location = trash.str + trash.len;
4056
Willy Tarreau71241ab2012-12-27 11:30:54 +01004057 switch(rule->type) {
4058 case REDIRECT_TYPE_SCHEME: {
4059 const char *path;
4060 const char *host;
4061 struct hdr_ctx ctx;
4062 int pathlen;
4063 int hostlen;
4064
4065 host = "";
4066 hostlen = 0;
4067 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004068 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004069 host = ctx.line + ctx.val;
4070 hostlen = ctx.vlen;
4071 }
4072
4073 path = http_get_path(txn);
4074 /* build message using path */
4075 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004076 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004077 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4078 int qs = 0;
4079 while (qs < pathlen) {
4080 if (path[qs] == '?') {
4081 pathlen = qs;
4082 break;
4083 }
4084 qs++;
4085 }
4086 }
4087 } else {
4088 path = "/";
4089 pathlen = 1;
4090 }
4091
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004092 if (rule->rdr_str) { /* this is an old "redirect" rule */
4093 /* check if we can add scheme + "://" + host + path */
4094 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
4095 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004096
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004097 /* add scheme */
4098 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4099 trash.len += rule->rdr_len;
4100 }
4101 else {
4102 /* add scheme with executing log format */
4103 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004104
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004105 /* check if we can add scheme + "://" + host + path */
4106 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
4107 return 0;
4108 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004109 /* add "://" */
4110 memcpy(trash.str + trash.len, "://", 3);
4111 trash.len += 3;
4112
4113 /* add host */
4114 memcpy(trash.str + trash.len, host, hostlen);
4115 trash.len += hostlen;
4116
4117 /* add path */
4118 memcpy(trash.str + trash.len, path, pathlen);
4119 trash.len += pathlen;
4120
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004121 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004122 if (trash.len && trash.str[trash.len - 1] != '/' &&
4123 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4124 if (trash.len > trash.size - 5)
4125 return 0;
4126 trash.str[trash.len] = '/';
4127 trash.len++;
4128 }
4129
4130 break;
4131 }
4132 case REDIRECT_TYPE_PREFIX: {
4133 const char *path;
4134 int pathlen;
4135
4136 path = http_get_path(txn);
4137 /* build message using path */
4138 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004139 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004140 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4141 int qs = 0;
4142 while (qs < pathlen) {
4143 if (path[qs] == '?') {
4144 pathlen = qs;
4145 break;
4146 }
4147 qs++;
4148 }
4149 }
4150 } else {
4151 path = "/";
4152 pathlen = 1;
4153 }
4154
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004155 if (rule->rdr_str) { /* this is an old "redirect" rule */
4156 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4157 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004158
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004159 /* add prefix. Note that if prefix == "/", we don't want to
4160 * add anything, otherwise it makes it hard for the user to
4161 * configure a self-redirection.
4162 */
4163 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4164 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4165 trash.len += rule->rdr_len;
4166 }
4167 }
4168 else {
4169 /* add prefix with executing log format */
4170 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4171
4172 /* Check length */
4173 if (trash.len + pathlen > trash.size - 4)
4174 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004175 }
4176
4177 /* add path */
4178 memcpy(trash.str + trash.len, path, pathlen);
4179 trash.len += pathlen;
4180
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004181 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004182 if (trash.len && trash.str[trash.len - 1] != '/' &&
4183 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4184 if (trash.len > trash.size - 5)
4185 return 0;
4186 trash.str[trash.len] = '/';
4187 trash.len++;
4188 }
4189
4190 break;
4191 }
4192 case REDIRECT_TYPE_LOCATION:
4193 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004194 if (rule->rdr_str) { /* this is an old "redirect" rule */
4195 if (trash.len + rule->rdr_len > trash.size - 4)
4196 return 0;
4197
4198 /* add location */
4199 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4200 trash.len += rule->rdr_len;
4201 }
4202 else {
4203 /* add location with executing log format */
4204 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004205
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004206 /* Check left length */
4207 if (trash.len > trash.size - 4)
4208 return 0;
4209 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004210 break;
4211 }
4212
4213 if (rule->cookie_len) {
4214 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4215 trash.len += 14;
4216 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4217 trash.len += rule->cookie_len;
4218 memcpy(trash.str + trash.len, "\r\n", 2);
4219 trash.len += 2;
4220 }
4221
4222 /* add end of headers and the keep-alive/close status.
4223 * We may choose to set keep-alive if the Location begins
4224 * with a slash, because the client will come back to the
4225 * same server.
4226 */
4227 txn->status = rule->code;
4228 /* let's log the request time */
4229 s->logs.tv_request = now;
4230
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004231 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004232 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004233 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004234 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4235 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4236 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004237 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004238 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4239 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4240 trash.len += 30;
4241 } else {
4242 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4243 trash.len += 24;
4244 }
4245 }
4246 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4247 trash.len += 4;
Willy Tarreaub329a312015-05-22 16:27:37 +02004248 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004249 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004250 bi_fast_delete(req->chn->buf, req->sov);
4251 req->next -= req->sov;
4252 req->sov = 0;
4253 s->req.analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004254 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreaub329a312015-05-22 16:27:37 +02004255 req->msg_state = HTTP_MSG_CLOSED;
4256 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004257 /* Trim any possible response */
4258 res->chn->buf->i = 0;
4259 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004260 } else {
4261 /* keep-alive not possible */
4262 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4263 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4264 trash.len += 29;
4265 } else {
4266 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4267 trash.len += 23;
4268 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004269 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreaub329a312015-05-22 16:27:37 +02004270 req->chn->analysers = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004271 }
4272
Willy Tarreaue7dff022015-04-03 01:14:29 +02004273 if (!(s->flags & SF_ERR_MASK))
4274 s->flags |= SF_ERR_LOCAL;
4275 if (!(s->flags & SF_FINST_MASK))
4276 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004277
4278 return 1;
4279}
4280
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004281/* This stream analyser runs all HTTP request processing which is common to
4282 * frontends and backends, which means blocking ACLs, filters, connection-close,
4283 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004284 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004285 * either needs more data or wants to immediately abort the request (eg: deny,
4286 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004287 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004288int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004289{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004290 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004291 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004292 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004293 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004294 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004295 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004296
Willy Tarreau655dce92009-11-08 13:10:58 +01004297 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004298 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004299 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004300 }
4301
Willy Tarreau87b09662015-04-03 00:22:06 +02004302 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 +02004303 now_ms, __FUNCTION__,
4304 s,
4305 req,
4306 req->rex, req->wex,
4307 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004308 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004309 req->analysers);
4310
Willy Tarreau65410832014-04-28 21:25:43 +02004311 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004312 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004313
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004314 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004315 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004316 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004317
Willy Tarreau0b748332014-04-29 00:13:29 +02004318 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004319 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4320 goto return_prx_yield;
4321
Willy Tarreau0b748332014-04-29 00:13:29 +02004322 case HTTP_RULE_RES_CONT:
4323 case HTTP_RULE_RES_STOP: /* nothing to do */
4324 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004325
Willy Tarreau0b748332014-04-29 00:13:29 +02004326 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4327 if (txn->flags & TX_CLTARPIT)
4328 goto tarpit;
4329 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004330
Willy Tarreau0b748332014-04-29 00:13:29 +02004331 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4332 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004333
Willy Tarreau0b748332014-04-29 00:13:29 +02004334 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004335 goto done;
4336
Willy Tarreau0b748332014-04-29 00:13:29 +02004337 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4338 goto return_bad_req;
4339 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004340 }
4341
Willy Tarreau52542592014-04-28 18:33:26 +02004342 /* OK at this stage, we know that the request was accepted according to
4343 * the http-request rules, we can check for the stats. Note that the
4344 * URI is detected *before* the req* rules in order not to be affected
4345 * by a possible reqrep, while they are processed *after* so that a
4346 * reqdeny can still block them. This clearly needs to change in 1.6!
4347 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004348 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004349 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004350 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004351 txn->status = 500;
4352 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004353 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004354
Willy Tarreaue7dff022015-04-03 01:14:29 +02004355 if (!(s->flags & SF_ERR_MASK))
4356 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004357 goto return_prx_cond;
4358 }
4359
4360 /* parse the whole stats request and extract the relevant information */
4361 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004362 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004363 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004364
Willy Tarreau0b748332014-04-29 00:13:29 +02004365 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4366 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004367
Willy Tarreau0b748332014-04-29 00:13:29 +02004368 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4369 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004370 }
4371
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004372 /* evaluate the req* rules except reqadd */
4373 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004374 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004375 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004376
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004377 if (txn->flags & TX_CLDENY)
4378 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004379
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004380 if (txn->flags & TX_CLTARPIT)
4381 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004382 }
Willy Tarreau06619262006-12-17 08:37:22 +01004383
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004384 /* add request headers from the rule sets in the same order */
4385 list_for_each_entry(wl, &px->req_add, list) {
4386 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004387 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004388 ret = acl_pass(ret);
4389 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4390 ret = !ret;
4391 if (!ret)
4392 continue;
4393 }
4394
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004395 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004396 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004397 }
4398
Willy Tarreau52542592014-04-28 18:33:26 +02004399
4400 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004401 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004402 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004403 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4404 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004405
Willy Tarreaue7dff022015-04-03 01:14:29 +02004406 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4407 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4408 if (!(s->flags & SF_FINST_MASK))
4409 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004410
Willy Tarreau70730dd2014-04-24 18:06:27 +02004411 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004412 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004413 select_compression_request_header(s, req->buf);
4414
4415 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004416 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004417 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004418 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004419
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004420 /* check whether we have some ACLs set to redirect this request */
4421 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004422 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004423 int ret;
4424
Willy Tarreau192252e2015-04-04 01:47:55 +02004425 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004426 ret = acl_pass(ret);
4427 if (rule->cond->pol == ACL_COND_UNLESS)
4428 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004429 if (!ret)
4430 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004431 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004432 if (!http_apply_redirect_rule(rule, s, txn))
4433 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004434 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004435 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004436
Willy Tarreau2be39392010-01-03 17:24:51 +01004437 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4438 * If this happens, then the data will not come immediately, so we must
4439 * send all what we have without waiting. Note that due to the small gain
4440 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004441 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004442 * itself once used.
4443 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004444 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004445
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004446 done: /* done with this analyser, continue with next ones that the calling
4447 * points will have set, if any.
4448 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004449 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004450 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4451 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004452 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004453
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004454 tarpit:
4455 /* When a connection is tarpitted, we use the tarpit timeout,
4456 * which may be the same as the connect timeout if unspecified.
4457 * If unset, then set it to zero because we really want it to
4458 * eventually expire. We build the tarpit as an analyser.
4459 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004460 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004461
4462 /* wipe the request out so that we can drop the connection early
4463 * if the client closes first.
4464 */
4465 channel_dont_connect(req);
4466 req->analysers = 0; /* remove switching rules etc... */
4467 req->analysers |= AN_REQ_HTTP_TARPIT;
4468 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4469 if (!req->analyse_exp)
4470 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004471 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004472 sess->fe->fe_counters.denied_req++;
4473 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004474 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004475 if (sess->listener->counters)
4476 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004477 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004478
4479 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004480 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004481 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004482 s->logs.tv_request = now;
CJ Ess108b1dd2015-04-07 12:03:37 -04004483 stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004484 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004485 sess->fe->fe_counters.denied_req++;
4486 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004487 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004488 if (sess->listener->counters)
4489 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004490 goto return_prx_cond;
4491
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004492 return_bad_req:
4493 /* We centralize bad requests processing here */
4494 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4495 /* we detected a parsing error. We want to archive this request
4496 * in the dedicated proxy area for later troubleshooting.
4497 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004498 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004499 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004500
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004501 txn->req.msg_state = HTTP_MSG_ERROR;
4502 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004503 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004504
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004505 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004506 if (sess->listener->counters)
4507 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004508
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004509 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004510 if (!(s->flags & SF_ERR_MASK))
4511 s->flags |= SF_ERR_PRXCOND;
4512 if (!(s->flags & SF_FINST_MASK))
4513 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004514
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004515 req->analysers = 0;
4516 req->analyse_exp = TICK_ETERNITY;
4517 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004518
4519 return_prx_yield:
4520 channel_dont_connect(req);
4521 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004522}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004523
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004524/* This function performs all the processing enabled for the current request.
4525 * It returns 1 if the processing can continue on next analysers, or zero if it
4526 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004527 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004528 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004529int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004530{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004531 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004532 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004533 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004534 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004535
Willy Tarreau655dce92009-11-08 13:10:58 +01004536 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004537 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004538 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004539 return 0;
4540 }
4541
Willy Tarreau87b09662015-04-03 00:22:06 +02004542 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 +02004543 now_ms, __FUNCTION__,
4544 s,
4545 req,
4546 req->rex, req->wex,
4547 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004548 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004549 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004550
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004551 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004552 select_compression_request_header(s, req->buf);
4553
Willy Tarreau59234e92008-11-30 23:51:27 +01004554 /*
4555 * Right now, we know that we have processed the entire headers
4556 * and that unwanted requests have been filtered out. We can do
4557 * whatever we want with the remaining request. Also, now we
4558 * may have separate values for ->fe, ->be.
4559 */
Willy Tarreau06619262006-12-17 08:37:22 +01004560
Willy Tarreau59234e92008-11-30 23:51:27 +01004561 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004562 * If HTTP PROXY is set we simply get remote server address parsing
4563 * incoming request. Note that this requires that a connection is
4564 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004565 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004566 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004567 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004568 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004569
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004570 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004571 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004572 txn->req.msg_state = HTTP_MSG_ERROR;
4573 txn->status = 500;
4574 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004575 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004576
Willy Tarreaue7dff022015-04-03 01:14:29 +02004577 if (!(s->flags & SF_ERR_MASK))
4578 s->flags |= SF_ERR_RESOURCE;
4579 if (!(s->flags & SF_FINST_MASK))
4580 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004581
4582 return 0;
4583 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004584
4585 path = http_get_path(txn);
4586 url2sa(req->buf->p + msg->sl.rq.u,
4587 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004588 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004589 /* if the path was found, we have to remove everything between
4590 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4591 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4592 * u_l characters by a single "/".
4593 */
4594 if (path) {
4595 char *cur_ptr = req->buf->p;
4596 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4597 int delta;
4598
4599 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4600 http_msg_move_end(&txn->req, delta);
4601 cur_end += delta;
4602 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4603 goto return_bad_req;
4604 }
4605 else {
4606 char *cur_ptr = req->buf->p;
4607 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4608 int delta;
4609
4610 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4611 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4612 http_msg_move_end(&txn->req, delta);
4613 cur_end += delta;
4614 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4615 goto return_bad_req;
4616 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004617 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004618
Willy Tarreau59234e92008-11-30 23:51:27 +01004619 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004620 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004621 * Note that doing so might move headers in the request, but
4622 * the fields will stay coherent and the URI will not move.
4623 * This should only be performed in the backend.
4624 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02004625 if ((s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004626 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4627 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004628
William Lallemanda73203e2012-03-12 12:48:57 +01004629 /* add unique-id if "header-unique-id" is specified */
4630
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004631 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004632 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4633 goto return_bad_req;
4634 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004635 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004636 }
William Lallemanda73203e2012-03-12 12:48:57 +01004637
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004638 if (sess->fe->header_unique_id && s->unique_id) {
4639 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004640 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004641 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004642 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004643 goto return_bad_req;
4644 }
4645
Cyril Bontéb21570a2009-11-29 20:04:48 +01004646 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004647 * 9: add X-Forwarded-For if either the frontend or the backend
4648 * asks for it.
4649 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004650 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004651 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004652 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4653 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4654 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004655 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004656 /* The header is set to be added only if none is present
4657 * and we found it, so don't do anything.
4658 */
4659 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004660 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004661 /* Add an X-Forwarded-For header unless the source IP is
4662 * in the 'except' network range.
4663 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004664 if ((!sess->fe->except_mask.s_addr ||
4665 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4666 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004667 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004668 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004669 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004670 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004671 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004672 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004673
4674 /* Note: we rely on the backend to get the header name to be used for
4675 * x-forwarded-for, because the header is really meant for the backends.
4676 * However, if the backend did not specify any option, we have to rely
4677 * on the frontend's header name.
4678 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004679 if (s->be->fwdfor_hdr_len) {
4680 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004681 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004682 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004683 len = sess->fe->fwdfor_hdr_len;
4684 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004685 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004686 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 +01004687
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004688 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004689 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004690 }
4691 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004692 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004693 /* FIXME: for the sake of completeness, we should also support
4694 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004695 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004696 int len;
4697 char pn[INET6_ADDRSTRLEN];
4698 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004699 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004700 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004701
Willy Tarreau59234e92008-11-30 23:51:27 +01004702 /* Note: we rely on the backend to get the header name to be used for
4703 * x-forwarded-for, because the header is really meant for the backends.
4704 * However, if the backend did not specify any option, we have to rely
4705 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004706 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004707 if (s->be->fwdfor_hdr_len) {
4708 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004709 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004710 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004711 len = sess->fe->fwdfor_hdr_len;
4712 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004713 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004714 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004715
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004716 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004717 goto return_bad_req;
4718 }
4719 }
4720
4721 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004722 * 10: add X-Original-To if either the frontend or the backend
4723 * asks for it.
4724 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004725 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004726
4727 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004728 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004729 /* Add an X-Original-To header unless the destination IP is
4730 * in the 'except' network range.
4731 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004732 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004733
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004734 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004735 ((!sess->fe->except_mask_to.s_addr ||
4736 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4737 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004738 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004739 (((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 +02004740 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004741 int len;
4742 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004743 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004744
4745 /* Note: we rely on the backend to get the header name to be used for
4746 * x-original-to, because the header is really meant for the backends.
4747 * However, if the backend did not specify any option, we have to rely
4748 * on the frontend's header name.
4749 */
4750 if (s->be->orgto_hdr_len) {
4751 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004752 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004753 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004754 len = sess->fe->orgto_hdr_len;
4755 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004756 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004757 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 +02004758
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004759 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004760 goto return_bad_req;
4761 }
4762 }
4763 }
4764
Willy Tarreau50fc7772012-11-11 22:19:57 +01004765 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4766 * If an "Upgrade" token is found, the header is left untouched in order not to have
4767 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4768 * "Upgrade" is present in the Connection header.
4769 */
4770 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4771 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004772 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004773 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004774 unsigned int want_flags = 0;
4775
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004776 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004777 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004778 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004779 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004780 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004781 want_flags |= TX_CON_CLO_SET;
4782 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004783 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004784 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004785 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004786 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004787 want_flags |= TX_CON_KAL_SET;
4788 }
4789
4790 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004791 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004792 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004793
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004794
Willy Tarreau522d6c02009-12-06 18:49:18 +01004795 /* If we have no server assigned yet and we're balancing on url_param
4796 * with a POST request, we may be interested in checking the body for
4797 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004798 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004799 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004800 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004801 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004802 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004803 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004804 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004805
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004806 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004807 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004808#ifdef TCP_QUICKACK
4809 /* We expect some data from the client. Unless we know for sure
4810 * we already have a full request, we have to re-enable quick-ack
4811 * in case we previously disabled it, otherwise we might cause
4812 * the client to delay further data.
4813 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004814 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004815 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004816 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004817 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004818 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004819#endif
4820 }
Willy Tarreau03945942009-12-22 16:50:27 +01004821
Willy Tarreau59234e92008-11-30 23:51:27 +01004822 /*************************************************************
4823 * OK, that's finished for the headers. We have done what we *
4824 * could. Let's switch to the DATA state. *
4825 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004826 req->analyse_exp = TICK_ETERNITY;
4827 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004828
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004829 /* if the server closes the connection, we want to immediately react
4830 * and close the socket to save packets and syscalls.
4831 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004832 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004833 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004834
Willy Tarreau59234e92008-11-30 23:51:27 +01004835 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004836 /* OK let's go on with the BODY now */
4837 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004838
Willy Tarreau59234e92008-11-30 23:51:27 +01004839 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004840 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004841 /* we detected a parsing error. We want to archive this request
4842 * in the dedicated proxy area for later troubleshooting.
4843 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004844 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004845 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004846
Willy Tarreau59234e92008-11-30 23:51:27 +01004847 txn->req.msg_state = HTTP_MSG_ERROR;
4848 txn->status = 400;
4849 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004850 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004851
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004852 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004853 if (sess->listener->counters)
4854 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004855
Willy Tarreaue7dff022015-04-03 01:14:29 +02004856 if (!(s->flags & SF_ERR_MASK))
4857 s->flags |= SF_ERR_PRXCOND;
4858 if (!(s->flags & SF_FINST_MASK))
4859 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004860 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004861}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004862
Willy Tarreau60b85b02008-11-30 23:28:40 +01004863/* This function is an analyser which processes the HTTP tarpit. It always
4864 * returns zero, at the beginning because it prevents any other processing
4865 * from occurring, and at the end because it terminates the request.
4866 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004867int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004868{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004869 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004870
4871 /* This connection is being tarpitted. The CLIENT side has
4872 * already set the connect expiration date to the right
4873 * timeout. We just have to check that the client is still
4874 * there and that the timeout has not expired.
4875 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004876 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004877 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004878 !tick_is_expired(req->analyse_exp, now_ms))
4879 return 0;
4880
4881 /* We will set the queue timer to the time spent, just for
4882 * logging purposes. We fake a 500 server error, so that the
4883 * attacker will not suspect his connection has been tarpitted.
4884 * It will not cause trouble to the logs because we can exclude
4885 * the tarpitted connections by filtering on the 'PT' status flags.
4886 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004887 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4888
4889 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004890 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004891 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004892
4893 req->analysers = 0;
4894 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004895
Willy Tarreaue7dff022015-04-03 01:14:29 +02004896 if (!(s->flags & SF_ERR_MASK))
4897 s->flags |= SF_ERR_PRXCOND;
4898 if (!(s->flags & SF_FINST_MASK))
4899 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004900 return 0;
4901}
4902
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004903/* This function is an analyser which waits for the HTTP request body. It waits
4904 * for either the buffer to be full, or the full advertised contents to have
4905 * reached the buffer. It must only be called after the standard HTTP request
4906 * processing has occurred, because it expects the request to be parsed and will
4907 * look for the Expect header. It may send a 100-Continue interim response. It
4908 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4909 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4910 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004911 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004912int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004913{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004914 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004915 struct http_txn *txn = s->txn;
4916 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004917
4918 /* We have to parse the HTTP request body to find any required data.
4919 * "balance url_param check_post" should have been the only way to get
4920 * into this. We were brought here after HTTP header analysis, so all
4921 * related structures are ready.
4922 */
4923
Willy Tarreau890988f2014-04-10 11:59:33 +02004924 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4925 /* This is the first call */
4926 if (msg->msg_state < HTTP_MSG_BODY)
4927 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004928
Willy Tarreau890988f2014-04-10 11:59:33 +02004929 if (msg->msg_state < HTTP_MSG_100_SENT) {
4930 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4931 * send an HTTP/1.1 100 Continue intermediate response.
4932 */
4933 if (msg->flags & HTTP_MSGF_VER_11) {
4934 struct hdr_ctx ctx;
4935 ctx.idx = 0;
4936 /* Expect is allowed in 1.1, look for it */
4937 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4938 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004939 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004940 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004941 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004942 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004943 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004944
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004945 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004946 * req->buf->p still points to the beginning of the message. We
4947 * must save the body in msg->next because it survives buffer
4948 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004949 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004950 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004951
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004952 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004953 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4954 else
4955 msg->msg_state = HTTP_MSG_DATA;
4956 }
4957
Willy Tarreau890988f2014-04-10 11:59:33 +02004958 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4959 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004960 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004961 goto missing_data;
4962
4963 /* OK we have everything we need now */
4964 goto http_end;
4965 }
4966
4967 /* OK here we're parsing a chunked-encoded message */
4968
Willy Tarreau522d6c02009-12-06 18:49:18 +01004969 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004970 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004971 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004972 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004973 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004974 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004975
Willy Tarreau115acb92009-12-26 13:56:06 +01004976 if (!ret)
4977 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004978 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004979 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004980 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004981 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004982 }
4983
Willy Tarreaud98cf932009-12-27 22:54:55 +01004984 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004985 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4986 * for at least a whole chunk or the whole content length bytes after
4987 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004988 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004989 if (msg->msg_state == HTTP_MSG_TRAILERS)
4990 goto http_end;
4991
Willy Tarreaue115b492015-05-01 23:05:14 +02004992 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004993 goto http_end;
4994
4995 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004996 /* we get here if we need to wait for more data. If the buffer is full,
4997 * we have the maximum we can expect.
4998 */
4999 if (buffer_full(req->buf, global.tune.maxrewrite))
5000 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01005001
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005002 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01005003 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01005004 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02005005
Willy Tarreaue7dff022015-04-03 01:14:29 +02005006 if (!(s->flags & SF_ERR_MASK))
5007 s->flags |= SF_ERR_CLITO;
5008 if (!(s->flags & SF_FINST_MASK))
5009 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005010 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005011 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005012
5013 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005014 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005015 /* Not enough data. We'll re-use the http-request
5016 * timeout here. Ideally, we should set the timeout
5017 * relative to the accept() date. We just set the
5018 * request timeout once at the beginning of the
5019 * request.
5020 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005021 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005022 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005023 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005024 return 0;
5025 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005026
5027 http_end:
5028 /* The situation will not evolve, so let's give up on the analysis. */
5029 s->logs.tv_request = now; /* update the request timer to reflect full request */
5030 req->analysers &= ~an_bit;
5031 req->analyse_exp = TICK_ETERNITY;
5032 return 1;
5033
5034 return_bad_req: /* let's centralize all bad requests */
5035 txn->req.msg_state = HTTP_MSG_ERROR;
5036 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005037 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005038
Willy Tarreaue7dff022015-04-03 01:14:29 +02005039 if (!(s->flags & SF_ERR_MASK))
5040 s->flags |= SF_ERR_PRXCOND;
5041 if (!(s->flags & SF_FINST_MASK))
5042 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005043
Willy Tarreau522d6c02009-12-06 18:49:18 +01005044 return_err_msg:
5045 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005046 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005047 if (sess->listener->counters)
5048 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005049 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005050}
5051
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005052/* send a server's name with an outgoing request over an established connection.
5053 * Note: this function is designed to be called once the request has been scheduled
5054 * for being forwarded. This is the reason why it rewinds the buffer before
5055 * proceeding.
5056 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005057int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005058
5059 struct hdr_ctx ctx;
5060
Mark Lamourinec2247f02012-01-04 13:02:01 -05005061 char *hdr_name = be->server_id_hdr_name;
5062 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005063 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005064 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005065 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005066
William Lallemandd9e90662012-01-30 17:27:17 +01005067 ctx.idx = 0;
5068
Willy Tarreau211cdec2014-04-17 20:18:08 +02005069 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005070 if (old_o) {
5071 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005072 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005073 txn->req.next += old_o;
5074 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005075 }
5076
Willy Tarreau9b28e032012-10-12 23:49:43 +02005077 old_i = chn->buf->i;
5078 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 -05005079 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005080 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005081 }
5082
5083 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005084 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005085 memcpy(hdr_val, hdr_name, hdr_name_len);
5086 hdr_val += hdr_name_len;
5087 *hdr_val++ = ':';
5088 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005089 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5090 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005091
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005092 if (old_o) {
5093 /* If this was a forwarded request, we must readjust the amount of
5094 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005095 * variations. Note that the current state is >= HTTP_MSG_BODY,
5096 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005097 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005098 old_o += chn->buf->i - old_i;
5099 b_adv(chn->buf, old_o);
5100 txn->req.next -= old_o;
5101 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005102 }
5103
Mark Lamourinec2247f02012-01-04 13:02:01 -05005104 return 0;
5105}
5106
Willy Tarreau610ecce2010-01-04 21:15:02 +01005107/* Terminate current transaction and prepare a new one. This is very tricky
5108 * right now but it works.
5109 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005110void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005111{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005112 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005113 struct proxy *fe = strm_fe(s);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005114 struct connection *srv_conn;
5115 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005116 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005117
Willy Tarreau610ecce2010-01-04 21:15:02 +01005118 /* FIXME: We need a more portable way of releasing a backend's and a
5119 * server's connections. We need a safer way to reinitialize buffer
5120 * flags. We also need a more accurate method for computing per-request
5121 * data.
5122 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005123 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005124
Willy Tarreau4213a112013-12-15 10:25:42 +01005125 /* unless we're doing keep-alive, we want to quickly close the connection
5126 * to the server.
5127 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005128 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005129 !si_conn_ready(&s->si[1])) {
5130 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5131 si_shutr(&s->si[1]);
5132 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005133 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005134
Willy Tarreaue7dff022015-04-03 01:14:29 +02005135 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005136 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005137 if (unlikely(s->srv_conn))
5138 sess_change_server(s, NULL);
5139 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005140
5141 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005142 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005143
Willy Tarreaueee5b512015-04-03 23:46:31 +02005144 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005145 int n;
5146
Willy Tarreaueee5b512015-04-03 23:46:31 +02005147 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005148 if (n < 1 || n > 5)
5149 n = 0;
5150
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005151 if (fe->mode == PR_MODE_HTTP) {
5152 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005153 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005154 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005155 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005156 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005157 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005158 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005159 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005160 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005161 s->be->be_counters.p.http.comp_rsp++;
5162 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005163 }
5164
5165 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005166 s->logs.bytes_in -= s->req.buf->i;
5167 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005168
5169 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005170 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005171 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005172 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005173 s->do_log(s);
5174 }
5175
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005176 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005177 stream_stop_content_counters(s);
5178 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005179
Willy Tarreau610ecce2010-01-04 21:15:02 +01005180 s->logs.accept_date = date; /* user-visible date for logging */
5181 s->logs.tv_accept = now; /* corrected date for internal use */
5182 tv_zero(&s->logs.tv_request);
5183 s->logs.t_queue = -1;
5184 s->logs.t_connect = -1;
5185 s->logs.t_data = -1;
5186 s->logs.t_close = 0;
5187 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5188 s->logs.srv_queue_size = 0; /* we will get this number soon */
5189
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005190 s->logs.bytes_in = s->req.total = s->req.buf->i;
5191 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005192
5193 if (s->pend_pos)
5194 pendconn_free(s->pend_pos);
5195
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005196 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005197 if (s->flags & SF_CURR_SESS) {
5198 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005199 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005200 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005201 if (may_dequeue_tasks(objt_server(s->target), s->be))
5202 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005203 }
5204
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005205 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005206
Willy Tarreau4213a112013-12-15 10:25:42 +01005207 /* only release our endpoint if we don't intend to reuse the
5208 * connection.
5209 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005210 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005211 !si_conn_ready(&s->si[1])) {
5212 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005213 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005214 }
5215
Willy Tarreau350f4872014-11-28 14:42:25 +01005216 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5217 s->si[1].err_type = SI_ET_NONE;
5218 s->si[1].conn_retries = 0; /* used for logging too */
5219 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005220 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 +01005221 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);
5222 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 +02005223 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5224 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5225 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005226
Willy Tarreaueee5b512015-04-03 23:46:31 +02005227 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005228 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005229 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005230
5231 if (prev_status == 401 || prev_status == 407) {
5232 /* In HTTP keep-alive mode, if we receive a 401, we still have
5233 * a chance of being able to send the visitor again to the same
5234 * server over the same connection. This is required by some
5235 * broken protocols such as NTLM, and anyway whenever there is
5236 * an opportunity for sending the challenge to the proper place,
5237 * it's better to do it (at least it helps with debugging).
5238 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005239 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005240 if (srv_conn)
5241 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005242 }
5243
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005244 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005245 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005246
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005247 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005248 s->req.flags |= CF_NEVER_WAIT;
5249 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005250 }
5251
Willy Tarreau610ecce2010-01-04 21:15:02 +01005252 /* if the request buffer is not empty, it means we're
5253 * about to process another request, so send pending
5254 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005255 * Just don't do this if the buffer is close to be full,
5256 * because the request will wait for it to flush a little
5257 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005258 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005259 if (s->req.buf->i) {
5260 if (s->res.buf->o &&
5261 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5262 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5263 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005264 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005265
5266 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005267 channel_auto_read(&s->req);
5268 channel_auto_close(&s->req);
5269 channel_auto_read(&s->res);
5270 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005271
Willy Tarreau27375622013-12-17 00:00:28 +01005272 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005273 if (srv_conn) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005274 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005275 if (!srv)
5276 si_idle_conn(&s->si[1], NULL);
5277 else if ((srv_conn->flags & CO_FL_PRIVATE) ||
5278 ((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR))
5279 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005280 else if (prev_flags & TX_NOT_FIRST)
5281 /* note: we check the request, not the connection, but
5282 * this is valid for strategies SAFE and AGGR, and in
5283 * case of ALWS, we don't care anyway.
5284 */
5285 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005286 else
5287 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005288 }
Willy Tarreau27375622013-12-17 00:00:28 +01005289
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005290 s->req.analysers = strm_li(s)->analysers;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005291 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005292}
5293
5294
5295/* This function updates the request state machine according to the response
5296 * state machine and buffer flags. It returns 1 if it changes anything (flag
5297 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5298 * it is only used to find when a request/response couple is complete. Both
5299 * this function and its equivalent should loop until both return zero. It
5300 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5301 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005302int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005303{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005304 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005305 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005306 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005307 unsigned int old_state = txn->req.msg_state;
5308
Willy Tarreau610ecce2010-01-04 21:15:02 +01005309 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5310 return 0;
5311
5312 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005313 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005314 * We can shut the read side unless we want to abort_on_close,
5315 * or we have a POST request. The issue with POST requests is
5316 * that some browsers still send a CRLF after the request, and
5317 * this CRLF must be read so that it does not remain in the kernel
5318 * buffers, otherwise a close could cause an RST on some systems
5319 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005320 * Note that if we're using keep-alive on the client side, we'd
5321 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005322 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005323 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005324 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005325 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5326 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5327 !(s->be->options & PR_O_ABRT_CLOSE) &&
5328 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005329 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005330
Willy Tarreau40f151a2012-12-20 12:10:09 +01005331 /* if the server closes the connection, we want to immediately react
5332 * and close the socket to save packets and syscalls.
5333 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005334 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005335
Willy Tarreau610ecce2010-01-04 21:15:02 +01005336 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5337 goto wait_other_side;
5338
5339 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5340 /* The server has not finished to respond, so we
5341 * don't want to move in order not to upset it.
5342 */
5343 goto wait_other_side;
5344 }
5345
5346 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5347 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005348 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005349 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005350 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005351 goto wait_other_side;
5352 }
5353
5354 /* When we get here, it means that both the request and the
5355 * response have finished receiving. Depending on the connection
5356 * mode, we'll have to wait for the last bytes to leave in either
5357 * direction, and sometimes for a close to be effective.
5358 */
5359
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005360 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5361 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005362 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5363 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005364 }
5365 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5366 /* Option forceclose is set, or either side wants to close,
5367 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005368 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005369 * once both states are CLOSED.
5370 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005371 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5372 channel_shutr_now(chn);
5373 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005374 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005375 }
5376 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005377 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5378 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005379 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005380 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5381 channel_auto_read(chn);
5382 txn->req.msg_state = HTTP_MSG_TUNNEL;
5383 chn->flags |= CF_NEVER_WAIT;
5384 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005385 }
5386
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005387 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005388 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005389 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005390
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005391 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005392 txn->req.msg_state = HTTP_MSG_CLOSING;
5393 goto http_msg_closing;
5394 }
5395 else {
5396 txn->req.msg_state = HTTP_MSG_CLOSED;
5397 goto http_msg_closed;
5398 }
5399 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005400 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005401 }
5402
5403 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5404 http_msg_closing:
5405 /* nothing else to forward, just waiting for the output buffer
5406 * to be empty and for the shutw_now to take effect.
5407 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005408 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005409 txn->req.msg_state = HTTP_MSG_CLOSED;
5410 goto http_msg_closed;
5411 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005412 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005413 txn->req.msg_state = HTTP_MSG_ERROR;
5414 goto wait_other_side;
5415 }
5416 }
5417
5418 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5419 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005420 /* see above in MSG_DONE why we only do this in these states */
5421 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5422 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5423 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005424 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005425 goto wait_other_side;
5426 }
5427
5428 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005429 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005430}
5431
5432
5433/* This function updates the response state machine according to the request
5434 * state machine and buffer flags. It returns 1 if it changes anything (flag
5435 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5436 * it is only used to find when a request/response couple is complete. Both
5437 * this function and its equivalent should loop until both return zero. It
5438 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5439 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005440int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005441{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005442 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005443 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005444 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005445 unsigned int old_state = txn->rsp.msg_state;
5446
Willy Tarreau610ecce2010-01-04 21:15:02 +01005447 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5448 return 0;
5449
5450 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5451 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005452 * still monitor the server connection for a possible close
5453 * while the request is being uploaded, so we don't disable
5454 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005455 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005456 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005457
5458 if (txn->req.msg_state == HTTP_MSG_ERROR)
5459 goto wait_other_side;
5460
5461 if (txn->req.msg_state < HTTP_MSG_DONE) {
5462 /* The client seems to still be sending data, probably
5463 * because we got an error response during an upload.
5464 * We have the choice of either breaking the connection
5465 * or letting it pass through. Let's do the later.
5466 */
5467 goto wait_other_side;
5468 }
5469
5470 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5471 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005472 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005473 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005474 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005475 goto wait_other_side;
5476 }
5477
5478 /* When we get here, it means that both the request and the
5479 * response have finished receiving. Depending on the connection
5480 * mode, we'll have to wait for the last bytes to leave in either
5481 * direction, and sometimes for a close to be effective.
5482 */
5483
5484 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5485 /* Server-close mode : shut read and wait for the request
5486 * side to close its output buffer. The caller will detect
5487 * when we're in DONE and the other is in CLOSED and will
5488 * catch that for the final cleanup.
5489 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005490 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5491 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005492 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005493 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5494 /* Option forceclose is set, or either side wants to close,
5495 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005496 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005497 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005498 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005499 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5500 channel_shutr_now(chn);
5501 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005502 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005503 }
5504 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005505 /* The last possible modes are keep-alive and tunnel. Tunnel will
5506 * need to forward remaining data. Keep-alive will need to monitor
5507 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005508 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005509 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005510 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005511 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5512 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005513 }
5514
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005515 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005516 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005517 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005518 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5519 goto http_msg_closing;
5520 }
5521 else {
5522 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5523 goto http_msg_closed;
5524 }
5525 }
5526 goto wait_other_side;
5527 }
5528
5529 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5530 http_msg_closing:
5531 /* nothing else to forward, just waiting for the output buffer
5532 * to be empty and for the shutw_now to take effect.
5533 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005534 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005535 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5536 goto http_msg_closed;
5537 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005538 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005539 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005540 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005541 if (objt_server(s->target))
5542 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005543 goto wait_other_side;
5544 }
5545 }
5546
5547 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5548 http_msg_closed:
5549 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005550 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005551 channel_auto_close(chn);
5552 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005553 goto wait_other_side;
5554 }
5555
5556 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005557 /* We force the response to leave immediately if we're waiting for the
5558 * other side, since there is no pending shutdown to push it out.
5559 */
5560 if (!channel_is_empty(chn))
5561 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005562 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005563}
5564
5565
5566/* Resync the request and response state machines. Return 1 if either state
5567 * changes.
5568 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005569int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005570{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005571 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005572 int old_req_state = txn->req.msg_state;
5573 int old_res_state = txn->rsp.msg_state;
5574
Willy Tarreau610ecce2010-01-04 21:15:02 +01005575 http_sync_req_state(s);
5576 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005577 if (!http_sync_res_state(s))
5578 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005579 if (!http_sync_req_state(s))
5580 break;
5581 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005582
Willy Tarreau610ecce2010-01-04 21:15:02 +01005583 /* OK, both state machines agree on a compatible state.
5584 * There are a few cases we're interested in :
5585 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5586 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5587 * directions, so let's simply disable both analysers.
5588 * - HTTP_MSG_CLOSED on the response only means we must abort the
5589 * request.
5590 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5591 * with server-close mode means we've completed one request and we
5592 * must re-initialize the server connection.
5593 */
5594
5595 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5596 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5597 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5598 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005599 s->req.analysers = 0;
5600 channel_auto_close(&s->req);
5601 channel_auto_read(&s->req);
5602 s->res.analysers = 0;
5603 channel_auto_close(&s->res);
5604 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005605 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005606 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005607 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005608 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005609 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005610 s->res.analysers = 0;
5611 channel_auto_close(&s->res);
5612 channel_auto_read(&s->res);
5613 s->req.analysers = 0;
5614 channel_abort(&s->req);
5615 channel_auto_close(&s->req);
5616 channel_auto_read(&s->req);
5617 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005618 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005619 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5620 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005621 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005622 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5623 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5624 /* server-close/keep-alive: terminate this transaction,
5625 * possibly killing the server connection and reinitialize
5626 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005627 */
5628 http_end_txn_clean_session(s);
5629 }
5630
Willy Tarreau610ecce2010-01-04 21:15:02 +01005631 return txn->req.msg_state != old_req_state ||
5632 txn->rsp.msg_state != old_res_state;
5633}
5634
Willy Tarreaud98cf932009-12-27 22:54:55 +01005635/* This function is an analyser which forwards request body (including chunk
5636 * sizes if any). It is called as soon as we must forward, even if we forward
5637 * zero byte. The only situation where it must not be called is when we're in
5638 * tunnel mode and we want to forward till the close. It's used both to forward
5639 * remaining data and to resync after end of body. It expects the msg_state to
5640 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005641 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005642 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005643 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005644 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005645int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005646{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005647 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005648 struct http_txn *txn = s->txn;
5649 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005650
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005651 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5652 return 0;
5653
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005654 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005655 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005656 /* Output closed while we were sending data. We must abort and
5657 * wake the other side up.
5658 */
5659 msg->msg_state = HTTP_MSG_ERROR;
5660 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005661 return 1;
5662 }
5663
Willy Tarreaud98cf932009-12-27 22:54:55 +01005664 /* Note that we don't have to send 100-continue back because we don't
5665 * need the data to complete our job, and it's up to the server to
5666 * decide whether to return 100, 417 or anything else in return of
5667 * an "Expect: 100-continue" header.
5668 */
5669
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005670 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005671 /* we have msg->sov which points to the first byte of message
5672 * body, and req->buf.p still points to the beginning of the
5673 * message. We forward the headers now, as we don't need them
5674 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005675 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005676 b_adv(req->buf, msg->sov);
5677 msg->next -= msg->sov;
5678 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005679
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005680 /* The previous analysers guarantee that the state is somewhere
5681 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5682 * msg->next are always correct.
5683 */
5684 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5685 if (msg->flags & HTTP_MSGF_TE_CHNK)
5686 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5687 else
5688 msg->msg_state = HTTP_MSG_DATA;
5689 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005690 }
5691
Willy Tarreau7ba23542014-04-17 21:50:00 +02005692 /* Some post-connect processing might want us to refrain from starting to
5693 * forward data. Currently, the only reason for this is "balance url_param"
5694 * whichs need to parse/process the request after we've enabled forwarding.
5695 */
5696 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005697 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005698 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005699 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005700 goto missing_data;
5701 }
5702 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5703 }
5704
Willy Tarreau80a92c02014-03-12 10:41:13 +01005705 /* in most states, we should abort in case of early close */
5706 channel_auto_close(req);
5707
Willy Tarreauefdf0942014-04-24 20:08:57 +02005708 if (req->to_forward) {
5709 /* We can't process the buffer's contents yet */
5710 req->flags |= CF_WAKE_WRITE;
5711 goto missing_data;
5712 }
5713
Willy Tarreaud98cf932009-12-27 22:54:55 +01005714 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005715 if (msg->msg_state == HTTP_MSG_DATA) {
5716 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005717 /* we may have some pending data starting at req->buf->p */
5718 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005719 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005720 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005721 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005722 msg->next += msg->chunk_len;
5723 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005724
5725 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005726 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005727 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005728 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005729 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005730 }
5731 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005732 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005733 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005734 * TRAILERS state.
5735 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005736 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005737
Willy Tarreau54d23df2012-10-25 19:04:45 +02005738 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005739 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005740 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005741 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005742 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005743 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005744 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005745 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005746 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005747 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005748 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005749 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005750 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005751
5752 if (ret == 0)
5753 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005754 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005755 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005756 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005757 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005758 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005759 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005760 /* we're in MSG_CHUNK_SIZE now */
5761 }
5762 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005763 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005764
5765 if (ret == 0)
5766 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005767 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005768 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005769 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005770 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005771 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005772 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005773 /* we're in HTTP_MSG_DONE now */
5774 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005775 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005776 int old_state = msg->msg_state;
5777
Willy Tarreau610ecce2010-01-04 21:15:02 +01005778 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005779
5780 /* we may have some pending data starting at req->buf->p
5781 * such as last chunk of data or trailers.
5782 */
5783 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005784 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005785 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005786 msg->next = 0;
5787
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005788 /* we don't want to forward closes on DONE except in
5789 * tunnel mode.
5790 */
5791 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005792 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005793 if (http_resync_states(s)) {
5794 /* some state changes occurred, maybe the analyser
5795 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005796 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005797 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005798 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005799 /* request errors are most likely due to
5800 * the server aborting the transfer.
5801 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005802 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005803 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005804 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005805 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005806 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005807 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005808 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005809 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005810
5811 /* If "option abortonclose" is set on the backend, we
5812 * want to monitor the client's connection and forward
5813 * any shutdown notification to the server, which will
5814 * decide whether to close or to go on processing the
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005815 * request. We only do that in tunnel mode, and not in
5816 * other modes since it can be abused to exhaust source
5817 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005818 */
5819 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005820 channel_auto_read(req);
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005821 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5822 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5823 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005824 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005825 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005826 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005827 /* POST requests may require to read extra CRLF
5828 * sent by broken browsers and which could cause
5829 * an RST to be sent upon close on some systems
5830 * (eg: Linux).
5831 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005832 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005833 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005834
Willy Tarreau610ecce2010-01-04 21:15:02 +01005835 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005836 }
5837 }
5838
Willy Tarreaud98cf932009-12-27 22:54:55 +01005839 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005840 /* we may have some pending data starting at req->buf->p */
5841 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005842 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005843 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5844
Willy Tarreaubed410e2014-04-22 08:19:34 +02005845 msg->next = 0;
5846 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5847
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005848 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005849 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005850 if (!(s->flags & SF_ERR_MASK))
5851 s->flags |= SF_ERR_CLICL;
5852 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005853 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005854 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005855 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005856 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005857 }
5858
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005859 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005860 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005861 if (objt_server(s->target))
5862 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005863
5864 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005865 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005866
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005867 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005868 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005869 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005870
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005871 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005872 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005873 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005874 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005875 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005876
Willy Tarreau5c620922011-05-11 19:56:11 +02005877 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005878 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005879 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005880 * modes are already handled by the stream sock layer. We must not do
5881 * this in content-length mode because it could present the MSG_MORE
5882 * flag with the last block of forwarded data, which would cause an
5883 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005884 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005885 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005886 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005887
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005888 return 0;
5889
Willy Tarreaud98cf932009-12-27 22:54:55 +01005890 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005891 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005892 if (sess->listener->counters)
5893 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005894
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005895 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005896 /* we may have some pending data starting at req->buf->p */
5897 b_adv(req->buf, msg->next);
5898 msg->next = 0;
5899
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005900 txn->req.msg_state = HTTP_MSG_ERROR;
5901 if (txn->status) {
5902 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005903 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005904 } else {
5905 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005906 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005907 }
5908 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005909 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005910
Willy Tarreaue7dff022015-04-03 01:14:29 +02005911 if (!(s->flags & SF_ERR_MASK))
5912 s->flags |= SF_ERR_PRXCOND;
5913 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005914 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005915 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005916 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005917 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005918 }
5919 return 0;
5920
5921 aborted_xfer:
5922 txn->req.msg_state = HTTP_MSG_ERROR;
5923 if (txn->status) {
5924 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005925 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005926 } else {
5927 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005928 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005929 }
5930 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005931 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005932
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005933 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005934 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005935 if (objt_server(s->target))
5936 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005937
Willy Tarreaue7dff022015-04-03 01:14:29 +02005938 if (!(s->flags & SF_ERR_MASK))
5939 s->flags |= SF_ERR_SRVCL;
5940 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005941 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005942 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005943 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005944 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005945 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005946 return 0;
5947}
5948
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005949/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5950 * processing can continue on next analysers, or zero if it either needs more
5951 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005952 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005953 * when it has nothing left to do, and may remove any analyser when it wants to
5954 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005955 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005956int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005957{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005958 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005959 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005960 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005961 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005962 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005963 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005964 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005965
Willy Tarreau87b09662015-04-03 00:22:06 +02005966 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 +02005967 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005968 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005969 rep,
5970 rep->rex, rep->wex,
5971 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005972 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005973 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005974
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005975 /*
5976 * Now parse the partial (or complete) lines.
5977 * We will check the response syntax, and also join multi-line
5978 * headers. An index of all the lines will be elaborated while
5979 * parsing.
5980 *
5981 * For the parsing, we use a 28 states FSM.
5982 *
5983 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005984 * rep->buf->p = beginning of response
5985 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5986 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005987 * msg->eol = end of current header or line (LF or CRLF)
5988 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005989 */
5990
Willy Tarreau628c40c2014-04-24 19:11:26 +02005991 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005992 /* There's a protected area at the end of the buffer for rewriting
5993 * purposes. We don't want to start to parse the request if the
5994 * protected area is affected, because we may have to move processed
5995 * data later, which is much more complicated.
5996 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005997 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005998 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005999 /* some data has still not left the buffer, wake us once that's done */
6000 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
6001 goto abort_response;
6002 channel_dont_close(rep);
6003 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01006004 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02006005 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01006006 }
6007
Willy Tarreau379357a2013-06-08 12:55:46 +02006008 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
6009 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
6010 buffer_slow_realign(rep->buf);
6011
Willy Tarreau9b28e032012-10-12 23:49:43 +02006012 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01006013 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01006014 }
6015
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006016 /* 1: we might have to print this header in debug mode */
6017 if (unlikely((global.mode & MODE_DEBUG) &&
6018 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02006019 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006020 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006021
Willy Tarreau9b28e032012-10-12 23:49:43 +02006022 sol = rep->buf->p;
6023 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006024 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006025
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006026 sol += hdr_idx_first_pos(&txn->hdr_idx);
6027 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006028
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006029 while (cur_idx) {
6030 eol = sol + txn->hdr_idx.v[cur_idx].len;
6031 debug_hdr("srvhdr", s, sol, eol);
6032 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
6033 cur_idx = txn->hdr_idx.v[cur_idx].next;
6034 }
6035 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006036
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006037 /*
6038 * Now we quickly check if we have found a full valid response.
6039 * If not so, we check the FD and buffer states before leaving.
6040 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01006041 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006042 * responses are checked first.
6043 *
6044 * Depending on whether the client is still there or not, we
6045 * may send an error response back or not. Note that normally
6046 * we should only check for HTTP status there, and check I/O
6047 * errors somewhere else.
6048 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006049
Willy Tarreau655dce92009-11-08 13:10:58 +01006050 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006051 /* Invalid response */
6052 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6053 /* we detected a parsing error. We want to archive this response
6054 * in the dedicated proxy area for later troubleshooting.
6055 */
6056 hdr_response_bad:
6057 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006058 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006059
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006060 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006061 if (objt_server(s->target)) {
6062 objt_server(s->target)->counters.failed_resp++;
6063 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006064 }
Willy Tarreau64648412010-03-05 10:41:54 +01006065 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006066 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006067 rep->analysers = 0;
6068 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006069 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006070 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006071 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006072
Willy Tarreaue7dff022015-04-03 01:14:29 +02006073 if (!(s->flags & SF_ERR_MASK))
6074 s->flags |= SF_ERR_PRXCOND;
6075 if (!(s->flags & SF_FINST_MASK))
6076 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006077
6078 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006079 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006080
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006081 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006082 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006083 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006084 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006085 goto hdr_response_bad;
6086 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006087
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006088 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006089 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006090 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006091 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006092 else if (txn->flags & TX_NOT_FIRST)
6093 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006094
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006095 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006096 if (objt_server(s->target)) {
6097 objt_server(s->target)->counters.failed_resp++;
6098 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006099 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006100
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006101 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006102 rep->analysers = 0;
6103 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006104 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006105 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006106 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006107
Willy Tarreaue7dff022015-04-03 01:14:29 +02006108 if (!(s->flags & SF_ERR_MASK))
6109 s->flags |= SF_ERR_SRVCL;
6110 if (!(s->flags & SF_FINST_MASK))
6111 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006112 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006113 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006114
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006115 /* read timeout : return a 504 to the client. */
6116 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006117 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006118 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006119 else if (txn->flags & TX_NOT_FIRST)
6120 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006121
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006122 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006123 if (objt_server(s->target)) {
6124 objt_server(s->target)->counters.failed_resp++;
6125 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006126 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006127
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006128 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006129 rep->analysers = 0;
6130 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006131 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006132 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006133 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006134
Willy Tarreaue7dff022015-04-03 01:14:29 +02006135 if (!(s->flags & SF_ERR_MASK))
6136 s->flags |= SF_ERR_SRVTO;
6137 if (!(s->flags & SF_FINST_MASK))
6138 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006139 return 0;
6140 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006141
Willy Tarreauf003d372012-11-26 13:35:37 +01006142 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006143 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006144 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006145 s->be->be_counters.cli_aborts++;
6146 if (objt_server(s->target))
6147 objt_server(s->target)->counters.cli_aborts++;
6148
6149 rep->analysers = 0;
6150 channel_auto_close(rep);
6151
6152 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006153 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006154 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006155
Willy Tarreaue7dff022015-04-03 01:14:29 +02006156 if (!(s->flags & SF_ERR_MASK))
6157 s->flags |= SF_ERR_CLICL;
6158 if (!(s->flags & SF_FINST_MASK))
6159 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006160
Willy Tarreau87b09662015-04-03 00:22:06 +02006161 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006162 return 0;
6163 }
6164
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006165 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006166 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006167 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006168 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006169 else if (txn->flags & TX_NOT_FIRST)
6170 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006171
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006172 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006173 if (objt_server(s->target)) {
6174 objt_server(s->target)->counters.failed_resp++;
6175 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006176 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006177
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006178 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006179 rep->analysers = 0;
6180 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006181 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006182 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006183 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006184
Willy Tarreaue7dff022015-04-03 01:14:29 +02006185 if (!(s->flags & SF_ERR_MASK))
6186 s->flags |= SF_ERR_SRVCL;
6187 if (!(s->flags & SF_FINST_MASK))
6188 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006189 return 0;
6190 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006191
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006192 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006193 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006194 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006195 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006196 else if (txn->flags & TX_NOT_FIRST)
6197 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006198
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006199 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006200 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006201 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006202
Willy Tarreaue7dff022015-04-03 01:14:29 +02006203 if (!(s->flags & SF_ERR_MASK))
6204 s->flags |= SF_ERR_CLICL;
6205 if (!(s->flags & SF_FINST_MASK))
6206 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006207
Willy Tarreau87b09662015-04-03 00:22:06 +02006208 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006209 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006210 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006211
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006212 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006213 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006214 return 0;
6215 }
6216
6217 /* More interesting part now : we know that we have a complete
6218 * response which at least looks like HTTP. We have an indicator
6219 * of each header's length, so we can parse them quickly.
6220 */
6221
6222 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006223 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006224
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006225 /*
6226 * 1: get the status code
6227 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006228 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006229 if (n < 1 || n > 5)
6230 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006231 /* when the client triggers a 4xx from the server, it's most often due
6232 * to a missing object or permission. These events should be tracked
6233 * because if they happen often, it may indicate a brute force or a
6234 * vulnerability scan.
6235 */
6236 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006237 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006238
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006239 if (objt_server(s->target))
6240 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006241
Willy Tarreau91852eb2015-05-01 13:26:00 +02006242 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6243 * exactly one digit "." one digit. This check may be disabled using
6244 * option accept-invalid-http-response.
6245 */
6246 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6247 if (msg->sl.st.v_l != 8) {
6248 msg->err_pos = 0;
6249 goto hdr_response_bad;
6250 }
6251
6252 if (rep->buf->p[4] != '/' ||
6253 !isdigit((unsigned char)rep->buf->p[5]) ||
6254 rep->buf->p[6] != '.' ||
6255 !isdigit((unsigned char)rep->buf->p[7])) {
6256 msg->err_pos = 4;
6257 goto hdr_response_bad;
6258 }
6259 }
6260
Willy Tarreau5b154472009-12-21 20:11:07 +01006261 /* check if the response is HTTP/1.1 or above */
6262 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006263 ((rep->buf->p[5] > '1') ||
6264 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006265 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006266
6267 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006268 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 +01006269
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006270 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006271 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006272
Willy Tarreau9b28e032012-10-12 23:49:43 +02006273 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006274
Willy Tarreau39650402010-03-15 19:44:39 +01006275 /* Adjust server's health based on status code. Note: status codes 501
6276 * and 505 are triggered on demand by client request, so we must not
6277 * count them as server failures.
6278 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006279 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006280 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006281 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006282 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006283 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006284 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006285
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006286 /*
6287 * 2: check for cacheability.
6288 */
6289
6290 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006291 case 100:
6292 /*
6293 * We may be facing a 100-continue response, in which case this
6294 * is not the right response, and we're waiting for the next one.
6295 * Let's allow this response to go to the client and wait for the
6296 * next one.
6297 */
6298 hdr_idx_init(&txn->hdr_idx);
6299 msg->next -= channel_forward(rep, msg->next);
6300 msg->msg_state = HTTP_MSG_RPBEFORE;
6301 txn->status = 0;
6302 s->logs.t_data = -1; /* was not a response yet */
6303 goto next_one;
6304
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006305 case 200:
6306 case 203:
6307 case 206:
6308 case 300:
6309 case 301:
6310 case 410:
6311 /* RFC2616 @13.4:
6312 * "A response received with a status code of
6313 * 200, 203, 206, 300, 301 or 410 MAY be stored
6314 * by a cache (...) unless a cache-control
6315 * directive prohibits caching."
6316 *
6317 * RFC2616 @9.5: POST method :
6318 * "Responses to this method are not cacheable,
6319 * unless the response includes appropriate
6320 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006321 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006322 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006323 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006324 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6325 break;
6326 default:
6327 break;
6328 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006329
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006330 /*
6331 * 3: we may need to capture headers
6332 */
6333 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006334 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006335 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006336 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006337
Willy Tarreau557f1992015-05-01 10:05:17 +02006338 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6339 * by RFC7230#3.3.3 :
6340 *
6341 * The length of a message body is determined by one of the following
6342 * (in order of precedence):
6343 *
6344 * 1. Any response to a HEAD request and any response with a 1xx
6345 * (Informational), 204 (No Content), or 304 (Not Modified) status
6346 * code is always terminated by the first empty line after the
6347 * header fields, regardless of the header fields present in the
6348 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006349 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006350 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6351 * the connection will become a tunnel immediately after the empty
6352 * line that concludes the header fields. A client MUST ignore any
6353 * Content-Length or Transfer-Encoding header fields received in
6354 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006355 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006356 * 3. If a Transfer-Encoding header field is present and the chunked
6357 * transfer coding (Section 4.1) is the final encoding, the message
6358 * body length is determined by reading and decoding the chunked
6359 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006360 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006361 * If a Transfer-Encoding header field is present in a response and
6362 * the chunked transfer coding is not the final encoding, the
6363 * message body length is determined by reading the connection until
6364 * it is closed by the server. If a Transfer-Encoding header field
6365 * is present in a request and the chunked transfer coding is not
6366 * the final encoding, the message body length cannot be determined
6367 * reliably; the server MUST respond with the 400 (Bad Request)
6368 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006369 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006370 * If a message is received with both a Transfer-Encoding and a
6371 * Content-Length header field, the Transfer-Encoding overrides the
6372 * Content-Length. Such a message might indicate an attempt to
6373 * perform request smuggling (Section 9.5) or response splitting
6374 * (Section 9.4) and ought to be handled as an error. A sender MUST
6375 * remove the received Content-Length field prior to forwarding such
6376 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006377 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006378 * 4. If a message is received without Transfer-Encoding and with
6379 * either multiple Content-Length header fields having differing
6380 * field-values or a single Content-Length header field having an
6381 * invalid value, then the message framing is invalid and the
6382 * recipient MUST treat it as an unrecoverable error. If this is a
6383 * request message, the server MUST respond with a 400 (Bad Request)
6384 * status code and then close the connection. If this is a response
6385 * message received by a proxy, the proxy MUST close the connection
6386 * to the server, discard the received response, and send a 502 (Bad
6387 * Gateway) response to the client. If this is a response message
6388 * received by a user agent, the user agent MUST close the
6389 * connection to the server and discard the received response.
6390 *
6391 * 5. If a valid Content-Length header field is present without
6392 * Transfer-Encoding, its decimal value defines the expected message
6393 * body length in octets. If the sender closes the connection or
6394 * the recipient times out before the indicated number of octets are
6395 * received, the recipient MUST consider the message to be
6396 * incomplete and close the connection.
6397 *
6398 * 6. If this is a request message and none of the above are true, then
6399 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006400 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006401 * 7. Otherwise, this is a response message without a declared message
6402 * body length, so the message body length is determined by the
6403 * number of octets received prior to the server closing the
6404 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006405 */
6406
6407 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006408 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006409 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006410 * FIXME: should we parse anyway and return an error on chunked encoding ?
6411 */
6412 if (txn->meth == HTTP_METH_HEAD ||
6413 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006414 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006415 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006416 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006417 goto skip_content_length;
6418 }
6419
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006420 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006421 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006422 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006423 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006424 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6425 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006426 /* bad transfer-encoding (chunked followed by something else) */
6427 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006428 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006429 break;
6430 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006431 }
6432
Willy Tarreau1c913912015-04-30 10:57:51 +02006433 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006434 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006435 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006436 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6437 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6438 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006439 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006440 signed long long cl;
6441
Willy Tarreauad14f752011-09-02 20:33:27 +02006442 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006443 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006444 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006445 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006446
Willy Tarreauad14f752011-09-02 20:33:27 +02006447 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006448 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006449 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006450 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006451
Willy Tarreauad14f752011-09-02 20:33:27 +02006452 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006453 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006454 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006455 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006456
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006457 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006458 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006459 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006460 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006461
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006462 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006463 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006464 }
6465
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006466 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006467 select_compression_response_header(s, rep->buf);
6468
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006469skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006470 /* Now we have to check if we need to modify the Connection header.
6471 * This is more difficult on the response than it is on the request,
6472 * because we can have two different HTTP versions and we don't know
6473 * how the client will interprete a response. For instance, let's say
6474 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6475 * HTTP/1.1 response without any header. Maybe it will bound itself to
6476 * HTTP/1.0 because it only knows about it, and will consider the lack
6477 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6478 * the lack of header as a keep-alive. Thus we will use two flags
6479 * indicating how a request MAY be understood by the client. In case
6480 * of multiple possibilities, we'll fix the header to be explicit. If
6481 * ambiguous cases such as both close and keepalive are seen, then we
6482 * will fall back to explicit close. Note that we won't take risks with
6483 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006484 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006485 */
6486
Willy Tarreaudc008c52010-02-01 16:20:08 +01006487 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6488 txn->status == 101)) {
6489 /* Either we've established an explicit tunnel, or we're
6490 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006491 * to understand the next protocols. We have to switch to tunnel
6492 * mode, so that we transfer the request and responses then let
6493 * this protocol pass unmodified. When we later implement specific
6494 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006495 * header which contains information about that protocol for
6496 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006497 */
6498 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6499 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006500 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6501 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006502 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006503 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006504 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006505
Willy Tarreau70dffda2014-01-30 03:07:23 +01006506 /* this situation happens when combining pretend-keepalive with httpclose. */
6507 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006508 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006509 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006510 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6511
Willy Tarreau60466522010-01-18 19:08:45 +01006512 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006513 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006514 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6515 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006516
Willy Tarreau60466522010-01-18 19:08:45 +01006517 /* now adjust header transformations depending on current state */
6518 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6519 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6520 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006521 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006522 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006523 }
Willy Tarreau60466522010-01-18 19:08:45 +01006524 else { /* SCL / KAL */
6525 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006526 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006527 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006528 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006529
Willy Tarreau60466522010-01-18 19:08:45 +01006530 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006531 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006532
Willy Tarreau60466522010-01-18 19:08:45 +01006533 /* Some keep-alive responses are converted to Server-close if
6534 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006535 */
Willy Tarreau60466522010-01-18 19:08:45 +01006536 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6537 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006538 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006539 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006540 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006541 }
6542
Willy Tarreau7959a552013-09-23 16:44:27 +02006543 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006544 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006545
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006546 /* end of job, return OK */
6547 rep->analysers &= ~an_bit;
6548 rep->analyse_exp = TICK_ETERNITY;
6549 channel_auto_close(rep);
6550 return 1;
6551
6552 abort_keep_alive:
6553 /* A keep-alive request to the server failed on a network error.
6554 * The client is required to retry. We need to close without returning
6555 * any other information so that the client retries.
6556 */
6557 txn->status = 0;
6558 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006559 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006560 channel_auto_close(rep);
6561 s->logs.logwait = 0;
6562 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006563 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006564 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006565 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006566 return 0;
6567}
6568
6569/* This function performs all the processing enabled for the current response.
6570 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006571 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006572 * other functions. It works like process_request (see indications above).
6573 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006574int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006575{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006576 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006577 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006578 struct http_msg *msg = &txn->rsp;
6579 struct proxy *cur_proxy;
6580 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006581 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006582
Willy Tarreau87b09662015-04-03 00:22:06 +02006583 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 +02006584 now_ms, __FUNCTION__,
6585 s,
6586 rep,
6587 rep->rex, rep->wex,
6588 rep->flags,
6589 rep->buf->i,
6590 rep->analysers);
6591
6592 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6593 return 0;
6594
Willy Tarreau70730dd2014-04-24 18:06:27 +02006595 /* The stats applet needs to adjust the Connection header but we don't
6596 * apply any filter there.
6597 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006598 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6599 rep->analysers &= ~an_bit;
6600 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006601 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006602 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006603
Willy Tarreau58975672014-04-24 21:13:57 +02006604 /*
6605 * We will have to evaluate the filters.
6606 * As opposed to version 1.2, now they will be evaluated in the
6607 * filters order and not in the header order. This means that
6608 * each filter has to be validated among all headers.
6609 *
6610 * Filters are tried with ->be first, then with ->fe if it is
6611 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006612 *
6613 * Maybe we are in resume condiion. In this case I choose the
6614 * "struct proxy" which contains the rule list matching the resume
6615 * pointer. If none of theses "struct proxy" match, I initialise
6616 * the process with the first one.
6617 *
6618 * In fact, I check only correspondance betwwen the current list
6619 * pointer and the ->fe rule list. If it doesn't match, I initialize
6620 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006621 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006622 if (s->current_rule_list == &sess->fe->http_res_rules)
6623 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006624 else
6625 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006626 while (1) {
6627 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006628
Willy Tarreau58975672014-04-24 21:13:57 +02006629 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006630 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006631 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006632
Willy Tarreau51d861a2015-05-22 17:30:48 +02006633 if (ret == HTTP_RULE_RES_BADREQ)
6634 goto return_srv_prx_502;
6635
6636 if (ret == HTTP_RULE_RES_DONE) {
6637 rep->analysers &= ~an_bit;
6638 rep->analyse_exp = TICK_ETERNITY;
6639 return 1;
6640 }
6641 }
6642
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006643 /* we need to be called again. */
6644 if (ret == HTTP_RULE_RES_YIELD) {
6645 channel_dont_close(rep);
6646 return 0;
6647 }
6648
Willy Tarreau58975672014-04-24 21:13:57 +02006649 /* try headers filters */
6650 if (rule_set->rsp_exp != NULL) {
6651 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6652 return_bad_resp:
6653 if (objt_server(s->target)) {
6654 objt_server(s->target)->counters.failed_resp++;
6655 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006656 }
Willy Tarreau58975672014-04-24 21:13:57 +02006657 s->be->be_counters.failed_resp++;
6658 return_srv_prx_502:
6659 rep->analysers = 0;
6660 txn->status = 502;
6661 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006662 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006663 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006664 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006665 if (!(s->flags & SF_ERR_MASK))
6666 s->flags |= SF_ERR_PRXCOND;
6667 if (!(s->flags & SF_FINST_MASK))
6668 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006669 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006670 }
Willy Tarreau58975672014-04-24 21:13:57 +02006671 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006672
Willy Tarreau58975672014-04-24 21:13:57 +02006673 /* has the response been denied ? */
6674 if (txn->flags & TX_SVDENY) {
6675 if (objt_server(s->target))
6676 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006677
Willy Tarreau58975672014-04-24 21:13:57 +02006678 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006679 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006680 if (sess->listener->counters)
6681 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006682
Willy Tarreau58975672014-04-24 21:13:57 +02006683 goto return_srv_prx_502;
6684 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006685
Willy Tarreau58975672014-04-24 21:13:57 +02006686 /* add response headers from the rule sets in the same order */
6687 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006688 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006689 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006690 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006691 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006692 ret = acl_pass(ret);
6693 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6694 ret = !ret;
6695 if (!ret)
6696 continue;
6697 }
6698 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6699 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006700 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006701
Willy Tarreau58975672014-04-24 21:13:57 +02006702 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006703 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006704 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006705 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006706 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006707
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006708 /* After this point, this anayzer can't return yield, so we can
6709 * remove the bit corresponding to this analyzer from the list.
6710 *
6711 * Note that the intermediate returns and goto found previously
6712 * reset the analyzers.
6713 */
6714 rep->analysers &= ~an_bit;
6715 rep->analyse_exp = TICK_ETERNITY;
6716
Willy Tarreau58975672014-04-24 21:13:57 +02006717 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006718 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006719 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006720
Willy Tarreau58975672014-04-24 21:13:57 +02006721 /*
6722 * Now check for a server cookie.
6723 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006724 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006725 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006726
Willy Tarreau58975672014-04-24 21:13:57 +02006727 /*
6728 * Check for cache-control or pragma headers if required.
6729 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006730 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 +02006731 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006732
Willy Tarreau58975672014-04-24 21:13:57 +02006733 /*
6734 * Add server cookie in the response if needed
6735 */
6736 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6737 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006738 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006739 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6740 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6741 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6742 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6743 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006744 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006745 /* the server is known, it's not the one the client requested, or the
6746 * cookie's last seen date needs to be refreshed. We have to
6747 * insert a set-cookie here, except if we want to insert only on POST
6748 * requests and this one isn't. Note that servers which don't have cookies
6749 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006750 */
Willy Tarreau58975672014-04-24 21:13:57 +02006751 if (!objt_server(s->target)->cookie) {
6752 chunk_printf(&trash,
6753 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6754 s->be->cookie_name);
6755 }
6756 else {
6757 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006758
Willy Tarreau58975672014-04-24 21:13:57 +02006759 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6760 /* emit last_date, which is mandatory */
6761 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6762 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6763 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006764
Willy Tarreau58975672014-04-24 21:13:57 +02006765 if (s->be->cookie_maxlife) {
6766 /* emit first_date, which is either the original one or
6767 * the current date.
6768 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006769 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006770 s30tob64(txn->cookie_first_date ?
6771 txn->cookie_first_date >> 2 :
6772 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006773 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006774 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006775 }
Willy Tarreau58975672014-04-24 21:13:57 +02006776 chunk_appendf(&trash, "; path=/");
6777 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006778
Willy Tarreau58975672014-04-24 21:13:57 +02006779 if (s->be->cookie_domain)
6780 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006781
Willy Tarreau58975672014-04-24 21:13:57 +02006782 if (s->be->ck_opts & PR_CK_HTTPONLY)
6783 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006784
Willy Tarreau58975672014-04-24 21:13:57 +02006785 if (s->be->ck_opts & PR_CK_SECURE)
6786 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006787
Willy Tarreau58975672014-04-24 21:13:57 +02006788 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6789 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006790
Willy Tarreau58975672014-04-24 21:13:57 +02006791 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006792 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006793 /* the server did not change, only the date was updated */
6794 txn->flags |= TX_SCK_UPDATED;
6795 else
6796 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006797
Willy Tarreau58975672014-04-24 21:13:57 +02006798 /* Here, we will tell an eventual cache on the client side that we don't
6799 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6800 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6801 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006802 */
Willy Tarreau58975672014-04-24 21:13:57 +02006803 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006804
Willy Tarreau58975672014-04-24 21:13:57 +02006805 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006806
Willy Tarreau58975672014-04-24 21:13:57 +02006807 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6808 "Cache-control: private", 22) < 0))
6809 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006810 }
Willy Tarreau58975672014-04-24 21:13:57 +02006811 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006812
Willy Tarreau58975672014-04-24 21:13:57 +02006813 /*
6814 * Check if result will be cacheable with a cookie.
6815 * We'll block the response if security checks have caught
6816 * nasty things such as a cacheable cookie.
6817 */
6818 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6819 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6820 (s->be->options & PR_O_CHK_CACHE)) {
6821 /* we're in presence of a cacheable response containing
6822 * a set-cookie header. We'll block it as requested by
6823 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006824 */
Willy Tarreau58975672014-04-24 21:13:57 +02006825 if (objt_server(s->target))
6826 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006827
Willy Tarreau58975672014-04-24 21:13:57 +02006828 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006829 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006830 if (sess->listener->counters)
6831 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006832
Willy Tarreau58975672014-04-24 21:13:57 +02006833 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6834 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6835 send_log(s->be, LOG_ALERT,
6836 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6837 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6838 goto return_srv_prx_502;
6839 }
Willy Tarreau03945942009-12-22 16:50:27 +01006840
Willy Tarreau70730dd2014-04-24 18:06:27 +02006841 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006842 /*
6843 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6844 * If an "Upgrade" token is found, the header is left untouched in order
6845 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006846 * if anything but "Upgrade" is present in the Connection header. We don't
6847 * want to touch any 101 response either since it's switching to another
6848 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006849 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006850 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006851 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006852 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006853 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6854 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006855
Willy Tarreau58975672014-04-24 21:13:57 +02006856 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6857 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6858 /* we want a keep-alive response here. Keep-alive header
6859 * required if either side is not 1.1.
6860 */
6861 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6862 want_flags |= TX_CON_KAL_SET;
6863 }
6864 else {
6865 /* we want a close response here. Close header required if
6866 * the server is 1.1, regardless of the client.
6867 */
6868 if (msg->flags & HTTP_MSGF_VER_11)
6869 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006870 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006871
Willy Tarreau58975672014-04-24 21:13:57 +02006872 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6873 http_change_connection_header(txn, msg, want_flags);
6874 }
6875
6876 skip_header_mangling:
6877 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6878 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6879 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006880
Willy Tarreau58975672014-04-24 21:13:57 +02006881 /* if the user wants to log as soon as possible, without counting
6882 * bytes from the server, then this is the right moment. We have
6883 * to temporarily assign bytes_out to log what we currently have.
6884 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006885 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006886 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6887 s->logs.bytes_out = txn->rsp.eoh;
6888 s->do_log(s);
6889 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006890 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006891 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006892}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006893
Willy Tarreaud98cf932009-12-27 22:54:55 +01006894/* This function is an analyser which forwards response body (including chunk
6895 * sizes if any). It is called as soon as we must forward, even if we forward
6896 * zero byte. The only situation where it must not be called is when we're in
6897 * tunnel mode and we want to forward till the close. It's used both to forward
6898 * remaining data and to resync after end of body. It expects the msg_state to
6899 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006900 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006901 *
6902 * It is capable of compressing response data both in content-length mode and
6903 * in chunked mode. The state machines follows different flows depending on
6904 * whether content-length and chunked modes are used, since there are no
6905 * trailers in content-length :
6906 *
6907 * chk-mode cl-mode
6908 * ,----- BODY -----.
6909 * / \
6910 * V size > 0 V chk-mode
6911 * .--> SIZE -------------> DATA -------------> CRLF
6912 * | | size == 0 | last byte |
6913 * | v final crlf v inspected |
6914 * | TRAILERS -----------> DONE |
6915 * | |
6916 * `----------------------------------------------'
6917 *
6918 * Compression only happens in the DATA state, and must be flushed in final
6919 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6920 * is performed at once on final states for all bytes parsed, or when leaving
6921 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006922 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006923int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006924{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006925 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006926 struct http_txn *txn = s->txn;
6927 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006928 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006929 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006930 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006931
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006932 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6933 return 0;
6934
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006935 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006936 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006937 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006938 /* Output closed while we were sending data. We must abort and
6939 * wake the other side up.
6940 */
6941 msg->msg_state = HTTP_MSG_ERROR;
6942 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006943 return 1;
6944 }
6945
Willy Tarreau4fe41902010-06-07 22:27:41 +02006946 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006947 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006948
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006949 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006950 /* we have msg->sov which points to the first byte of message
6951 * body, and res->buf.p still points to the beginning of the
6952 * message. We forward the headers now, as we don't need them
6953 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006954 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006955 b_adv(res->buf, msg->sov);
6956 msg->next -= msg->sov;
6957 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006958
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006959 /* The previous analysers guarantee that the state is somewhere
6960 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6961 * msg->next are always correct.
6962 */
6963 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6964 if (msg->flags & HTTP_MSGF_TE_CHNK)
6965 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6966 else
6967 msg->msg_state = HTTP_MSG_DATA;
6968 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006969 }
6970
Willy Tarreauefdf0942014-04-24 20:08:57 +02006971 if (res->to_forward) {
6972 /* We can't process the buffer's contents yet */
6973 res->flags |= CF_WAKE_WRITE;
6974 goto missing_data;
6975 }
6976
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006977 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6978 /* We need a compression buffer in the DATA state to put the
6979 * output of compressed data, and in CRLF state to let the
6980 * TRAILERS state finish the job of removing the trailing CRLF.
6981 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006982 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006983 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006984 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006985 goto aborted_xfer; /* no memory */
6986 }
6987
6988 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006989 if (ret < 0) {
6990 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006991 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006992 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006993 compressing = 1;
6994 }
6995
Willy Tarreaud98cf932009-12-27 22:54:55 +01006996 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006997 switch (msg->msg_state - HTTP_MSG_DATA) {
6998 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006999 /* we may have some pending data starting at res->buf->p */
7000 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02007001 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01007002 if (ret < 0)
7003 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02007004
Willy Tarreaud5a67832014-04-21 10:54:27 +02007005 if (msg->chunk_len) {
7006 /* input empty or output full */
7007 if (res->buf->i > msg->next)
7008 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02007009 goto missing_data;
7010 }
William Lallemandbf3ae612012-11-19 12:35:37 +01007011 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007012 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02007013 if (msg->chunk_len > res->buf->i - msg->next) {
7014 /* output full */
7015 res->flags |= CF_WAKE_WRITE;
7016 goto missing_data;
7017 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007018 msg->next += msg->chunk_len;
7019 msg->chunk_len = 0;
7020 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01007021
7022 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01007023 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02007024 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01007025 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01007026 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007027 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01007028 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007029 /* fall through for HTTP_MSG_CHUNK_CRLF */
7030
7031 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
7032 /* we want the CRLF after the data */
7033
7034 ret = http_skip_chunk_crlf(msg);
7035 if (ret == 0)
7036 goto missing_data;
7037 else if (ret < 0) {
7038 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007039 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007040 goto return_bad_res;
7041 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007042 /* we're in MSG_CHUNK_SIZE now, fall through */
7043
7044 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01007045 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02007046 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01007047 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01007048 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007049
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007050 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02007051 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01007052 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007053 else if (ret < 0) {
7054 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007055 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007056 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007057 }
Willy Tarreau0161d622013-04-02 01:26:55 +02007058 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007059 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01007060
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007061 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02007062 if (unlikely(compressing)) {
7063 /* we need to flush output contents before syncing FSMs */
7064 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7065 compressing = 0;
7066 }
7067
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007068 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007069 if (ret == 0)
7070 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007071 else if (ret < 0) {
7072 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007073 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007074 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007075 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02007076 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007077
7078 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01007079 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007080 if (unlikely(compressing)) {
7081 /* we need to flush output contents before syncing FSMs */
7082 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7083 compressing = 0;
7084 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007085
Willy Tarreauc623c172014-04-18 09:53:50 +02007086 /* we may have some pending data starting at res->buf->p
7087 * such as a last chunk of data or trailers.
7088 */
7089 b_adv(res->buf, msg->next);
7090 msg->next = 0;
7091
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007092 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02007093 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007094 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7095 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007096 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02007097
Willy Tarreau610ecce2010-01-04 21:15:02 +01007098 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01007099 /* some state changes occurred, maybe the analyser
7100 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01007101 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007102 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007103 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007104 /* response errors are most likely due to
7105 * the client aborting the transfer.
7106 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007107 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007108 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007109 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007110 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01007111 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007112 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007113 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01007114 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007115 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007116 }
7117 }
7118
Willy Tarreaud98cf932009-12-27 22:54:55 +01007119 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02007120 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007121 if (unlikely(compressing)) {
7122 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02007123 compressing = 0;
7124 }
Willy Tarreauf003d372012-11-26 13:35:37 +01007125
Willy Tarreauc623c172014-04-18 09:53:50 +02007126 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
7127 b_adv(res->buf, msg->next);
7128 msg->next = 0;
7129 msg->chunk_len -= channel_forward(res, msg->chunk_len);
7130 }
7131
Willy Tarreauf003d372012-11-26 13:35:37 +01007132 if (res->flags & CF_SHUTW)
7133 goto aborted_xfer;
7134
7135 /* stop waiting for data if the input is closed before the end. If the
7136 * client side was already closed, it means that the client has aborted,
7137 * so we don't want to count this as a server abort. Otherwise it's a
7138 * server abort.
7139 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007140 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007141 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01007142 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007143 if (!(s->flags & SF_ERR_MASK))
7144 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007145 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007146 if (objt_server(s->target))
7147 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007148 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01007149 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01007150
Willy Tarreau40dba092010-03-04 18:14:51 +01007151 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007152 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01007153 goto return_bad_res;
7154
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007155 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007156 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007157 * Similarly, with keep-alive on the client side, we don't want to forward a
7158 * close.
7159 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007160 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007161 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7162 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007163 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007164
Willy Tarreau5c620922011-05-11 19:56:11 +02007165 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007166 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007167 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007168 * modes are already handled by the stream sock layer. We must not do
7169 * this in content-length mode because it could present the MSG_MORE
7170 * flag with the last block of forwarded data, which would cause an
7171 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007172 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007173 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007174 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007175
Willy Tarreau87b09662015-04-03 00:22:06 +02007176 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007177 return 0;
7178
Willy Tarreau40dba092010-03-04 18:14:51 +01007179 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007180 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007181 if (objt_server(s->target))
7182 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007183
7184 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02007185 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02007186 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02007187 compressing = 0;
7188 }
7189
Willy Tarreauc623c172014-04-18 09:53:50 +02007190 /* we may have some pending data starting at res->buf->p */
7191 if (s->comp_algo == NULL) {
7192 b_adv(res->buf, msg->next);
7193 msg->next = 0;
7194 }
7195
Willy Tarreaud98cf932009-12-27 22:54:55 +01007196 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007197 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007198 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007199 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007200 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007201 if (objt_server(s->target))
7202 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007203
Willy Tarreaue7dff022015-04-03 01:14:29 +02007204 if (!(s->flags & SF_ERR_MASK))
7205 s->flags |= SF_ERR_PRXCOND;
7206 if (!(s->flags & SF_FINST_MASK))
7207 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007208 return 0;
7209
7210 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02007211 if (unlikely(compressing)) {
7212 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
7213 compressing = 0;
7214 }
7215
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007216 txn->rsp.msg_state = HTTP_MSG_ERROR;
7217 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007218 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007219 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007220 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007221
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007222 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007223 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007224 if (objt_server(s->target))
7225 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007226
Willy Tarreaue7dff022015-04-03 01:14:29 +02007227 if (!(s->flags & SF_ERR_MASK))
7228 s->flags |= SF_ERR_CLICL;
7229 if (!(s->flags & SF_FINST_MASK))
7230 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007231 return 0;
7232}
7233
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007234/* Iterate the same filter through all request headers.
7235 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007236 * Since it can manage the switch to another backend, it updates the per-proxy
7237 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007238 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007239int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007240{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007241 char *cur_ptr, *cur_end, *cur_next;
7242 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007243 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007244 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007245 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007246
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007247 last_hdr = 0;
7248
Willy Tarreau9b28e032012-10-12 23:49:43 +02007249 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007250 old_idx = 0;
7251
7252 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007253 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007254 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007255 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007256 (exp->action == ACT_ALLOW ||
7257 exp->action == ACT_DENY ||
7258 exp->action == ACT_TARPIT))
7259 return 0;
7260
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007261 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007262 if (!cur_idx)
7263 break;
7264
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007265 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007266 cur_ptr = cur_next;
7267 cur_end = cur_ptr + cur_hdr->len;
7268 cur_next = cur_end + cur_hdr->cr + 1;
7269
7270 /* Now we have one header between cur_ptr and cur_end,
7271 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007272 */
7273
Willy Tarreau15a53a42015-01-21 13:39:42 +01007274 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007275 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007276 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007277 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007278 last_hdr = 1;
7279 break;
7280
7281 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007282 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007283 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007284 break;
7285
7286 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007287 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007288 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007289 break;
7290
7291 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007292 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7293 if (trash.len < 0)
7294 return -1;
7295
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007296 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007297 /* FIXME: if the user adds a newline in the replacement, the
7298 * index will not be recalculated for now, and the new line
7299 * will not be counted as a new header.
7300 */
7301
7302 cur_end += delta;
7303 cur_next += delta;
7304 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007305 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007306 break;
7307
7308 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007309 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007310 cur_next += delta;
7311
Willy Tarreaufa355d42009-11-29 18:12:29 +01007312 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007313 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7314 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007315 cur_hdr->len = 0;
7316 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007317 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007318 break;
7319
7320 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007321 }
7322
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007323 /* keep the link from this header to next one in case of later
7324 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007325 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007326 old_idx = cur_idx;
7327 }
7328 return 0;
7329}
7330
7331
7332/* Apply the filter to the request line.
7333 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7334 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007335 * Since it can manage the switch to another backend, it updates the per-proxy
7336 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007337 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007338int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007339{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007340 char *cur_ptr, *cur_end;
7341 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007342 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007343 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007344
Willy Tarreau3d300592007-03-18 18:34:41 +01007345 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007346 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007347 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007348 (exp->action == ACT_ALLOW ||
7349 exp->action == ACT_DENY ||
7350 exp->action == ACT_TARPIT))
7351 return 0;
7352 else if (exp->action == ACT_REMOVE)
7353 return 0;
7354
7355 done = 0;
7356
Willy Tarreau9b28e032012-10-12 23:49:43 +02007357 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007358 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007359
7360 /* Now we have the request line between cur_ptr and cur_end */
7361
Willy Tarreau15a53a42015-01-21 13:39:42 +01007362 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007363 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007364 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007365 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007366 done = 1;
7367 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007368
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007369 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007370 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007371 done = 1;
7372 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007373
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007374 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007375 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007376 done = 1;
7377 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007378
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007379 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007380 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7381 if (trash.len < 0)
7382 return -1;
7383
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007384 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007385 /* FIXME: if the user adds a newline in the replacement, the
7386 * index will not be recalculated for now, and the new line
7387 * will not be counted as a new header.
7388 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007389
Willy Tarreaufa355d42009-11-29 18:12:29 +01007390 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007391 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007392 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007393 HTTP_MSG_RQMETH,
7394 cur_ptr, cur_end + 1,
7395 NULL, NULL);
7396 if (unlikely(!cur_end))
7397 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007398
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007399 /* we have a full request and we know that we have either a CR
7400 * or an LF at <ptr>.
7401 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007402 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7403 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007404 /* there is no point trying this regex on headers */
7405 return 1;
7406 }
7407 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007408 return done;
7409}
Willy Tarreau97de6242006-12-27 17:18:38 +01007410
Willy Tarreau58f10d72006-12-04 02:26:12 +01007411
Willy Tarreau58f10d72006-12-04 02:26:12 +01007412
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007413/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007414 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007415 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007416 * unparsable request. Since it can manage the switch to another backend, it
7417 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007418 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007419int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007420{
Willy Tarreau192252e2015-04-04 01:47:55 +02007421 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007422 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007423 struct hdr_exp *exp;
7424
7425 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007426 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007427
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007428 /*
7429 * The interleaving of transformations and verdicts
7430 * makes it difficult to decide to continue or stop
7431 * the evaluation.
7432 */
7433
Willy Tarreau6c123b12010-01-28 20:22:06 +01007434 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7435 break;
7436
Willy Tarreau3d300592007-03-18 18:34:41 +01007437 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007438 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007439 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007440 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007441
7442 /* if this filter had a condition, evaluate it now and skip to
7443 * next filter if the condition does not match.
7444 */
7445 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007446 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007447 ret = acl_pass(ret);
7448 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7449 ret = !ret;
7450
7451 if (!ret)
7452 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007453 }
7454
7455 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007456 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007457 if (unlikely(ret < 0))
7458 return -1;
7459
7460 if (likely(ret == 0)) {
7461 /* The filter did not match the request, it can be
7462 * iterated through all headers.
7463 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007464 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7465 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007466 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007467 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007468 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007469}
7470
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007471
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007472/* Find the end of a cookie value contained between <s> and <e>. It works the
7473 * same way as with headers above except that the semi-colon also ends a token.
7474 * See RFC2965 for more information. Note that it requires a valid header to
7475 * return a valid result.
7476 */
7477char *find_cookie_value_end(char *s, const char *e)
7478{
7479 int quoted, qdpair;
7480
7481 quoted = qdpair = 0;
7482 for (; s < e; s++) {
7483 if (qdpair) qdpair = 0;
7484 else if (quoted) {
7485 if (*s == '\\') qdpair = 1;
7486 else if (*s == '"') quoted = 0;
7487 }
7488 else if (*s == '"') quoted = 1;
7489 else if (*s == ',' || *s == ';') return s;
7490 }
7491 return s;
7492}
7493
7494/* Delete a value in a header between delimiters <from> and <next> in buffer
7495 * <buf>. The number of characters displaced is returned, and the pointer to
7496 * the first delimiter is updated if required. The function tries as much as
7497 * possible to respect the following principles :
7498 * - replace <from> delimiter by the <next> one unless <from> points to a
7499 * colon, in which case <next> is simply removed
7500 * - set exactly one space character after the new first delimiter, unless
7501 * there are not enough characters in the block being moved to do so.
7502 * - remove unneeded spaces before the previous delimiter and after the new
7503 * one.
7504 *
7505 * It is the caller's responsibility to ensure that :
7506 * - <from> points to a valid delimiter or the colon ;
7507 * - <next> points to a valid delimiter or the final CR/LF ;
7508 * - there are non-space chars before <from> ;
7509 * - there is a CR/LF at or after <next>.
7510 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007511int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007512{
7513 char *prev = *from;
7514
7515 if (*prev == ':') {
7516 /* We're removing the first value, preserve the colon and add a
7517 * space if possible.
7518 */
7519 if (!http_is_crlf[(unsigned char)*next])
7520 next++;
7521 prev++;
7522 if (prev < next)
7523 *prev++ = ' ';
7524
7525 while (http_is_spht[(unsigned char)*next])
7526 next++;
7527 } else {
7528 /* Remove useless spaces before the old delimiter. */
7529 while (http_is_spht[(unsigned char)*(prev-1)])
7530 prev--;
7531 *from = prev;
7532
7533 /* copy the delimiter and if possible a space if we're
7534 * not at the end of the line.
7535 */
7536 if (!http_is_crlf[(unsigned char)*next]) {
7537 *prev++ = *next++;
7538 if (prev + 1 < next)
7539 *prev++ = ' ';
7540 while (http_is_spht[(unsigned char)*next])
7541 next++;
7542 }
7543 }
7544 return buffer_replace2(buf, prev, next, NULL, 0);
7545}
7546
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007547/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007548 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007549 * desirable to call it only when needed. This code is quite complex because
7550 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7551 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007552 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007553void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007554{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007555 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007556 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007557 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007558 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007559 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7560 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007561
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007562 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007563 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007564 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007565
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007566 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007567 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007568 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007569
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007570 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007571 hdr_beg = hdr_next;
7572 hdr_end = hdr_beg + cur_hdr->len;
7573 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007574
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007575 /* We have one full header between hdr_beg and hdr_end, and the
7576 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007577 * "Cookie:" headers.
7578 */
7579
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007580 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007581 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007582 old_idx = cur_idx;
7583 continue;
7584 }
7585
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007586 del_from = NULL; /* nothing to be deleted */
7587 preserve_hdr = 0; /* assume we may kill the whole header */
7588
Willy Tarreau58f10d72006-12-04 02:26:12 +01007589 /* Now look for cookies. Conforming to RFC2109, we have to support
7590 * attributes whose name begin with a '$', and associate them with
7591 * the right cookie, if we want to delete this cookie.
7592 * So there are 3 cases for each cookie read :
7593 * 1) it's a special attribute, beginning with a '$' : ignore it.
7594 * 2) it's a server id cookie that we *MAY* want to delete : save
7595 * some pointers on it (last semi-colon, beginning of cookie...)
7596 * 3) it's an application cookie : we *MAY* have to delete a previous
7597 * "special" cookie.
7598 * At the end of loop, if a "special" cookie remains, we may have to
7599 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007600 * *MUST* delete it.
7601 *
7602 * Note: RFC2965 is unclear about the processing of spaces around
7603 * the equal sign in the ATTR=VALUE form. A careful inspection of
7604 * the RFC explicitly allows spaces before it, and not within the
7605 * tokens (attrs or values). An inspection of RFC2109 allows that
7606 * too but section 10.1.3 lets one think that spaces may be allowed
7607 * after the equal sign too, resulting in some (rare) buggy
7608 * implementations trying to do that. So let's do what servers do.
7609 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7610 * allowed quoted strings in values, with any possible character
7611 * after a backslash, including control chars and delimitors, which
7612 * causes parsing to become ambiguous. Browsers also allow spaces
7613 * within values even without quotes.
7614 *
7615 * We have to keep multiple pointers in order to support cookie
7616 * removal at the beginning, middle or end of header without
7617 * corrupting the header. All of these headers are valid :
7618 *
7619 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7620 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7621 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7622 * | | | | | | | | |
7623 * | | | | | | | | hdr_end <--+
7624 * | | | | | | | +--> next
7625 * | | | | | | +----> val_end
7626 * | | | | | +-----------> val_beg
7627 * | | | | +--------------> equal
7628 * | | | +----------------> att_end
7629 * | | +---------------------> att_beg
7630 * | +--------------------------> prev
7631 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007632 */
7633
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007634 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7635 /* Iterate through all cookies on this line */
7636
7637 /* find att_beg */
7638 att_beg = prev + 1;
7639 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7640 att_beg++;
7641
7642 /* find att_end : this is the first character after the last non
7643 * space before the equal. It may be equal to hdr_end.
7644 */
7645 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007646
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007647 while (equal < hdr_end) {
7648 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007649 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007650 if (http_is_spht[(unsigned char)*equal++])
7651 continue;
7652 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007653 }
7654
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007655 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7656 * is between <att_beg> and <equal>, both may be identical.
7657 */
7658
7659 /* look for end of cookie if there is an equal sign */
7660 if (equal < hdr_end && *equal == '=') {
7661 /* look for the beginning of the value */
7662 val_beg = equal + 1;
7663 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7664 val_beg++;
7665
7666 /* find the end of the value, respecting quotes */
7667 next = find_cookie_value_end(val_beg, hdr_end);
7668
7669 /* make val_end point to the first white space or delimitor after the value */
7670 val_end = next;
7671 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7672 val_end--;
7673 } else {
7674 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007675 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007676
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007677 /* We have nothing to do with attributes beginning with '$'. However,
7678 * they will automatically be removed if a header before them is removed,
7679 * since they're supposed to be linked together.
7680 */
7681 if (*att_beg == '$')
7682 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007683
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007684 /* Ignore cookies with no equal sign */
7685 if (equal == next) {
7686 /* This is not our cookie, so we must preserve it. But if we already
7687 * scheduled another cookie for removal, we cannot remove the
7688 * complete header, but we can remove the previous block itself.
7689 */
7690 preserve_hdr = 1;
7691 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007692 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007693 val_end += delta;
7694 next += delta;
7695 hdr_end += delta;
7696 hdr_next += delta;
7697 cur_hdr->len += delta;
7698 http_msg_move_end(&txn->req, delta);
7699 prev = del_from;
7700 del_from = NULL;
7701 }
7702 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007703 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007704
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007705 /* if there are spaces around the equal sign, we need to
7706 * strip them otherwise we'll get trouble for cookie captures,
7707 * or even for rewrites. Since this happens extremely rarely,
7708 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007709 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007710 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7711 int stripped_before = 0;
7712 int stripped_after = 0;
7713
7714 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007715 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007716 equal += stripped_before;
7717 val_beg += stripped_before;
7718 }
7719
7720 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007721 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007722 val_beg += stripped_after;
7723 stripped_before += stripped_after;
7724 }
7725
7726 val_end += stripped_before;
7727 next += stripped_before;
7728 hdr_end += stripped_before;
7729 hdr_next += stripped_before;
7730 cur_hdr->len += stripped_before;
7731 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007732 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007733 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007734
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007735 /* First, let's see if we want to capture this cookie. We check
7736 * that we don't already have a client side cookie, because we
7737 * can only capture one. Also as an optimisation, we ignore
7738 * cookies shorter than the declared name.
7739 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007740 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7741 (val_end - att_beg >= sess->fe->capture_namelen) &&
7742 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007743 int log_len = val_end - att_beg;
7744
7745 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7746 Alert("HTTP logging : out of memory.\n");
7747 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007748 if (log_len > sess->fe->capture_len)
7749 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007750 memcpy(txn->cli_cookie, att_beg, log_len);
7751 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007752 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007753 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007754
Willy Tarreaubca99692010-10-06 19:25:55 +02007755 /* Persistence cookies in passive, rewrite or insert mode have the
7756 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007757 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007758 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007759 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007760 * For cookies in prefix mode, the form is :
7761 *
7762 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007763 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007764 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7765 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7766 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007767 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007768
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007769 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7770 * have the server ID between val_beg and delim, and the original cookie between
7771 * delim+1 and val_end. Otherwise, delim==val_end :
7772 *
7773 * Cookie: NAME=SRV; # in all but prefix modes
7774 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7775 * | || || | |+-> next
7776 * | || || | +--> val_end
7777 * | || || +---------> delim
7778 * | || |+------------> val_beg
7779 * | || +-------------> att_end = equal
7780 * | |+-----------------> att_beg
7781 * | +------------------> prev
7782 * +-------------------------> hdr_beg
7783 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007784
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007785 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007786 for (delim = val_beg; delim < val_end; delim++)
7787 if (*delim == COOKIE_DELIM)
7788 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007789 } else {
7790 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007791 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007792 /* Now check if the cookie contains a date field, which would
7793 * appear after a vertical bar ('|') just after the server name
7794 * and before the delimiter.
7795 */
7796 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7797 if (vbar1) {
7798 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007799 * right is the last seen date. It is a base64 encoded
7800 * 30-bit value representing the UNIX date since the
7801 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007802 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007803 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007804 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007805 if (val_end - vbar1 >= 5) {
7806 val = b64tos30(vbar1);
7807 if (val > 0)
7808 txn->cookie_last_date = val << 2;
7809 }
7810 /* look for a second vertical bar */
7811 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7812 if (vbar1 && (val_end - vbar1 > 5)) {
7813 val = b64tos30(vbar1 + 1);
7814 if (val > 0)
7815 txn->cookie_first_date = val << 2;
7816 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007817 }
7818 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007819
Willy Tarreauf64d1412010-10-07 20:06:11 +02007820 /* if the cookie has an expiration date and the proxy wants to check
7821 * it, then we do that now. We first check if the cookie is too old,
7822 * then only if it has expired. We detect strict overflow because the
7823 * time resolution here is not great (4 seconds). Cookies with dates
7824 * in the future are ignored if their offset is beyond one day. This
7825 * allows an admin to fix timezone issues without expiring everyone
7826 * and at the same time avoids keeping unwanted side effects for too
7827 * long.
7828 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007829 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7830 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007831 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007832 txn->flags &= ~TX_CK_MASK;
7833 txn->flags |= TX_CK_OLD;
7834 delim = val_beg; // let's pretend we have not found the cookie
7835 txn->cookie_first_date = 0;
7836 txn->cookie_last_date = 0;
7837 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007838 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7839 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007840 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007841 txn->flags &= ~TX_CK_MASK;
7842 txn->flags |= TX_CK_EXPIRED;
7843 delim = val_beg; // let's pretend we have not found the cookie
7844 txn->cookie_first_date = 0;
7845 txn->cookie_last_date = 0;
7846 }
7847
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007848 /* Here, we'll look for the first running server which supports the cookie.
7849 * This allows to share a same cookie between several servers, for example
7850 * to dedicate backup servers to specific servers only.
7851 * However, to prevent clients from sticking to cookie-less backup server
7852 * when they have incidentely learned an empty cookie, we simply ignore
7853 * empty cookies and mark them as invalid.
7854 * The same behaviour is applied when persistence must be ignored.
7855 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007856 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007857 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007858
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007859 while (srv) {
7860 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7861 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007862 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007863 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007864 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007865 /* we found the server and we can use it */
7866 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007867 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007868 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007869 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007870 break;
7871 } else {
7872 /* we found a server, but it's down,
7873 * mark it as such and go on in case
7874 * another one is available.
7875 */
7876 txn->flags &= ~TX_CK_MASK;
7877 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007878 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007879 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007880 srv = srv->next;
7881 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007882
Willy Tarreauf64d1412010-10-07 20:06:11 +02007883 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007884 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007885 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007886 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007887 txn->flags |= TX_CK_UNUSED;
7888 else
7889 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007890 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007891
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007892 /* depending on the cookie mode, we may have to either :
7893 * - delete the complete cookie if we're in insert+indirect mode, so that
7894 * the server never sees it ;
7895 * - remove the server id from the cookie value, and tag the cookie as an
7896 * application cookie so that it does not get accidentely removed later,
7897 * if we're in cookie prefix mode
7898 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007899 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007900 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007901
Willy Tarreau9b28e032012-10-12 23:49:43 +02007902 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007903 val_end += delta;
7904 next += delta;
7905 hdr_end += delta;
7906 hdr_next += delta;
7907 cur_hdr->len += delta;
7908 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007909
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007910 del_from = NULL;
7911 preserve_hdr = 1; /* we want to keep this cookie */
7912 }
7913 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007914 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007915 del_from = prev;
7916 }
7917 } else {
7918 /* This is not our cookie, so we must preserve it. But if we already
7919 * scheduled another cookie for removal, we cannot remove the
7920 * complete header, but we can remove the previous block itself.
7921 */
7922 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007923
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007924 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007925 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007926 if (att_beg >= del_from)
7927 att_beg += delta;
7928 if (att_end >= del_from)
7929 att_end += delta;
7930 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007931 val_end += delta;
7932 next += delta;
7933 hdr_end += delta;
7934 hdr_next += delta;
7935 cur_hdr->len += delta;
7936 http_msg_move_end(&txn->req, delta);
7937 prev = del_from;
7938 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007939 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007940 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007941
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007942 /* continue with next cookie on this header line */
7943 att_beg = next;
7944 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007945
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007946 /* There are no more cookies on this line.
7947 * We may still have one (or several) marked for deletion at the
7948 * end of the line. We must do this now in two ways :
7949 * - if some cookies must be preserved, we only delete from the
7950 * mark to the end of line ;
7951 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007952 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007953 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007954 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007955 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007956 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007957 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007958 cur_hdr->len += delta;
7959 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007960 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007961
7962 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007963 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7964 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007965 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007966 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007967 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007968 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007969 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007970 }
7971
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007972 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007973 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007974 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007975}
7976
7977
Willy Tarreaua15645d2007-03-18 16:22:39 +01007978/* Iterate the same filter through all response headers contained in <rtr>.
7979 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7980 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007981int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007982{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007983 char *cur_ptr, *cur_end, *cur_next;
7984 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007985 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007986 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007987 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007988
7989 last_hdr = 0;
7990
Willy Tarreau9b28e032012-10-12 23:49:43 +02007991 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007992 old_idx = 0;
7993
7994 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007995 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007996 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007997 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007998 (exp->action == ACT_ALLOW ||
7999 exp->action == ACT_DENY))
8000 return 0;
8001
8002 cur_idx = txn->hdr_idx.v[old_idx].next;
8003 if (!cur_idx)
8004 break;
8005
8006 cur_hdr = &txn->hdr_idx.v[cur_idx];
8007 cur_ptr = cur_next;
8008 cur_end = cur_ptr + cur_hdr->len;
8009 cur_next = cur_end + cur_hdr->cr + 1;
8010
8011 /* Now we have one header between cur_ptr and cur_end,
8012 * and the next header starts at cur_next.
8013 */
8014
Willy Tarreau15a53a42015-01-21 13:39:42 +01008015 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008016 switch (exp->action) {
8017 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008018 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008019 last_hdr = 1;
8020 break;
8021
8022 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008023 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008024 last_hdr = 1;
8025 break;
8026
8027 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008028 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8029 if (trash.len < 0)
8030 return -1;
8031
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008032 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008033 /* FIXME: if the user adds a newline in the replacement, the
8034 * index will not be recalculated for now, and the new line
8035 * will not be counted as a new header.
8036 */
8037
8038 cur_end += delta;
8039 cur_next += delta;
8040 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008041 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008042 break;
8043
8044 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008045 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008046 cur_next += delta;
8047
Willy Tarreaufa355d42009-11-29 18:12:29 +01008048 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008049 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8050 txn->hdr_idx.used--;
8051 cur_hdr->len = 0;
8052 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008053 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008054 break;
8055
8056 }
8057 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008058
8059 /* keep the link from this header to next one in case of later
8060 * removal of next header.
8061 */
8062 old_idx = cur_idx;
8063 }
8064 return 0;
8065}
8066
8067
8068/* Apply the filter to the status line in the response buffer <rtr>.
8069 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8070 * or -1 if a replacement resulted in an invalid status line.
8071 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008072int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008073{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008074 char *cur_ptr, *cur_end;
8075 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008076 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008077 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008078
8079
Willy Tarreau3d300592007-03-18 18:34:41 +01008080 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008081 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008082 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008083 (exp->action == ACT_ALLOW ||
8084 exp->action == ACT_DENY))
8085 return 0;
8086 else if (exp->action == ACT_REMOVE)
8087 return 0;
8088
8089 done = 0;
8090
Willy Tarreau9b28e032012-10-12 23:49:43 +02008091 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008092 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008093
8094 /* Now we have the status line between cur_ptr and cur_end */
8095
Willy Tarreau15a53a42015-01-21 13:39:42 +01008096 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008097 switch (exp->action) {
8098 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008099 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008100 done = 1;
8101 break;
8102
8103 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008104 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008105 done = 1;
8106 break;
8107
8108 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008109 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8110 if (trash.len < 0)
8111 return -1;
8112
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008113 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008114 /* FIXME: if the user adds a newline in the replacement, the
8115 * index will not be recalculated for now, and the new line
8116 * will not be counted as a new header.
8117 */
8118
Willy Tarreaufa355d42009-11-29 18:12:29 +01008119 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008120 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008121 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008122 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008123 cur_ptr, cur_end + 1,
8124 NULL, NULL);
8125 if (unlikely(!cur_end))
8126 return -1;
8127
8128 /* we have a full respnse and we know that we have either a CR
8129 * or an LF at <ptr>.
8130 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008131 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008132 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008133 /* there is no point trying this regex on headers */
8134 return 1;
8135 }
8136 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008137 return done;
8138}
8139
8140
8141
8142/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008143 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008144 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8145 * unparsable response.
8146 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008147int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008148{
Willy Tarreau192252e2015-04-04 01:47:55 +02008149 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008150 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008151 struct hdr_exp *exp;
8152
8153 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008154 int ret;
8155
8156 /*
8157 * The interleaving of transformations and verdicts
8158 * makes it difficult to decide to continue or stop
8159 * the evaluation.
8160 */
8161
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008162 if (txn->flags & TX_SVDENY)
8163 break;
8164
Willy Tarreau3d300592007-03-18 18:34:41 +01008165 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008166 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8167 exp->action == ACT_PASS)) {
8168 exp = exp->next;
8169 continue;
8170 }
8171
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008172 /* if this filter had a condition, evaluate it now and skip to
8173 * next filter if the condition does not match.
8174 */
8175 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008176 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008177 ret = acl_pass(ret);
8178 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8179 ret = !ret;
8180 if (!ret)
8181 continue;
8182 }
8183
Willy Tarreaua15645d2007-03-18 16:22:39 +01008184 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008185 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008186 if (unlikely(ret < 0))
8187 return -1;
8188
8189 if (likely(ret == 0)) {
8190 /* The filter did not match the response, it can be
8191 * iterated through all headers.
8192 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008193 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8194 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008195 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008196 }
8197 return 0;
8198}
8199
8200
Willy Tarreaua15645d2007-03-18 16:22:39 +01008201/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008202 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008203 * desirable to call it only when needed. This function is also used when we
8204 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008205 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008206void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008207{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008208 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008209 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008210 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008211 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008212 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008213 char *hdr_beg, *hdr_end, *hdr_next;
8214 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008215
Willy Tarreaua15645d2007-03-18 16:22:39 +01008216 /* Iterate through the headers.
8217 * we start with the start line.
8218 */
8219 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008220 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008221
8222 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8223 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008224 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008225
8226 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008227 hdr_beg = hdr_next;
8228 hdr_end = hdr_beg + cur_hdr->len;
8229 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008230
Willy Tarreau24581ba2010-08-31 22:39:35 +02008231 /* We have one full header between hdr_beg and hdr_end, and the
8232 * next header starts at hdr_next. We're only interested in
8233 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008234 */
8235
Willy Tarreau24581ba2010-08-31 22:39:35 +02008236 is_cookie2 = 0;
8237 prev = hdr_beg + 10;
8238 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008239 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008240 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8241 if (!val) {
8242 old_idx = cur_idx;
8243 continue;
8244 }
8245 is_cookie2 = 1;
8246 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008247 }
8248
Willy Tarreau24581ba2010-08-31 22:39:35 +02008249 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8250 * <prev> points to the colon.
8251 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008252 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008253
Willy Tarreau24581ba2010-08-31 22:39:35 +02008254 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8255 * check-cache is enabled) and we are not interested in checking
8256 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008257 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008258 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008259 return;
8260
Willy Tarreau24581ba2010-08-31 22:39:35 +02008261 /* OK so now we know we have to process this response cookie.
8262 * The format of the Set-Cookie header is slightly different
8263 * from the format of the Cookie header in that it does not
8264 * support the comma as a cookie delimiter (thus the header
8265 * cannot be folded) because the Expires attribute described in
8266 * the original Netscape's spec may contain an unquoted date
8267 * with a comma inside. We have to live with this because
8268 * many browsers don't support Max-Age and some browsers don't
8269 * support quoted strings. However the Set-Cookie2 header is
8270 * clean.
8271 *
8272 * We have to keep multiple pointers in order to support cookie
8273 * removal at the beginning, middle or end of header without
8274 * corrupting the header (in case of set-cookie2). A special
8275 * pointer, <scav> points to the beginning of the set-cookie-av
8276 * fields after the first semi-colon. The <next> pointer points
8277 * either to the end of line (set-cookie) or next unquoted comma
8278 * (set-cookie2). All of these headers are valid :
8279 *
8280 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8281 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8282 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8283 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8284 * | | | | | | | | | |
8285 * | | | | | | | | +-> next hdr_end <--+
8286 * | | | | | | | +------------> scav
8287 * | | | | | | +--------------> val_end
8288 * | | | | | +--------------------> val_beg
8289 * | | | | +----------------------> equal
8290 * | | | +------------------------> att_end
8291 * | | +----------------------------> att_beg
8292 * | +------------------------------> prev
8293 * +-----------------------------------------> hdr_beg
8294 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008295
Willy Tarreau24581ba2010-08-31 22:39:35 +02008296 for (; prev < hdr_end; prev = next) {
8297 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008298
Willy Tarreau24581ba2010-08-31 22:39:35 +02008299 /* find att_beg */
8300 att_beg = prev + 1;
8301 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8302 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008303
Willy Tarreau24581ba2010-08-31 22:39:35 +02008304 /* find att_end : this is the first character after the last non
8305 * space before the equal. It may be equal to hdr_end.
8306 */
8307 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008308
Willy Tarreau24581ba2010-08-31 22:39:35 +02008309 while (equal < hdr_end) {
8310 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8311 break;
8312 if (http_is_spht[(unsigned char)*equal++])
8313 continue;
8314 att_end = equal;
8315 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008316
Willy Tarreau24581ba2010-08-31 22:39:35 +02008317 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8318 * is between <att_beg> and <equal>, both may be identical.
8319 */
8320
8321 /* look for end of cookie if there is an equal sign */
8322 if (equal < hdr_end && *equal == '=') {
8323 /* look for the beginning of the value */
8324 val_beg = equal + 1;
8325 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8326 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008327
Willy Tarreau24581ba2010-08-31 22:39:35 +02008328 /* find the end of the value, respecting quotes */
8329 next = find_cookie_value_end(val_beg, hdr_end);
8330
8331 /* make val_end point to the first white space or delimitor after the value */
8332 val_end = next;
8333 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8334 val_end--;
8335 } else {
8336 /* <equal> points to next comma, semi-colon or EOL */
8337 val_beg = val_end = next = equal;
8338 }
8339
8340 if (next < hdr_end) {
8341 /* Set-Cookie2 supports multiple cookies, and <next> points to
8342 * a colon or semi-colon before the end. So skip all attr-value
8343 * pairs and look for the next comma. For Set-Cookie, since
8344 * commas are permitted in values, skip to the end.
8345 */
8346 if (is_cookie2)
8347 next = find_hdr_value_end(next, hdr_end);
8348 else
8349 next = hdr_end;
8350 }
8351
8352 /* Now everything is as on the diagram above */
8353
8354 /* Ignore cookies with no equal sign */
8355 if (equal == val_end)
8356 continue;
8357
8358 /* If there are spaces around the equal sign, we need to
8359 * strip them otherwise we'll get trouble for cookie captures,
8360 * or even for rewrites. Since this happens extremely rarely,
8361 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008362 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008363 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8364 int stripped_before = 0;
8365 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008366
Willy Tarreau24581ba2010-08-31 22:39:35 +02008367 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008368 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008369 equal += stripped_before;
8370 val_beg += stripped_before;
8371 }
8372
8373 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008374 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008375 val_beg += stripped_after;
8376 stripped_before += stripped_after;
8377 }
8378
8379 val_end += stripped_before;
8380 next += stripped_before;
8381 hdr_end += stripped_before;
8382 hdr_next += stripped_before;
8383 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008384 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008385 }
8386
8387 /* First, let's see if we want to capture this cookie. We check
8388 * that we don't already have a server side cookie, because we
8389 * can only capture one. Also as an optimisation, we ignore
8390 * cookies shorter than the declared name.
8391 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008392 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008393 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008394 (val_end - att_beg >= sess->fe->capture_namelen) &&
8395 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008396 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008397 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008398 Alert("HTTP logging : out of memory.\n");
8399 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008400 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008401 if (log_len > sess->fe->capture_len)
8402 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008403 memcpy(txn->srv_cookie, att_beg, log_len);
8404 txn->srv_cookie[log_len] = 0;
8405 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008406 }
8407
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008408 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008409 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008410 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008411 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8412 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008413 /* assume passive cookie by default */
8414 txn->flags &= ~TX_SCK_MASK;
8415 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008416
8417 /* If the cookie is in insert mode on a known server, we'll delete
8418 * this occurrence because we'll insert another one later.
8419 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008420 * a direct access.
8421 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008422 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008423 /* The "preserve" flag was set, we don't want to touch the
8424 * server's cookie.
8425 */
8426 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008427 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008428 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008429 /* this cookie must be deleted */
8430 if (*prev == ':' && next == hdr_end) {
8431 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008432 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008433 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8434 txn->hdr_idx.used--;
8435 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008436 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008437 hdr_next += delta;
8438 http_msg_move_end(&txn->rsp, delta);
8439 /* note: while both invalid now, <next> and <hdr_end>
8440 * are still equal, so the for() will stop as expected.
8441 */
8442 } else {
8443 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008444 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008445 next = prev;
8446 hdr_end += delta;
8447 hdr_next += delta;
8448 cur_hdr->len += delta;
8449 http_msg_move_end(&txn->rsp, delta);
8450 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008451 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008452 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008453 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008454 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008455 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008456 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008457 * with this server since we know it.
8458 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008459 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008460 next += delta;
8461 hdr_end += delta;
8462 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008463 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008464 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008465
Willy Tarreauf1348312010-10-07 15:54:11 +02008466 txn->flags &= ~TX_SCK_MASK;
8467 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008468 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008469 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008470 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008471 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008472 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008473 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008474 next += delta;
8475 hdr_end += delta;
8476 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008477 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008478 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008479
Willy Tarreau827aee92011-03-10 16:55:02 +01008480 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008481 txn->flags &= ~TX_SCK_MASK;
8482 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008483 }
8484 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008485 /* that's done for this cookie, check the next one on the same
8486 * line when next != hdr_end (only if is_cookie2).
8487 */
8488 }
8489 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008490 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008491 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008492}
8493
8494
Willy Tarreaua15645d2007-03-18 16:22:39 +01008495/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008496 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008497 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008498void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008499{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008500 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008501 char *p1, *p2;
8502
8503 char *cur_ptr, *cur_end, *cur_next;
8504 int cur_idx;
8505
Willy Tarreau5df51872007-11-25 16:20:08 +01008506 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008507 return;
8508
8509 /* Iterate through the headers.
8510 * we start with the start line.
8511 */
8512 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008513 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008514
8515 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8516 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008517 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008518
8519 cur_hdr = &txn->hdr_idx.v[cur_idx];
8520 cur_ptr = cur_next;
8521 cur_end = cur_ptr + cur_hdr->len;
8522 cur_next = cur_end + cur_hdr->cr + 1;
8523
8524 /* We have one full header between cur_ptr and cur_end, and the
8525 * next header starts at cur_next. We're only interested in
8526 * "Cookie:" headers.
8527 */
8528
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008529 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8530 if (val) {
8531 if ((cur_end - (cur_ptr + val) >= 8) &&
8532 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8533 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8534 return;
8535 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008536 }
8537
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008538 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8539 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008540 continue;
8541
8542 /* OK, right now we know we have a cache-control header at cur_ptr */
8543
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008544 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008545
8546 if (p1 >= cur_end) /* no more info */
8547 continue;
8548
8549 /* p1 is at the beginning of the value */
8550 p2 = p1;
8551
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008552 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008553 p2++;
8554
8555 /* we have a complete value between p1 and p2 */
8556 if (p2 < cur_end && *p2 == '=') {
8557 /* we have something of the form no-cache="set-cookie" */
8558 if ((cur_end - p1 >= 21) &&
8559 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8560 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008561 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008562 continue;
8563 }
8564
8565 /* OK, so we know that either p2 points to the end of string or to a comma */
8566 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008567 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008568 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8569 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8570 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008571 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008572 return;
8573 }
8574
8575 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008576 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008577 continue;
8578 }
8579 }
8580}
8581
Willy Tarreau58f10d72006-12-04 02:26:12 +01008582
Willy Tarreaub2513902006-12-17 14:52:38 +01008583/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008584 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008585 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008586 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008587 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008588 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008589 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008590 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008591 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008592int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008593{
8594 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008595 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008596 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008597
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008598 if (!uri_auth)
8599 return 0;
8600
Cyril Bonté70be45d2010-10-12 00:14:35 +02008601 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008602 return 0;
8603
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008604 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008605 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008606 return 0;
8607
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008608 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008609 return 0;
8610
Willy Tarreaub2513902006-12-17 14:52:38 +01008611 return 1;
8612}
8613
Willy Tarreau4076a152009-04-02 15:18:36 +02008614/*
8615 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008616 * By default it tries to report the error position as msg->err_pos. However if
8617 * this one is not set, it will then report msg->next, which is the last known
8618 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008619 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008620 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008621void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008622 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008623 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008624{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008625 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008626 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008627 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008628
Willy Tarreau9b28e032012-10-12 23:49:43 +02008629 es->len = MIN(chn->buf->i, sizeof(es->buf));
8630 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008631 len1 = MIN(len1, es->len);
8632 len2 = es->len - len1; /* remaining data if buffer wraps */
8633
Willy Tarreau9b28e032012-10-12 23:49:43 +02008634 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008635 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008636 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008637
Willy Tarreau4076a152009-04-02 15:18:36 +02008638 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008639 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008640 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008641 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008642
Willy Tarreau4076a152009-04-02 15:18:36 +02008643 es->when = date; // user-visible date
8644 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008645 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008646 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008647 if (objt_conn(sess->origin))
8648 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008649 else
8650 memset(&es->src, 0, sizeof(es->src));
8651
Willy Tarreau078272e2010-12-12 12:46:33 +01008652 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008653 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008654 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008655 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008656 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008657 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008658 es->b_out = chn->buf->o;
8659 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008660 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008661 es->m_clen = msg->chunk_len;
8662 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008663}
Willy Tarreaub2513902006-12-17 14:52:38 +01008664
Willy Tarreau294c4732011-12-16 21:35:50 +01008665/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8666 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8667 * performed over the whole headers. Otherwise it must contain a valid header
8668 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8669 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8670 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8671 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008672 * -1. The value fetch stops at commas, so this function is suited for use with
8673 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008674 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008675 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008676unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008677 struct hdr_idx *idx, int occ,
8678 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008679{
Willy Tarreau294c4732011-12-16 21:35:50 +01008680 struct hdr_ctx local_ctx;
8681 char *ptr_hist[MAX_HDR_HISTORY];
8682 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008683 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008684 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008685
Willy Tarreau294c4732011-12-16 21:35:50 +01008686 if (!ctx) {
8687 local_ctx.idx = 0;
8688 ctx = &local_ctx;
8689 }
8690
Willy Tarreaubce70882009-09-07 11:51:47 +02008691 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008692 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008693 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008694 occ--;
8695 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008696 *vptr = ctx->line + ctx->val;
8697 *vlen = ctx->vlen;
8698 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008699 }
8700 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008701 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008702 }
8703
8704 /* negative occurrence, we scan all the list then walk back */
8705 if (-occ > MAX_HDR_HISTORY)
8706 return 0;
8707
Willy Tarreau294c4732011-12-16 21:35:50 +01008708 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008709 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008710 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8711 len_hist[hist_ptr] = ctx->vlen;
8712 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008713 hist_ptr = 0;
8714 found++;
8715 }
8716 if (-occ > found)
8717 return 0;
8718 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008719 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8720 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8721 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008722 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008723 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008724 if (hist_ptr >= MAX_HDR_HISTORY)
8725 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008726 *vptr = ptr_hist[hist_ptr];
8727 *vlen = len_hist[hist_ptr];
8728 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008729}
8730
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008731/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8732 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8733 * performed over the whole headers. Otherwise it must contain a valid header
8734 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8735 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8736 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8737 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8738 * -1. This function differs from http_get_hdr() in that it only returns full
8739 * line header values and does not stop at commas.
8740 * The return value is 0 if nothing was found, or non-zero otherwise.
8741 */
8742unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8743 struct hdr_idx *idx, int occ,
8744 struct hdr_ctx *ctx, char **vptr, int *vlen)
8745{
8746 struct hdr_ctx local_ctx;
8747 char *ptr_hist[MAX_HDR_HISTORY];
8748 int len_hist[MAX_HDR_HISTORY];
8749 unsigned int hist_ptr;
8750 int found;
8751
8752 if (!ctx) {
8753 local_ctx.idx = 0;
8754 ctx = &local_ctx;
8755 }
8756
8757 if (occ >= 0) {
8758 /* search from the beginning */
8759 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8760 occ--;
8761 if (occ <= 0) {
8762 *vptr = ctx->line + ctx->val;
8763 *vlen = ctx->vlen;
8764 return 1;
8765 }
8766 }
8767 return 0;
8768 }
8769
8770 /* negative occurrence, we scan all the list then walk back */
8771 if (-occ > MAX_HDR_HISTORY)
8772 return 0;
8773
8774 found = hist_ptr = 0;
8775 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8776 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8777 len_hist[hist_ptr] = ctx->vlen;
8778 if (++hist_ptr >= MAX_HDR_HISTORY)
8779 hist_ptr = 0;
8780 found++;
8781 }
8782 if (-occ > found)
8783 return 0;
8784 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8785 * find occurrence -occ, so we have to check [hist_ptr+occ].
8786 */
8787 hist_ptr += occ;
8788 if (hist_ptr >= MAX_HDR_HISTORY)
8789 hist_ptr -= MAX_HDR_HISTORY;
8790 *vptr = ptr_hist[hist_ptr];
8791 *vlen = len_hist[hist_ptr];
8792 return 1;
8793}
8794
Willy Tarreaubaaee002006-06-26 02:48:02 +02008795/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008796 * Print a debug line with a header. Always stop at the first CR or LF char,
8797 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8798 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008799 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008800void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008801{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008802 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008803 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008804
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008805 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008806 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008807 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008808 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 +02008809
8810 for (max = 0; start + max < end; max++)
8811 if (start[max] == '\r' || start[max] == '\n')
8812 break;
8813
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008814 UBOUND(max, trash.size - trash.len - 3);
8815 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8816 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008817 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008818}
8819
Willy Tarreaueee5b512015-04-03 23:46:31 +02008820
8821/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8822 * The hdr_idx is allocated as well. In case of allocation failure, everything
8823 * allocated is freed and NULL is returned. Otherwise the new transaction is
8824 * assigned to the stream and returned.
8825 */
8826struct http_txn *http_alloc_txn(struct stream *s)
8827{
8828 struct http_txn *txn = s->txn;
8829
8830 if (txn)
8831 return txn;
8832
8833 txn = pool_alloc2(pool2_http_txn);
8834 if (!txn)
8835 return txn;
8836
8837 txn->hdr_idx.size = global.tune.max_http_hdr;
8838 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8839 if (!txn->hdr_idx.v) {
8840 pool_free2(pool2_http_txn, txn);
8841 return NULL;
8842 }
8843
8844 s->txn = txn;
8845 return txn;
8846}
8847
Willy Tarreau0937bc42009-12-22 15:03:09 +01008848/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008849 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008850 * the required fields are properly allocated and that we only need to (re)init
8851 * them. This should be used before processing any new request.
8852 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008853void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008854{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008855 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008856 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008857
8858 txn->flags = 0;
8859 txn->status = -1;
8860
Willy Tarreauf64d1412010-10-07 20:06:11 +02008861 txn->cookie_first_date = 0;
8862 txn->cookie_last_date = 0;
8863
Willy Tarreaueee5b512015-04-03 23:46:31 +02008864 txn->srv_cookie = NULL;
8865 txn->cli_cookie = NULL;
8866 txn->uri = NULL;
8867
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008868 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008869 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008870 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008871 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008872 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008873 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008874 txn->req.chunk_len = 0LL;
8875 txn->req.body_len = 0LL;
8876 txn->rsp.chunk_len = 0LL;
8877 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008878 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8879 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008880 txn->req.chn = &s->req;
8881 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008882
8883 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008884
8885 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8886 if (fe->options2 & PR_O2_REQBUG_OK)
8887 txn->req.err_pos = -1; /* let buggy requests pass */
8888
Willy Tarreau0937bc42009-12-22 15:03:09 +01008889 if (txn->hdr_idx.v)
8890 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008891
8892 vars_init(&s->vars_txn, SCOPE_TXN);
8893 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008894}
8895
8896/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008897void http_end_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
Willy Tarreau75195602014-03-11 15:48:55 +01008902 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008903 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008904 s->comp_algo->end(&s->comp_ctx);
8905 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008906 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008907
Willy Tarreau0937bc42009-12-22 15:03:09 +01008908 /* these ones will have been dynamically allocated */
8909 pool_free2(pool2_requri, txn->uri);
8910 pool_free2(pool2_capture, txn->cli_cookie);
8911 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008912 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008913
William Lallemanda73203e2012-03-12 12:48:57 +01008914 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008915 txn->uri = NULL;
8916 txn->srv_cookie = NULL;
8917 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008918
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008919 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008920 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008921 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008922 pool_free2(h->pool, s->req_cap[h->index]);
8923 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008924 }
8925
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008926 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008927 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008928 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008929 pool_free2(h->pool, s->res_cap[h->index]);
8930 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008931 }
8932
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008933 vars_prune(&s->vars_txn, s);
8934 vars_prune(&s->vars_reqres, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008935}
8936
8937/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008938void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008939{
8940 http_end_txn(s);
8941 http_init_txn(s);
8942
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008943 /* reinitialise the current rule list pointer to NULL. We are sure that
8944 * any rulelist match the NULL pointer.
8945 */
8946 s->current_rule_list = NULL;
8947
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008948 s->be = strm_fe(s);
8949 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008950 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008951 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008952 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008953 /* re-init store persistence */
8954 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008955 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008956
Willy Tarreau0937bc42009-12-22 15:03:09 +01008957 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008958
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008959 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008960
Willy Tarreau739cfba2010-01-25 23:11:14 +01008961 /* We must trim any excess data from the response buffer, because we
8962 * may have blocked an invalid response from a server that we don't
8963 * want to accidentely forward once we disable the analysers, nor do
8964 * we want those data to come along with next response. A typical
8965 * example of such data would be from a buggy server responding to
8966 * a HEAD with some data, or sending more than the advertised
8967 * content-length.
8968 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008969 if (unlikely(s->res.buf->i))
8970 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008971
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008972 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008973 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008974
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008975 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008976 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008977
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008978 s->req.rex = TICK_ETERNITY;
8979 s->req.wex = TICK_ETERNITY;
8980 s->req.analyse_exp = TICK_ETERNITY;
8981 s->res.rex = TICK_ETERNITY;
8982 s->res.wex = TICK_ETERNITY;
8983 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008984}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008985
Sasha Pachev218f0642014-06-16 12:05:59 -06008986void free_http_res_rules(struct list *r)
8987{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008988 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008989
8990 list_for_each_entry_safe(pr, tr, r, list) {
8991 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008992 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008993 free(pr);
8994 }
8995}
8996
8997void free_http_req_rules(struct list *r)
8998{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008999 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009000
9001 list_for_each_entry_safe(pr, tr, r, list) {
9002 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009003 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009004 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009005
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009006 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009007 free(pr);
9008 }
9009}
9010
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009011/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009012struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009013{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009014 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009015 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009016 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009017 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009018
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009019 rule = (struct act_rule*)calloc(1, sizeof(struct act_rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009020 if (!rule) {
9021 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009022 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009023 }
9024
CJ Ess108b1dd2015-04-07 12:03:37 -04009025 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009026 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009027 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009028 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009029 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009030 int code;
9031 int hc;
9032
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009033 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01009034 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009035 if (strcmp(args[cur_arg], "deny_status") == 0) {
9036 cur_arg++;
9037 if (!args[cur_arg]) {
9038 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9039 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9040 goto out_err;
9041 }
9042
9043 code = atol(args[cur_arg]);
9044 cur_arg++;
9045 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9046 if (http_err_codes[hc] == code) {
9047 rule->deny_status = hc;
9048 break;
9049 }
9050 }
9051
9052 if (hc >= HTTP_ERR_SIZE) {
9053 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9054 file, linenum, code);
9055 }
9056 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009057 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009058 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009059 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009060 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009061 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009062 cur_arg = 1;
9063
9064 while(*args[cur_arg]) {
9065 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009066 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009067 cur_arg+=2;
9068 continue;
9069 } else
9070 break;
9071 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009072 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009073 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009074 cur_arg = 1;
9075
9076 if (!*args[cur_arg] ||
9077 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9078 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9079 file, linenum, args[0]);
9080 goto out_err;
9081 }
9082 rule->arg.nice = atoi(args[cur_arg]);
9083 if (rule->arg.nice < -1024)
9084 rule->arg.nice = -1024;
9085 else if (rule->arg.nice > 1024)
9086 rule->arg.nice = 1024;
9087 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009088 } else if (!strcmp(args[0], "set-tos")) {
9089#ifdef IP_TOS
9090 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009091 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009092 cur_arg = 1;
9093
9094 if (!*args[cur_arg] ||
9095 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9096 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9097 file, linenum, args[0]);
9098 goto out_err;
9099 }
9100
9101 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9102 if (err && *err != '\0') {
9103 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9104 file, linenum, err, args[0]);
9105 goto out_err;
9106 }
9107 cur_arg++;
9108#else
9109 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9110 goto out_err;
9111#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009112 } else if (!strcmp(args[0], "set-mark")) {
9113#ifdef SO_MARK
9114 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009115 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009116 cur_arg = 1;
9117
9118 if (!*args[cur_arg] ||
9119 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9120 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9121 file, linenum, args[0]);
9122 goto out_err;
9123 }
9124
9125 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9126 if (err && *err != '\0') {
9127 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9128 file, linenum, err, args[0]);
9129 goto out_err;
9130 }
9131 cur_arg++;
9132 global.last_checks |= LSTCHK_NETADM;
9133#else
9134 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9135 goto out_err;
9136#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009137 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009138 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009139 cur_arg = 1;
9140
9141 if (!*args[cur_arg] ||
9142 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9143 bad_log_level:
9144 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9145 file, linenum, args[0]);
9146 goto out_err;
9147 }
9148 if (strcmp(args[cur_arg], "silent") == 0)
9149 rule->arg.loglevel = -1;
9150 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9151 goto bad_log_level;
9152 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009153 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009154 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009155 cur_arg = 1;
9156
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009157 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9158 (*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 +01009159 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9160 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009161 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009162 }
9163
9164 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9165 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9166 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009167
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009168 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009169 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009170 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9171 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009172 free(proxy->conf.lfs_file);
9173 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9174 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009175 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009176 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009177 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009178 cur_arg = 1;
9179
9180 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009181 (*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 -06009182 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9183 file, linenum, args[0]);
9184 goto out_err;
9185 }
9186
9187 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9188 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9189 LIST_INIT(&rule->arg.hdr_add.fmt);
9190
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009191 error = NULL;
9192 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9193 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9194 args[cur_arg + 1], error);
9195 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009196 goto out_err;
9197 }
9198
9199 proxy->conf.args.ctx = ARGC_HRQ;
9200 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9201 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9202 file, linenum);
9203
9204 free(proxy->conf.lfs_file);
9205 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9206 proxy->conf.lfs_line = proxy->conf.args.line;
9207 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009208 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009209 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009210 cur_arg = 1;
9211
9212 if (!*args[cur_arg] ||
9213 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9214 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9215 file, linenum, args[0]);
9216 goto out_err;
9217 }
9218
9219 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9220 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9221
9222 proxy->conf.args.ctx = ARGC_HRQ;
9223 free(proxy->conf.lfs_file);
9224 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9225 proxy->conf.lfs_line = proxy->conf.args.line;
9226 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009227 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9228 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009229 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009230 struct sample_expr *expr;
9231 unsigned int where;
9232 char *err = NULL;
9233
9234 cur_arg = 1;
9235 proxy->conf.args.ctx = ARGC_TRK;
9236
9237 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9238 if (!expr) {
9239 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9240 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9241 free(err);
9242 goto out_err;
9243 }
9244
9245 where = 0;
9246 if (proxy->cap & PR_CAP_FE)
9247 where |= SMP_VAL_FE_HRQ_HDR;
9248 if (proxy->cap & PR_CAP_BE)
9249 where |= SMP_VAL_BE_HRQ_HDR;
9250
9251 if (!(expr->fetch->val & where)) {
9252 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9253 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9254 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9255 args[cur_arg-1], sample_src_names(expr->fetch->use));
9256 free(expr);
9257 goto out_err;
9258 }
9259
9260 if (strcmp(args[cur_arg], "table") == 0) {
9261 cur_arg++;
9262 if (!args[cur_arg]) {
9263 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9264 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9265 free(expr);
9266 goto out_err;
9267 }
9268 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009269 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009270 cur_arg++;
9271 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009272 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009273 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009274 } else if (strcmp(args[0], "redirect") == 0) {
9275 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009276 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009277
Willy Tarreaube4653b2015-05-28 15:26:58 +02009278 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009279 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9280 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9281 goto out_err;
9282 }
9283
9284 /* this redirect rule might already contain a parsed condition which
9285 * we'll pass to the http-request rule.
9286 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009287 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009288 rule->arg.redir = redir;
9289 rule->cond = redir->cond;
9290 redir->cond = NULL;
9291 cur_arg = 2;
9292 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009293 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9294 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009295 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009296 /*
9297 * '+ 8' for 'add-acl('
9298 * '- 9' for 'add-acl(' + trailing ')'
9299 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009300 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009301
9302 cur_arg = 1;
9303
9304 if (!*args[cur_arg] ||
9305 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9306 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9307 file, linenum, args[0]);
9308 goto out_err;
9309 }
9310
9311 LIST_INIT(&rule->arg.map.key);
9312 proxy->conf.args.ctx = ARGC_HRQ;
9313 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9314 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9315 file, linenum);
9316 free(proxy->conf.lfs_file);
9317 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9318 proxy->conf.lfs_line = proxy->conf.args.line;
9319 cur_arg += 1;
9320 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9321 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009322 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009323 /*
9324 * '+ 8' for 'del-acl('
9325 * '- 9' for 'del-acl(' + trailing ')'
9326 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009327 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009328
9329 cur_arg = 1;
9330
9331 if (!*args[cur_arg] ||
9332 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9333 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9334 file, linenum, args[0]);
9335 goto out_err;
9336 }
9337
9338 LIST_INIT(&rule->arg.map.key);
9339 proxy->conf.args.ctx = ARGC_HRQ;
9340 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9341 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9342 file, linenum);
9343 free(proxy->conf.lfs_file);
9344 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9345 proxy->conf.lfs_line = proxy->conf.args.line;
9346 cur_arg += 1;
9347 } else if (strncmp(args[0], "del-map", 7) == 0) {
9348 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009349 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009350 /*
9351 * '+ 8' for 'del-map('
9352 * '- 9' for 'del-map(' + trailing ')'
9353 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009354 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009355
9356 cur_arg = 1;
9357
9358 if (!*args[cur_arg] ||
9359 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9360 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9361 file, linenum, args[0]);
9362 goto out_err;
9363 }
9364
9365 LIST_INIT(&rule->arg.map.key);
9366 proxy->conf.args.ctx = ARGC_HRQ;
9367 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9368 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9369 file, linenum);
9370 free(proxy->conf.lfs_file);
9371 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9372 proxy->conf.lfs_line = proxy->conf.args.line;
9373 cur_arg += 1;
9374 } else if (strncmp(args[0], "set-map", 7) == 0) {
9375 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009376 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009377 /*
9378 * '+ 8' for 'set-map('
9379 * '- 9' for 'set-map(' + trailing ')'
9380 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009381 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009382
9383 cur_arg = 1;
9384
9385 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9386 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9387 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9388 file, linenum, args[0]);
9389 goto out_err;
9390 }
9391
9392 LIST_INIT(&rule->arg.map.key);
9393 LIST_INIT(&rule->arg.map.value);
9394 proxy->conf.args.ctx = ARGC_HRQ;
9395
9396 /* key pattern */
9397 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9398 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9399 file, linenum);
9400
9401 /* value pattern */
9402 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, 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
9409 cur_arg += 2;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009410 } else if (strncmp(args[0], "set-src", 7) == 0) {
9411 struct sample_expr *expr;
9412 unsigned int where;
9413 char *err = NULL;
9414
9415 cur_arg = 1;
9416 proxy->conf.args.ctx = ARGC_HRQ;
9417
9418 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9419 if (!expr) {
9420 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9421 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9422 free(err);
9423 goto out_err;
9424 }
9425
9426 where = 0;
9427 if (proxy->cap & PR_CAP_FE)
9428 where |= SMP_VAL_FE_HRQ_HDR;
9429 if (proxy->cap & PR_CAP_BE)
9430 where |= SMP_VAL_BE_HRQ_HDR;
9431
9432 if (!(expr->fetch->val & where)) {
9433 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9434 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9435 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9436 args[cur_arg-1], sample_src_names(expr->fetch->use));
9437 free(expr);
9438 goto out_err;
9439 }
9440
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02009441 rule->arg.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009442 rule->action = ACT_HTTP_REQ_SET_SRC;
William Lallemand73025dd2014-04-24 14:38:37 +02009443 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9444 char *errmsg = NULL;
9445 cur_arg = 1;
9446 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009447 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009448 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009449 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9450 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9451 free(errmsg);
9452 goto out_err;
9453 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009454 } else {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009455 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'set-var', 'set-src', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009456 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009457 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009458 }
9459
9460 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9461 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009462 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009463
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009464 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9465 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9466 file, linenum, args[0], errmsg);
9467 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009468 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009469 }
9470 rule->cond = cond;
9471 }
9472 else if (*args[cur_arg]) {
9473 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9474 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9475 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009476 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009477 }
9478
9479 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009480 out_err:
9481 free(rule);
9482 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009483}
9484
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009485/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009486struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009487{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009488 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009489 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009490 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009491 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009492
9493 rule = calloc(1, sizeof(*rule));
9494 if (!rule) {
9495 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9496 goto out_err;
9497 }
9498
9499 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009500 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009501 cur_arg = 1;
9502 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009503 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009504 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009505 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009506 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009507 cur_arg = 1;
9508
9509 if (!*args[cur_arg] ||
9510 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9511 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9512 file, linenum, args[0]);
9513 goto out_err;
9514 }
9515 rule->arg.nice = atoi(args[cur_arg]);
9516 if (rule->arg.nice < -1024)
9517 rule->arg.nice = -1024;
9518 else if (rule->arg.nice > 1024)
9519 rule->arg.nice = 1024;
9520 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009521 } else if (!strcmp(args[0], "set-tos")) {
9522#ifdef IP_TOS
9523 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009524 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009525 cur_arg = 1;
9526
9527 if (!*args[cur_arg] ||
9528 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9529 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9530 file, linenum, args[0]);
9531 goto out_err;
9532 }
9533
9534 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9535 if (err && *err != '\0') {
9536 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9537 file, linenum, err, args[0]);
9538 goto out_err;
9539 }
9540 cur_arg++;
9541#else
9542 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9543 goto out_err;
9544#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009545 } else if (!strcmp(args[0], "set-mark")) {
9546#ifdef SO_MARK
9547 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009548 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009549 cur_arg = 1;
9550
9551 if (!*args[cur_arg] ||
9552 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9553 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9554 file, linenum, args[0]);
9555 goto out_err;
9556 }
9557
9558 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9559 if (err && *err != '\0') {
9560 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9561 file, linenum, err, args[0]);
9562 goto out_err;
9563 }
9564 cur_arg++;
9565 global.last_checks |= LSTCHK_NETADM;
9566#else
9567 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9568 goto out_err;
9569#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009570 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009571 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009572 cur_arg = 1;
9573
9574 if (!*args[cur_arg] ||
9575 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9576 bad_log_level:
9577 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9578 file, linenum, args[0]);
9579 goto out_err;
9580 }
9581 if (strcmp(args[cur_arg], "silent") == 0)
9582 rule->arg.loglevel = -1;
9583 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9584 goto bad_log_level;
9585 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009586 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009587 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009588 cur_arg = 1;
9589
9590 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9591 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9592 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9593 file, linenum, args[0]);
9594 goto out_err;
9595 }
9596
9597 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9598 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9599 LIST_INIT(&rule->arg.hdr_add.fmt);
9600
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009601 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009602 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009603 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9604 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009605 free(proxy->conf.lfs_file);
9606 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9607 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009608 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009609 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009610 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009611 cur_arg = 1;
9612
9613 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009614 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9615 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009616 file, linenum, args[0]);
9617 goto out_err;
9618 }
9619
9620 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9621 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9622 LIST_INIT(&rule->arg.hdr_add.fmt);
9623
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009624 error = NULL;
9625 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9626 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9627 args[cur_arg + 1], error);
9628 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009629 goto out_err;
9630 }
9631
9632 proxy->conf.args.ctx = ARGC_HRQ;
9633 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9634 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9635 file, linenum);
9636
9637 free(proxy->conf.lfs_file);
9638 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9639 proxy->conf.lfs_line = proxy->conf.args.line;
9640 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009641 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009642 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009643 cur_arg = 1;
9644
9645 if (!*args[cur_arg] ||
9646 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9647 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9648 file, linenum, args[0]);
9649 goto out_err;
9650 }
9651
9652 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9653 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9654
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009655 proxy->conf.args.ctx = ARGC_HRS;
9656 free(proxy->conf.lfs_file);
9657 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9658 proxy->conf.lfs_line = proxy->conf.args.line;
9659 cur_arg += 1;
9660 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9661 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009662 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009663 /*
9664 * '+ 8' for 'add-acl('
9665 * '- 9' for 'add-acl(' + trailing ')'
9666 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009667 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009668
9669 cur_arg = 1;
9670
9671 if (!*args[cur_arg] ||
9672 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9673 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9674 file, linenum, args[0]);
9675 goto out_err;
9676 }
9677
9678 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009679 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009680 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9681 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9682 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009683 free(proxy->conf.lfs_file);
9684 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9685 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009686
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009687 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009688 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9689 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009690 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009691 /*
9692 * '+ 8' for 'del-acl('
9693 * '- 9' for 'del-acl(' + trailing ')'
9694 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009695 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009696
9697 cur_arg = 1;
9698
9699 if (!*args[cur_arg] ||
9700 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9701 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9702 file, linenum, args[0]);
9703 goto out_err;
9704 }
9705
9706 LIST_INIT(&rule->arg.map.key);
9707 proxy->conf.args.ctx = ARGC_HRS;
9708 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9709 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9710 file, linenum);
9711 free(proxy->conf.lfs_file);
9712 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9713 proxy->conf.lfs_line = proxy->conf.args.line;
9714 cur_arg += 1;
9715 } else if (strncmp(args[0], "del-map", 7) == 0) {
9716 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009717 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009718 /*
9719 * '+ 8' for 'del-map('
9720 * '- 9' for 'del-map(' + trailing ')'
9721 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009722 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009723
9724 cur_arg = 1;
9725
9726 if (!*args[cur_arg] ||
9727 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9728 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9729 file, linenum, args[0]);
9730 goto out_err;
9731 }
9732
9733 LIST_INIT(&rule->arg.map.key);
9734 proxy->conf.args.ctx = ARGC_HRS;
9735 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9736 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9737 file, linenum);
9738 free(proxy->conf.lfs_file);
9739 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9740 proxy->conf.lfs_line = proxy->conf.args.line;
9741 cur_arg += 1;
9742 } else if (strncmp(args[0], "set-map", 7) == 0) {
9743 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009744 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009745 /*
9746 * '+ 8' for 'set-map('
9747 * '- 9' for 'set-map(' + trailing ')'
9748 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009749 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009750
9751 cur_arg = 1;
9752
9753 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9754 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9755 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9756 file, linenum, args[0]);
9757 goto out_err;
9758 }
9759
9760 LIST_INIT(&rule->arg.map.key);
9761 LIST_INIT(&rule->arg.map.value);
9762
9763 proxy->conf.args.ctx = ARGC_HRS;
9764
9765 /* key pattern */
9766 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9767 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9768 file, linenum);
9769
9770 /* value pattern */
9771 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9772 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9773 file, linenum);
9774
9775 free(proxy->conf.lfs_file);
9776 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9777 proxy->conf.lfs_line = proxy->conf.args.line;
9778
9779 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009780 } else if (strcmp(args[0], "redirect") == 0) {
9781 struct redirect_rule *redir;
9782 char *errmsg = NULL;
9783
9784 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9785 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9786 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9787 goto out_err;
9788 }
9789
9790 /* this redirect rule might already contain a parsed condition which
9791 * we'll pass to the http-request rule.
9792 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009793 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009794 rule->arg.redir = redir;
9795 rule->cond = redir->cond;
9796 redir->cond = NULL;
9797 cur_arg = 2;
9798 return rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009799 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9800 char *errmsg = NULL;
9801 cur_arg = 1;
9802 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009803 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009804 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009805 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9806 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9807 free(errmsg);
9808 goto out_err;
9809 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009810 } else {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02009811 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'del-acl', 'add-acl', 'del-map', 'set-map', 'set-var' but got '%s'%s.\n",
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009812 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9813 goto out_err;
9814 }
9815
9816 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9817 struct acl_cond *cond;
9818 char *errmsg = NULL;
9819
9820 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9821 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9822 file, linenum, args[0], errmsg);
9823 free(errmsg);
9824 goto out_err;
9825 }
9826 rule->cond = cond;
9827 }
9828 else if (*args[cur_arg]) {
9829 Alert("parsing [%s:%d]: 'http-response %s' expects"
9830 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9831 file, linenum, args[0], args[cur_arg]);
9832 goto out_err;
9833 }
9834
9835 return rule;
9836 out_err:
9837 free(rule);
9838 return NULL;
9839}
9840
Willy Tarreau4baae242012-12-27 12:00:31 +01009841/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009842 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009843 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9844 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009845 */
9846struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009847 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009848{
9849 struct redirect_rule *rule;
9850 int cur_arg;
9851 int type = REDIRECT_TYPE_NONE;
9852 int code = 302;
9853 const char *destination = NULL;
9854 const char *cookie = NULL;
9855 int cookie_set = 0;
9856 unsigned int flags = REDIRECT_FLAG_NONE;
9857 struct acl_cond *cond = NULL;
9858
9859 cur_arg = 0;
9860 while (*(args[cur_arg])) {
9861 if (strcmp(args[cur_arg], "location") == 0) {
9862 if (!*args[cur_arg + 1])
9863 goto missing_arg;
9864
9865 type = REDIRECT_TYPE_LOCATION;
9866 cur_arg++;
9867 destination = args[cur_arg];
9868 }
9869 else if (strcmp(args[cur_arg], "prefix") == 0) {
9870 if (!*args[cur_arg + 1])
9871 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009872 type = REDIRECT_TYPE_PREFIX;
9873 cur_arg++;
9874 destination = args[cur_arg];
9875 }
9876 else if (strcmp(args[cur_arg], "scheme") == 0) {
9877 if (!*args[cur_arg + 1])
9878 goto missing_arg;
9879
9880 type = REDIRECT_TYPE_SCHEME;
9881 cur_arg++;
9882 destination = args[cur_arg];
9883 }
9884 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9885 if (!*args[cur_arg + 1])
9886 goto missing_arg;
9887
9888 cur_arg++;
9889 cookie = args[cur_arg];
9890 cookie_set = 1;
9891 }
9892 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9893 if (!*args[cur_arg + 1])
9894 goto missing_arg;
9895
9896 cur_arg++;
9897 cookie = args[cur_arg];
9898 cookie_set = 0;
9899 }
9900 else if (strcmp(args[cur_arg], "code") == 0) {
9901 if (!*args[cur_arg + 1])
9902 goto missing_arg;
9903
9904 cur_arg++;
9905 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009906 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009907 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009908 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009909 args[cur_arg - 1], args[cur_arg]);
9910 return NULL;
9911 }
9912 }
9913 else if (!strcmp(args[cur_arg],"drop-query")) {
9914 flags |= REDIRECT_FLAG_DROP_QS;
9915 }
9916 else if (!strcmp(args[cur_arg],"append-slash")) {
9917 flags |= REDIRECT_FLAG_APPEND_SLASH;
9918 }
9919 else if (strcmp(args[cur_arg], "if") == 0 ||
9920 strcmp(args[cur_arg], "unless") == 0) {
9921 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9922 if (!cond) {
9923 memprintf(errmsg, "error in condition: %s", *errmsg);
9924 return NULL;
9925 }
9926 break;
9927 }
9928 else {
9929 memprintf(errmsg,
9930 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9931 args[cur_arg]);
9932 return NULL;
9933 }
9934 cur_arg++;
9935 }
9936
9937 if (type == REDIRECT_TYPE_NONE) {
9938 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9939 return NULL;
9940 }
9941
Willy Tarreaube4653b2015-05-28 15:26:58 +02009942 if (dir && type != REDIRECT_TYPE_LOCATION) {
9943 memprintf(errmsg, "response only supports redirect type 'location'");
9944 return NULL;
9945 }
9946
Willy Tarreau4baae242012-12-27 12:00:31 +01009947 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9948 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009949 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009950
9951 if (!use_fmt) {
9952 /* old-style static redirect rule */
9953 rule->rdr_str = strdup(destination);
9954 rule->rdr_len = strlen(destination);
9955 }
9956 else {
9957 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009958
9959 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9960 * if prefix == "/", we don't want to add anything, otherwise it
9961 * makes it hard for the user to configure a self-redirection.
9962 */
Godbachd9722032014-12-18 15:44:58 +08009963 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009964 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009965 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009966 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
9967 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009968 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009969 free(curproxy->conf.lfs_file);
9970 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9971 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009972 }
9973 }
9974
Willy Tarreau4baae242012-12-27 12:00:31 +01009975 if (cookie) {
9976 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9977 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9978 */
9979 rule->cookie_len = strlen(cookie);
9980 if (cookie_set) {
9981 rule->cookie_str = malloc(rule->cookie_len + 10);
9982 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9983 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9984 rule->cookie_len += 9;
9985 } else {
9986 rule->cookie_str = malloc(rule->cookie_len + 21);
9987 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9988 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9989 rule->cookie_len += 20;
9990 }
9991 }
9992 rule->type = type;
9993 rule->code = code;
9994 rule->flags = flags;
9995 LIST_INIT(&rule->list);
9996 return rule;
9997
9998 missing_arg:
9999 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10000 return NULL;
10001}
10002
Willy Tarreau8797c062007-05-07 00:55:35 +020010003/************************************************************************/
10004/* The code below is dedicated to ACL parsing and matching */
10005/************************************************************************/
10006
10007
Willy Tarreau14174bc2012-04-16 14:34:04 +020010008/* This function ensures that the prerequisites for an L7 fetch are ready,
10009 * which means that a request or response is ready. If some data is missing,
10010 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010011 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10012 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010013 *
10014 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010015 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10016 * decide whether or not an HTTP message is present ;
10017 * 0 if the requested data cannot be fetched or if it is certain that
10018 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010019 * 1 if an HTTP message is ready
10020 */
10021static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010022smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010023 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010024{
Willy Tarreau192252e2015-04-04 01:47:55 +020010025 struct http_txn *txn;
10026 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010027
Willy Tarreaueee5b512015-04-03 23:46:31 +020010028 /* Note: this function may only be used from places where
10029 * http_init_txn() has already been done, and implies that <s>,
10030 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
10031 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010032 */
10033
Willy Tarreau192252e2015-04-04 01:47:55 +020010034 if (!s)
10035 return 0;
10036 txn = s->txn;
10037
10038 if (!txn)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010039 return 0;
Willy Tarreau192252e2015-04-04 01:47:55 +020010040 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010041
10042 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010043 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010044
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010045 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010046 /* If the buffer does not leave enough free space at the end,
10047 * we must first realign it.
10048 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010049 if (s->req.buf->p > s->req.buf->data &&
10050 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10051 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010052
Willy Tarreau14174bc2012-04-16 14:34:04 +020010053 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010054 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010055 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010056
10057 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010058 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010059 http_msg_analyzer(msg, &txn->hdr_idx);
10060
10061 /* Still no valid request ? */
10062 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010063 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010064 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010065 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010066 }
10067 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010068 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010069 return 0;
10070 }
10071
10072 /* OK we just got a valid HTTP request. We have some minor
10073 * preparation to perform so that further checks can rely
10074 * on HTTP tests.
10075 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010076
10077 /* If the request was parsed but was too large, we must absolutely
10078 * return an error so that it is not processed. At the moment this
10079 * cannot happen, but if the parsers are to change in the future,
10080 * we want this check to be maintained.
10081 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010082 if (unlikely(s->req.buf->i + s->req.buf->p >
10083 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010084 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010085 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010086 return 1;
10087 }
10088
Willy Tarreau9b28e032012-10-12 23:49:43 +020010089 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010090 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010091 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010092
Willy Tarreau506d0502013-07-06 13:29:24 +020010093 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10094 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010095 }
10096
Willy Tarreau506d0502013-07-06 13:29:24 +020010097 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010098 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010099 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010100
10101 /* otherwise everything's ready for the request */
10102 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010103 else {
10104 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010105 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10106 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010107 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010108 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010109 }
10110
10111 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010112 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010113 return 1;
10114}
Willy Tarreau8797c062007-05-07 00:55:35 +020010115
Willy Tarreau6c616e02014-06-25 16:56:41 +020010116/* Note: these functinos *do* modify the sample. Even in case of success, at
10117 * least the type and uint value are modified.
10118 */
Willy Tarreauc0239e02012-04-16 14:42:55 +020010119#define CHECK_HTTP_MESSAGE_FIRST() \
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010120 do { int r = smp_prefetch_http(smp->px, smp->strm, smp->opt, args, smp, 1); if (r <= 0) return r; } while (0)
Willy Tarreauc0239e02012-04-16 14:42:55 +020010121
Willy Tarreau24e32d82012-04-23 23:55:44 +020010122#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010123 do { int r = smp_prefetch_http(smp->px, smp->strm, smp->opt, args, smp, 0); if (r <= 0) return r; } while (0)
Willy Tarreau24e32d82012-04-23 23:55:44 +020010124
Willy Tarreau8797c062007-05-07 00:55:35 +020010125
10126/* 1. Check on METHOD
10127 * We use the pre-parsed method if it is known, and store its number as an
10128 * integer. If it is unknown, we use the pointer and the length.
10129 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010130static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010131{
10132 int len, meth;
10133
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010134 len = strlen(text);
10135 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010136
10137 pattern->val.i = meth;
10138 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010139 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010140 pattern->len = len;
10141 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010142 else {
10143 pattern->ptr.str = NULL;
10144 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010145 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010146 return 1;
10147}
10148
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010149/* This function fetches the method of current HTTP request and stores
10150 * it in the global pattern struct as a chunk. There are two possibilities :
10151 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10152 * in <len> and <ptr> is NULL ;
10153 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10154 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010155 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010156 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010157static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010158smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010159{
10160 int meth;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010161 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010162
Willy Tarreau24e32d82012-04-23 23:55:44 +020010163 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010164
Willy Tarreau8797c062007-05-07 00:55:35 +020010165 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010166 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010167 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010168 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010169 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10170 /* ensure the indexes are not affected */
10171 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010172 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010173 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10174 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010175 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010176 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010177 return 1;
10178}
10179
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010180/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010181static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010182{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010183 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010184 struct pattern_list *lst;
10185 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010186
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010187 list_for_each_entry(lst, &expr->patterns, list) {
10188 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010189
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010190 /* well-known method */
10191 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010192 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010193 return pattern;
10194 else
10195 continue;
10196 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010197
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010198 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010199 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010200 continue;
10201
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010202 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010203 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10204 (!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 +010010205 return pattern;
10206 }
10207 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010208}
10209
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010210static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010211smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010212{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010213 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010214 char *ptr;
10215 int len;
10216
Willy Tarreauc0239e02012-04-16 14:42:55 +020010217 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010218
Willy Tarreau8797c062007-05-07 00:55:35 +020010219 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010220 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010221
10222 while ((len-- > 0) && (*ptr++ != '/'));
10223 if (len <= 0)
10224 return 0;
10225
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010226 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010227 smp->data.u.str.str = ptr;
10228 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010229
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010230 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010231 return 1;
10232}
10233
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010234static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010235smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010236{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010237 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010238 char *ptr;
10239 int len;
10240
Willy Tarreauc0239e02012-04-16 14:42:55 +020010241 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010242
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010243 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010244 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10245 return 0;
10246
Willy Tarreau8797c062007-05-07 00:55:35 +020010247 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010248 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010249
10250 while ((len-- > 0) && (*ptr++ != '/'));
10251 if (len <= 0)
10252 return 0;
10253
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010254 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010255 smp->data.u.str.str = ptr;
10256 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010257
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010258 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010259 return 1;
10260}
10261
10262/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010263static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010264smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010265{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010266 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010267 char *ptr;
10268 int len;
10269
Willy Tarreauc0239e02012-04-16 14:42:55 +020010270 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010271
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010272 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010273 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10274 return 0;
10275
Willy Tarreau8797c062007-05-07 00:55:35 +020010276 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010277 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010278
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010279 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010280 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010281 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010282 return 1;
10283}
10284
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010285/* returns the longest available part of the body. This requires that the body
10286 * has been waited for using http-buffer-request.
10287 */
10288static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010289smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010290{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010291 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010292 struct http_msg *msg;
10293 unsigned long len;
10294 unsigned long block1;
10295 char *body;
10296 struct chunk *temp;
10297
10298 CHECK_HTTP_MESSAGE_FIRST();
10299
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010300 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010301 msg = &txn->req;
10302 else
10303 msg = &txn->rsp;
10304
10305 len = http_body_bytes(msg);
10306 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10307
10308 block1 = len;
10309 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10310 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10311
10312 if (block1 == len) {
10313 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010314 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010315 smp->data.u.str.str = body;
10316 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010317 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10318 }
10319 else {
10320 /* buffer is wrapped, we need to defragment it */
10321 temp = get_trash_chunk();
10322 memcpy(temp->str, body, block1);
10323 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010324 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010325 smp->data.u.str.str = temp->str;
10326 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010327 smp->flags = SMP_F_VOL_TEST;
10328 }
10329 return 1;
10330}
10331
10332
10333/* returns the available length of the body. This requires that the body
10334 * has been waited for using http-buffer-request.
10335 */
10336static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010337smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010338{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010339 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010340 struct http_msg *msg;
10341
10342 CHECK_HTTP_MESSAGE_FIRST();
10343
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010344 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010345 msg = &txn->req;
10346 else
10347 msg = &txn->rsp;
10348
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010349 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010350 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010351
10352 smp->flags = SMP_F_VOL_TEST;
10353 return 1;
10354}
10355
10356
10357/* returns the advertised length of the body, or the advertised size of the
10358 * chunks available in the buffer. This requires that the body has been waited
10359 * for using http-buffer-request.
10360 */
10361static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010362smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010363{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010364 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010365 struct http_msg *msg;
10366
10367 CHECK_HTTP_MESSAGE_FIRST();
10368
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010369 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010370 msg = &txn->req;
10371 else
10372 msg = &txn->rsp;
10373
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010374 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010375 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010376
10377 smp->flags = SMP_F_VOL_TEST;
10378 return 1;
10379}
10380
10381
Willy Tarreau8797c062007-05-07 00:55:35 +020010382/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010383static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010384smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010385{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010386 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010387
Willy Tarreauc0239e02012-04-16 14:42:55 +020010388 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010389
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010390 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010391 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010392 smp->data.u.str.len = txn->req.sl.rq.u_l;
10393 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010394 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010395 return 1;
10396}
10397
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010398static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010399smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010400{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010401 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010402 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010403
Willy Tarreauc0239e02012-04-16 14:42:55 +020010404 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010405
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010406 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010407 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 +020010408 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010409 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010410
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010411 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010412 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010413 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010414 return 1;
10415}
10416
10417static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010418smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010419{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010420 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010421 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010422
Willy Tarreauc0239e02012-04-16 14:42:55 +020010423 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010424
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010425 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010426 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 +020010427 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10428 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010429
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010430 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010431 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010432 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010433 return 1;
10434}
10435
Willy Tarreau185b5c42012-04-26 15:11:51 +020010436/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10437 * Accepts an optional argument of type string containing the header field name,
10438 * and an optional argument of type signed or unsigned integer to request an
10439 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010440 * headers are considered from the first one. It does not stop on commas and
10441 * returns full lines instead (useful for User-Agent or Date for example).
10442 */
10443static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010444smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010445{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010446 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010447 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010448 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010449 int occ = 0;
10450 const char *name_str = NULL;
10451 int name_len = 0;
10452
10453 if (!ctx) {
10454 /* first call */
10455 ctx = &static_hdr_ctx;
10456 ctx->idx = 0;
10457 smp->ctx.a[0] = ctx;
10458 }
10459
10460 if (args) {
10461 if (args[0].type != ARGT_STR)
10462 return 0;
10463 name_str = args[0].data.str.str;
10464 name_len = args[0].data.str.len;
10465
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010466 if (args[1].type == ARGT_SINT)
10467 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010468 }
10469
10470 CHECK_HTTP_MESSAGE_FIRST();
10471
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010472 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010473 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 +020010474
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010475 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10476 /* search for header from the beginning */
10477 ctx->idx = 0;
10478
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010479 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010480 /* no explicit occurrence and single fetch => last header by default */
10481 occ = -1;
10482
10483 if (!occ)
10484 /* prepare to report multiple occurrences for ACL fetches */
10485 smp->flags |= SMP_F_NOT_LAST;
10486
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010487 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010488 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010489 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 +020010490 return 1;
10491
10492 smp->flags &= ~SMP_F_NOT_LAST;
10493 return 0;
10494}
10495
10496/* 6. Check on HTTP header count. The number of occurrences is returned.
10497 * Accepts exactly 1 argument of type string. It does not stop on commas and
10498 * returns full lines instead (useful for User-Agent or Date for example).
10499 */
10500static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010501smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010502{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010503 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010504 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010505 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010506 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010507 const char *name = NULL;
10508 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010509
Willy Tarreau601a4d12015-04-01 19:16:09 +020010510 if (args && args->type == ARGT_STR) {
10511 name = args->data.str.str;
10512 len = args->data.str.len;
10513 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010514
10515 CHECK_HTTP_MESSAGE_FIRST();
10516
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010517 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010518 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 +020010519
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010520 ctx.idx = 0;
10521 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010522 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010523 cnt++;
10524
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010525 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010526 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010527 smp->flags = SMP_F_VOL_HDR;
10528 return 1;
10529}
10530
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010531static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010532smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010533{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010534 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010535 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010536 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010537 struct chunk *temp;
10538 char del = ',';
10539
10540 if (args && args->type == ARGT_STR)
10541 del = *args[0].data.str.str;
10542
10543 CHECK_HTTP_MESSAGE_FIRST();
10544
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010545 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010546 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 +020010547
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010548 temp = get_trash_chunk();
10549
10550 ctx.idx = 0;
10551 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10552 if (temp->len)
10553 temp->str[temp->len++] = del;
10554 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10555 temp->len += ctx.del;
10556 }
10557
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010558 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010559 smp->data.u.str.str = temp->str;
10560 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010561 smp->flags = SMP_F_VOL_HDR;
10562 return 1;
10563}
10564
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010565/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10566 * Accepts an optional argument of type string containing the header field name,
10567 * and an optional argument of type signed or unsigned integer to request an
10568 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010569 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010570 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010571static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010572smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010573{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010574 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010575 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010576 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010577 int occ = 0;
10578 const char *name_str = NULL;
10579 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010580
Willy Tarreaua890d072013-04-02 12:01:06 +020010581 if (!ctx) {
10582 /* first call */
10583 ctx = &static_hdr_ctx;
10584 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010585 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010586 }
10587
Willy Tarreau185b5c42012-04-26 15:11:51 +020010588 if (args) {
10589 if (args[0].type != ARGT_STR)
10590 return 0;
10591 name_str = args[0].data.str.str;
10592 name_len = args[0].data.str.len;
10593
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010594 if (args[1].type == ARGT_SINT)
10595 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010596 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010597
Willy Tarreaue333ec92012-04-16 16:26:40 +020010598 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010599
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010600 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010601 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 +020010602
Willy Tarreau185b5c42012-04-26 15:11:51 +020010603 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010604 /* search for header from the beginning */
10605 ctx->idx = 0;
10606
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010607 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010608 /* no explicit occurrence and single fetch => last header by default */
10609 occ = -1;
10610
10611 if (!occ)
10612 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010613 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010614
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010615 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010616 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010617 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 +020010618 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010619
Willy Tarreau37406352012-04-23 16:16:37 +020010620 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010621 return 0;
10622}
10623
Willy Tarreauc11416f2007-06-17 16:58:38 +020010624/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010625 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010626 */
10627static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010628smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010629{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010630 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010631 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010632 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010633 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010634 const char *name = NULL;
10635 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010636
Willy Tarreau601a4d12015-04-01 19:16:09 +020010637 if (args && args->type == ARGT_STR) {
10638 name = args->data.str.str;
10639 len = args->data.str.len;
10640 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010641
Willy Tarreaue333ec92012-04-16 16:26:40 +020010642 CHECK_HTTP_MESSAGE_FIRST();
10643
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010644 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010645 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 +020010646
Willy Tarreau33a7e692007-06-10 19:45:56 +020010647 ctx.idx = 0;
10648 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010649 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010650 cnt++;
10651
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010652 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010653 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010654 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010655 return 1;
10656}
10657
Willy Tarreau185b5c42012-04-26 15:11:51 +020010658/* Fetch an HTTP header's integer value. The integer value is returned. It
10659 * takes a mandatory argument of type string and an optional one of type int
10660 * to designate a specific occurrence. It returns an unsigned integer, which
10661 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010662 */
10663static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010664smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010665{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010666 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010667
Willy Tarreauf853c462012-04-23 18:53:56 +020010668 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010669 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010670 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010671 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010672
Willy Tarreaud53e2422012-04-16 17:21:11 +020010673 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010674}
10675
Cyril Bonté69fa9922012-10-25 00:01:06 +020010676/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10677 * and an optional one of type int to designate a specific occurrence.
10678 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010679 */
10680static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010681smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010682{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010683 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010684
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010685 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010686 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010687 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010688 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010689 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010690 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010691 if (smp->data.u.str.len < temp->size - 1) {
10692 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10693 temp->str[smp->data.u.str.len] = '\0';
10694 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010695 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010696 break;
10697 }
10698 }
10699 }
10700
Willy Tarreaud53e2422012-04-16 17:21:11 +020010701 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010702 if (!(smp->flags & SMP_F_NOT_LAST))
10703 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010704 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010705 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010706}
10707
Willy Tarreau737b0c12007-06-10 21:28:46 +020010708/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10709 * the first '/' after the possible hostname, and ends before the possible '?'.
10710 */
10711static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010712smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010713{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010714 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010715 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010716
Willy Tarreauc0239e02012-04-16 14:42:55 +020010717 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010718
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010719 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010720 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010721 ptr = http_get_path(txn);
10722 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010723 return 0;
10724
10725 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010726 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010727 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010728
10729 while (ptr < end && *ptr != '?')
10730 ptr++;
10731
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010732 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010733 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010734 return 1;
10735}
10736
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010737/* This produces a concatenation of the first occurrence of the Host header
10738 * followed by the path component if it begins with a slash ('/'). This means
10739 * that '*' will not be added, resulting in exactly the first Host entry.
10740 * If no Host header is found, then the path is returned as-is. The returned
10741 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010742 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010743 */
10744static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010745smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010746{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010747 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010748 char *ptr, *end, *beg;
10749 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010750 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010751
10752 CHECK_HTTP_MESSAGE_FIRST();
10753
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010754 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010755 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010756 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010757 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010758
10759 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010760 temp = get_trash_chunk();
10761 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
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 = temp->str;
10764 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010765
10766 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010767 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010768 beg = http_get_path(txn);
10769 if (!beg)
10770 beg = end;
10771
10772 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10773
10774 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010775 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10776 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010777 }
10778
10779 smp->flags = SMP_F_VOL_1ST;
10780 return 1;
10781}
10782
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010783/* This produces a 32-bit hash of the concatenation of the first occurrence of
10784 * the Host header followed by the path component if it begins with a slash ('/').
10785 * This means that '*' will not be added, resulting in exactly the first Host
10786 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010787 * is hashed using the path hash followed by a full avalanche hash and provides a
10788 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010789 * high-traffic sites without having to store whole paths.
10790 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010791int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010792smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010793{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010794 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010795 struct hdr_ctx ctx;
10796 unsigned int hash = 0;
10797 char *ptr, *beg, *end;
10798 int len;
10799
10800 CHECK_HTTP_MESSAGE_FIRST();
10801
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010802 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010803 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010804 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010805 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10806 ptr = ctx.line + ctx.val;
10807 len = ctx.vlen;
10808 while (len--)
10809 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10810 }
10811
10812 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010813 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010814 beg = http_get_path(txn);
10815 if (!beg)
10816 beg = end;
10817
10818 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10819
10820 if (beg < ptr && *beg == '/') {
10821 while (beg < ptr)
10822 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10823 }
10824 hash = full_hash(hash);
10825
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010826 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010827 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010828 smp->flags = SMP_F_VOL_1ST;
10829 return 1;
10830}
10831
Willy Tarreau4a550602012-12-09 14:53:32 +010010832/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010833 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10834 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10835 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010836 * that in environments where IPv6 is insignificant, truncating the output to
10837 * 8 bytes would still work.
10838 */
10839static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010840smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010841{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010842 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010843 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010844
10845 if (!cli_conn)
10846 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010847
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010848 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010849 return 0;
10850
Willy Tarreau47ca5452012-12-23 20:22:19 +010010851 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010852 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010853 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010854
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010855 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010856 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010857 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010858 temp->len += 4;
10859 break;
10860 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010861 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010862 temp->len += 16;
10863 break;
10864 default:
10865 return 0;
10866 }
10867
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010868 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010869 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010870 return 1;
10871}
10872
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010873/* Extracts the query string, which comes after the question mark '?'. If no
10874 * question mark is found, nothing is returned. Otherwise it returns a sample
10875 * of type string carrying the whole query string.
10876 */
10877static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010878smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010879{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010880 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010881 char *ptr, *end;
10882
10883 CHECK_HTTP_MESSAGE_FIRST();
10884
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010885 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010886 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10887 end = ptr + txn->req.sl.rq.u_l;
10888
10889 /* look up the '?' */
10890 do {
10891 if (ptr == end)
10892 return 0;
10893 } while (*ptr++ != '?');
10894
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010895 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010896 smp->data.u.str.str = ptr;
10897 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010898 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10899 return 1;
10900}
10901
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010902static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010903smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010904{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010905 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10906 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10907 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010908
Willy Tarreau24e32d82012-04-23 23:55:44 +020010909 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010910
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010911 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010912 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010913 return 1;
10914}
10915
Willy Tarreau7f18e522010-10-22 20:04:13 +020010916/* return a valid test if the current request is the first one on the connection */
10917static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010918smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010919{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010920 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010921 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010922 return 1;
10923}
10924
Willy Tarreau34db1082012-04-19 17:16:54 +020010925/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010926static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010927smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010928{
10929
Willy Tarreau24e32d82012-04-23 23:55:44 +020010930 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010931 return 0;
10932
Willy Tarreauc0239e02012-04-16 14:42:55 +020010933 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010934
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010935 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010936 return 0;
10937
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010938 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010939 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010940 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010941 return 1;
10942}
Willy Tarreau8797c062007-05-07 00:55:35 +020010943
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010944/* Accepts exactly 1 argument of type userlist */
10945static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010946smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010947{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010948 if (!args || args->type != ARGT_USR)
10949 return 0;
10950
10951 CHECK_HTTP_MESSAGE_FIRST();
10952
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010953 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010954 return 0;
10955
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010956 /* if the user does not belong to the userlist or has a wrong password,
10957 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010958 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010959 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010960 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10961 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010962 return 0;
10963
10964 /* pat_match_auth() will need the user list */
10965 smp->ctx.a[0] = args->data.usr;
10966
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010967 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010968 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010969 smp->data.u.str.str = smp->strm->txn->auth.user;
10970 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010971
10972 return 1;
10973}
10974
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010975/* Try to find the next occurrence of a cookie name in a cookie header value.
10976 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10977 * the cookie value is returned into *value and *value_l, and the function
10978 * returns a pointer to the next pointer to search from if the value was found.
10979 * Otherwise if the cookie was not found, NULL is returned and neither value
10980 * nor value_l are touched. The input <hdr> string should first point to the
10981 * header's value, and the <hdr_end> pointer must point to the first character
10982 * not part of the value. <list> must be non-zero if value may represent a list
10983 * of values (cookie headers). This makes it faster to abort parsing when no
10984 * list is expected.
10985 */
10986static char *
10987extract_cookie_value(char *hdr, const char *hdr_end,
10988 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010989 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010990{
10991 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10992 char *next;
10993
10994 /* we search at least a cookie name followed by an equal, and more
10995 * generally something like this :
10996 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10997 */
10998 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10999 /* Iterate through all cookies on this line */
11000
11001 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
11002 att_beg++;
11003
11004 /* find att_end : this is the first character after the last non
11005 * space before the equal. It may be equal to hdr_end.
11006 */
11007 equal = att_end = att_beg;
11008
11009 while (equal < hdr_end) {
11010 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11011 break;
11012 if (http_is_spht[(unsigned char)*equal++])
11013 continue;
11014 att_end = equal;
11015 }
11016
11017 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11018 * is between <att_beg> and <equal>, both may be identical.
11019 */
11020
11021 /* look for end of cookie if there is an equal sign */
11022 if (equal < hdr_end && *equal == '=') {
11023 /* look for the beginning of the value */
11024 val_beg = equal + 1;
11025 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
11026 val_beg++;
11027
11028 /* find the end of the value, respecting quotes */
11029 next = find_cookie_value_end(val_beg, hdr_end);
11030
11031 /* make val_end point to the first white space or delimitor after the value */
11032 val_end = next;
11033 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
11034 val_end--;
11035 } else {
11036 val_beg = val_end = next = equal;
11037 }
11038
11039 /* We have nothing to do with attributes beginning with '$'. However,
11040 * they will automatically be removed if a header before them is removed,
11041 * since they're supposed to be linked together.
11042 */
11043 if (*att_beg == '$')
11044 continue;
11045
11046 /* Ignore cookies with no equal sign */
11047 if (equal == next)
11048 continue;
11049
11050 /* Now we have the cookie name between att_beg and att_end, and
11051 * its value between val_beg and val_end.
11052 */
11053
11054 if (att_end - att_beg == cookie_name_l &&
11055 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11056 /* let's return this value and indicate where to go on from */
11057 *value = val_beg;
11058 *value_l = val_end - val_beg;
11059 return next + 1;
11060 }
11061
11062 /* Set-Cookie headers only have the name in the first attr=value part */
11063 if (!list)
11064 break;
11065 }
11066
11067 return NULL;
11068}
11069
William Lallemanda43ba4e2014-01-28 18:14:25 +010011070/* Fetch a captured HTTP request header. The index is the position of
11071 * the "capture" option in the configuration file
11072 */
11073static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011074smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011075{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011076 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011077 int idx;
11078
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011079 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011080 return 0;
11081
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011082 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011083
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011084 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 +010011085 return 0;
11086
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011087 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011088 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011089 smp->data.u.str.str = smp->strm->req_cap[idx];
11090 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011091
11092 return 1;
11093}
11094
11095/* Fetch a captured HTTP response header. The index is the position of
11096 * the "capture" option in the configuration file
11097 */
11098static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011099smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011100{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011101 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011102 int idx;
11103
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011104 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011105 return 0;
11106
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011107 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011108
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011109 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 +010011110 return 0;
11111
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011112 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011113 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011114 smp->data.u.str.str = smp->strm->res_cap[idx];
11115 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011116
11117 return 1;
11118}
11119
William Lallemand65ad6e12014-01-31 15:08:02 +010011120/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11121static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011122smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011123{
11124 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011125 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011126 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011127
Willy Tarreau15e91e12015-04-04 00:52:09 +020011128 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011129 return 0;
11130
William Lallemand96a77852014-02-05 00:30:02 +010011131 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011132
William Lallemand96a77852014-02-05 00:30:02 +010011133 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11134 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011135
William Lallemand96a77852014-02-05 00:30:02 +010011136 temp = get_trash_chunk();
11137 temp->str = txn->uri;
11138 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011139 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011140 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011141 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011142
11143 return 1;
11144
11145}
11146
11147/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11148static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011149smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011150{
11151 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011152 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011153 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011154
Willy Tarreau15e91e12015-04-04 00:52:09 +020011155 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011156 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011157
William Lallemand65ad6e12014-01-31 15:08:02 +010011158 ptr = txn->uri;
11159
11160 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11161 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011162
William Lallemand65ad6e12014-01-31 15:08:02 +010011163 if (!*ptr)
11164 return 0;
11165
11166 ptr++; /* skip the space */
11167
11168 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011169 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011170 if (!ptr)
11171 return 0;
11172 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11173 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011174
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011175 smp->data.u.str = *temp;
11176 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011177 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011178 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011179
11180 return 1;
11181}
11182
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011183/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11184 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11185 */
11186static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011187smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011188{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011189 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011190
Willy Tarreau15e91e12015-04-04 00:52:09 +020011191 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011192 return 0;
11193
11194 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011195 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011196 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011197 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011198
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011199 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011200 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011201 smp->flags = SMP_F_CONST;
11202 return 1;
11203
11204}
11205
11206/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11207 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11208 */
11209static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011210smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011211{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011212 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011213
Willy Tarreau15e91e12015-04-04 00:52:09 +020011214 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011215 return 0;
11216
11217 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011218 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011219 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011220 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011221
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011222 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011223 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011224 smp->flags = SMP_F_CONST;
11225 return 1;
11226
11227}
11228
William Lallemand65ad6e12014-01-31 15:08:02 +010011229
Willy Tarreaue333ec92012-04-16 16:26:40 +020011230/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011231 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011232 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011233 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011234 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011235 * Accepts exactly 1 argument of type string. If the input options indicate
11236 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011237 * The returned sample is of type CSTR. Can be used to parse cookies in other
11238 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011239 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011240int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011241{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011242 struct http_txn *txn;
11243 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011244 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011245 const struct http_msg *msg;
11246 const char *hdr_name;
11247 int hdr_name_len;
11248 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011249 int occ = 0;
11250 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011251
Willy Tarreau24e32d82012-04-23 23:55:44 +020011252 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011253 return 0;
11254
Willy Tarreaua890d072013-04-02 12:01:06 +020011255 if (!ctx) {
11256 /* first call */
11257 ctx = &static_hdr_ctx;
11258 ctx->idx = 0;
11259 smp->ctx.a[2] = ctx;
11260 }
11261
Willy Tarreaue333ec92012-04-16 16:26:40 +020011262 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011263
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011264 txn = smp->strm->txn;
11265 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011266
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011267 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011268 msg = &txn->req;
11269 hdr_name = "Cookie";
11270 hdr_name_len = 6;
11271 } else {
11272 msg = &txn->rsp;
11273 hdr_name = "Set-Cookie";
11274 hdr_name_len = 10;
11275 }
11276
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011277 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011278 /* no explicit occurrence and single fetch => last cookie by default */
11279 occ = -1;
11280
11281 /* OK so basically here, either we want only one value and it's the
11282 * last one, or we want to iterate over all of them and we fetch the
11283 * next one.
11284 */
11285
Willy Tarreau9b28e032012-10-12 23:49:43 +020011286 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011287 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011288 /* search for the header from the beginning, we must first initialize
11289 * the search parameters.
11290 */
Willy Tarreau37406352012-04-23 16:16:37 +020011291 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011292 ctx->idx = 0;
11293 }
11294
Willy Tarreau28376d62012-04-26 21:26:10 +020011295 smp->flags |= SMP_F_VOL_HDR;
11296
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011297 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011298 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11299 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011300 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11301 goto out;
11302
Willy Tarreau24e32d82012-04-23 23:55:44 +020011303 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011304 continue;
11305
Willy Tarreau37406352012-04-23 16:16:37 +020011306 smp->ctx.a[0] = ctx->line + ctx->val;
11307 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011308 }
11309
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011310 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011311 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011312 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011313 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011314 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011315 &smp->data.u.str.str,
11316 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011317 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011318 found = 1;
11319 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011320 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011321 smp->flags |= SMP_F_NOT_LAST;
11322 return 1;
11323 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011324 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011325 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011326 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011327 /* all cookie headers and values were scanned. If we're looking for the
11328 * last occurrence, we may return it now.
11329 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011330 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011331 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011332 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011333}
11334
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011335/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011336 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011337 * multiple cookies may be parsed on the same line. The returned sample is of
11338 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011339 */
11340static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011341smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011342{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011343 struct http_txn *txn;
11344 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011345 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011346 const struct http_msg *msg;
11347 const char *hdr_name;
11348 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011349 int cnt;
11350 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011351 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011352
Willy Tarreau24e32d82012-04-23 23:55:44 +020011353 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011354 return 0;
11355
Willy Tarreaue333ec92012-04-16 16:26:40 +020011356 CHECK_HTTP_MESSAGE_FIRST();
11357
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011358 txn = smp->strm->txn;
11359 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011360
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011361 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011362 msg = &txn->req;
11363 hdr_name = "Cookie";
11364 hdr_name_len = 6;
11365 } else {
11366 msg = &txn->rsp;
11367 hdr_name = "Set-Cookie";
11368 hdr_name_len = 10;
11369 }
11370
Willy Tarreau9b28e032012-10-12 23:49:43 +020011371 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011372 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011373 ctx.idx = 0;
11374 cnt = 0;
11375
11376 while (1) {
11377 /* Note: val_beg == NULL every time we need to fetch a new header */
11378 if (!val_beg) {
11379 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11380 break;
11381
Willy Tarreau24e32d82012-04-23 23:55:44 +020011382 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011383 continue;
11384
11385 val_beg = ctx.line + ctx.val;
11386 val_end = val_beg + ctx.vlen;
11387 }
11388
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011389 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011390 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011391 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011392 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011393 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011394 &smp->data.u.str.str,
11395 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011396 cnt++;
11397 }
11398 }
11399
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011400 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011401 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011402 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011403 return 1;
11404}
11405
Willy Tarreau51539362012-05-08 12:46:28 +020011406/* Fetch an cookie's integer value. The integer value is returned. It
11407 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11408 */
11409static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011410smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011411{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011412 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011413
11414 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011415 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011416 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011417 }
11418
11419 return ret;
11420}
11421
Willy Tarreau8797c062007-05-07 00:55:35 +020011422/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011423/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011424/************************************************************************/
11425
David Cournapeau16023ee2010-12-23 20:55:41 +090011426/*
11427 * Given a path string and its length, find the position of beginning of the
11428 * query string. Returns NULL if no query string is found in the path.
11429 *
11430 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11431 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011432 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011433 */
bedis4c75cca2012-10-05 08:38:24 +020011434static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011435{
11436 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011437
bedis4c75cca2012-10-05 08:38:24 +020011438 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011439 return p ? p + 1 : NULL;
11440}
11441
bedis4c75cca2012-10-05 08:38:24 +020011442static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011443{
bedis4c75cca2012-10-05 08:38:24 +020011444 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011445}
11446
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011447/* after increasing a pointer value, it can exceed the first buffer
11448 * size. This function transform the value of <ptr> according with
11449 * the expected position. <chunks> is an array of the one or two
11450 * avalaible chunks. The first value is the start of the first chunk,
11451 * the second value if the end+1 of the first chunks. The third value
11452 * is NULL or the start of the second chunk and the fourth value is
11453 * the end+1 of the second chunk. The function returns 1 if does a
11454 * wrap, else returns 0.
11455 */
11456static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11457{
11458 if (*ptr < chunks[1])
11459 return 0;
11460 if (!chunks[2])
11461 return 0;
11462 *ptr = chunks[2] + ( *ptr - chunks[1] );
11463 return 1;
11464}
11465
David Cournapeau16023ee2010-12-23 20:55:41 +090011466/*
11467 * Given a url parameter, find the starting position of the first occurence,
11468 * or NULL if the parameter is not found.
11469 *
11470 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11471 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011472 *
11473 * Warning:this function returns a pointer that can be point to the first chunk
11474 * or the second chunk. The caller must be check the position before using the
11475 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011476 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011477static const char *
11478find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011479 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011480 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011481{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011482 const char *pos, *last, *equal;
11483 const char **bufs = chunks;
11484 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011485
David Cournapeau16023ee2010-12-23 20:55:41 +090011486
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011487 pos = bufs[0];
11488 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011489 while (pos <= last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011490 /* Check the equal. */
11491 equal = pos + url_param_name_l;
11492 if (fix_pointer_if_wrap(chunks, &equal)) {
11493 if (equal >= chunks[3])
11494 return NULL;
11495 } else {
11496 if (equal >= chunks[1])
11497 return NULL;
11498 }
11499 if (*equal == '=') {
11500 if (pos + url_param_name_l > last) {
11501 /* process wrap case, we detect a wrap. In this case, the
11502 * comparison is performed in two parts.
11503 */
11504
11505 /* This is the end, we dont have any other chunk. */
11506 if (bufs != chunks || !bufs[2])
11507 return NULL;
11508
11509 /* Compute the length of each part of the comparison. */
11510 l1 = last - pos;
11511 l2 = url_param_name_l - l1;
11512
11513 /* The second buffer is too short to contain the compared string. */
11514 if (bufs[2] + l2 > bufs[3])
11515 return NULL;
11516
11517 if (memcmp(pos, url_param_name, l1) == 0 &&
11518 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11519 return pos;
11520
11521 /* Perform wrapping and jump the string who fail the comparison. */
11522 bufs += 2;
11523 pos = bufs[0] + l2;
11524 last = bufs[1];
11525
11526 } else {
11527 /* process a simple comparison. */
11528 if (memcmp(pos, url_param_name, url_param_name_l) == 0) {
11529 return pos; }
11530 pos += url_param_name_l + 1;
11531 if (fix_pointer_if_wrap(chunks, &pos))
11532 last = bufs[2];
11533 }
11534 }
11535
11536 while (1) {
11537 /* Look for the next delimiter. */
11538 while (pos <= last && !is_param_delimiter(*pos, delim))
11539 pos++;
11540 if (pos < last)
11541 break;
11542 /* process buffer wrapping. */
11543 if (bufs != chunks || !bufs[2])
11544 return NULL;
11545 bufs += 2;
11546 pos = bufs[0];
11547 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011548 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011549 pos++;
11550 }
11551 return NULL;
11552}
11553
11554/*
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011555 * Given a url parameter name and a query string, returns its value and size
11556 * into *value and *value_l respectively, and returns non-zero. An empty
11557 * url_param_name matches the first available parameter. If the parameter is
11558 * not found, zero is returned and value/value_l are not touched.
David Cournapeau16023ee2010-12-23 20:55:41 +090011559 */
11560static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011561find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011562 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011563 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011564{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011565 const char *arg_start, *qs_end;
11566 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011567
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011568 arg_start = chunks[0];
11569 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011570 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011571 /* Looks for an argument name. */
11572 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011573 url_param_name, url_param_name_l,
11574 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011575 /* Check for wrapping. */
11576 if (arg_start > qs_end)
11577 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011578 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011579 if (!arg_start)
11580 return 0;
11581
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011582 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011583 while (1) {
11584 /* looks for the first argument. */
11585 value_start = memchr(arg_start, '=', qs_end - arg_start);
11586 if (!value_start) {
11587
11588 /* Check for wrapping. */
11589 if (arg_start >= chunks[0] &&
11590 arg_start <= chunks[1] &&
11591 chunks[2]) {
11592 arg_start = chunks[2];
11593 qs_end = chunks[3];
11594 continue;
11595 }
11596 return 0;
11597 }
11598 break;
11599 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011600 value_start++;
11601 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011602 else {
11603 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011604 value_start = arg_start + url_param_name_l + 1;
11605
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011606 /* Check for pointer wrapping. */
11607 if (fix_pointer_if_wrap(chunks, &value_start)) {
11608 /* Update the end pointer. */
11609 qs_end = chunks[3];
11610
11611 /* Check for overflow. */
11612 if (value_start > qs_end)
11613 return 0;
11614 }
11615 }
11616
David Cournapeau16023ee2010-12-23 20:55:41 +090011617 value_end = value_start;
11618
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011619 while (1) {
11620 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11621 value_end++;
11622 if (value_end < qs_end)
11623 break;
11624 /* process buffer wrapping. */
11625 if (value_end >= chunks[0] &&
11626 value_end <= chunks[1] &&
11627 chunks[2]) {
11628 value_end = chunks[2];
11629 qs_end = chunks[3];
11630 continue;
11631 }
11632 break;
11633 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011634
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011635 *vstart = value_start;
11636 *vend = value_end;
Willy Tarreau00134332011-01-04 14:57:34 +010011637 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011638}
11639
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011640/* This scans a URL-encoded query string. It takes an optionally wrapping
11641 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11642 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11643 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011644 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011645static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011646smp_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 +090011647{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011648 const char *vstart, *vend;
11649 struct chunk *temp;
11650 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011651
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011652 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011653 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011654 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011655 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011656 return 0;
11657
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011658 /* Create sample. If the value is contiguous, return the pointer as CONST,
11659 * if the value is wrapped, copy-it in a buffer.
11660 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011661 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011662 if (chunks[2] &&
11663 vstart >= chunks[0] && vstart <= chunks[1] &&
11664 vend >= chunks[2] && vend <= chunks[3]) {
11665 /* Wrapped case. */
11666 temp = get_trash_chunk();
11667 memcpy(temp->str, vstart, chunks[1] - vstart);
11668 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011669 smp->data.u.str.str = temp->str;
11670 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011671 } else {
11672 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011673 smp->data.u.str.str = (char *)vstart;
11674 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011675 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11676 }
11677
11678 /* Update context, check wrapping. */
11679 chunks[0] = vend;
11680 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11681 chunks[1] = chunks[3];
11682 chunks[2] = NULL;
11683 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011684
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011685 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011686 smp->flags |= SMP_F_NOT_LAST;
11687
David Cournapeau16023ee2010-12-23 20:55:41 +090011688 return 1;
11689}
11690
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011691/* This function iterates over each parameter of the query string. It uses
11692 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011693 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11694 * An optional parameter name is passed in args[0], otherwise any parameter is
11695 * considered. It supports an optional delimiter argument for the beginning of
11696 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011697 */
11698static int
11699smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11700{
11701 struct http_msg *msg;
11702 char delim = '?';
11703 const char *name;
11704 int name_len;
11705
Dragan Dosen26f77e52015-05-25 10:02:11 +020011706 if (!args ||
11707 (args[0].type && args[0].type != ARGT_STR) ||
11708 (args[1].type && args[1].type != ARGT_STR))
11709 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011710
Dragan Dosen26f77e52015-05-25 10:02:11 +020011711 name = "";
11712 name_len = 0;
11713 if (args->type == ARGT_STR) {
11714 name = args->data.str.str;
11715 name_len = args->data.str.len;
11716 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011717
Dragan Dosen26f77e52015-05-25 10:02:11 +020011718 if (args[1].type)
11719 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011720
Dragan Dosen26f77e52015-05-25 10:02:11 +020011721 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011722 CHECK_HTTP_MESSAGE_FIRST();
11723
11724 msg = &smp->strm->txn->req;
11725
11726 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11727 msg->sl.rq.u_l, delim);
11728 if (!smp->ctx.a[0])
11729 return 0;
11730
11731 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011732
11733 /* Assume that the context is filled with NULL pointer
11734 * before the first call.
11735 * smp->ctx.a[2] = NULL;
11736 * smp->ctx.a[3] = NULL;
11737 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011738 }
11739
11740 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11741}
11742
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011743/* This function iterates over each parameter of the body. This requires
11744 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011745 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11746 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11747 * optional second part if the body wraps at the end of the buffer. An optional
11748 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011749 */
11750static int
11751smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11752{
11753 struct http_txn *txn = smp->strm->txn;
11754 struct http_msg *msg;
11755 unsigned long len;
11756 unsigned long block1;
11757 char *body;
11758 const char *name;
11759 int name_len;
11760
11761 if (!args || (args[0].type && args[0].type != ARGT_STR))
11762 return 0;
11763
11764 name = "";
11765 name_len = 0;
11766 if (args[0].type == ARGT_STR) {
11767 name = args[0].data.str.str;
11768 name_len = args[0].data.str.len;
11769 }
11770
11771 if (!smp->ctx.a[0]) { // first call, find the query string
11772 CHECK_HTTP_MESSAGE_FIRST();
11773
11774 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
11775 msg = &txn->req;
11776 else
11777 msg = &txn->rsp;
11778
11779 len = http_body_bytes(msg);
11780 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11781
11782 block1 = len;
11783 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11784 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11785
11786 if (block1 == len) {
11787 /* buffer is not wrapped (or empty) */
11788 smp->ctx.a[0] = body;
11789 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011790
11791 /* Assume that the context is filled with NULL pointer
11792 * before the first call.
11793 * smp->ctx.a[2] = NULL;
11794 * smp->ctx.a[3] = NULL;
11795 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011796 }
11797 else {
11798 /* buffer is wrapped, we need to defragment it */
11799 smp->ctx.a[0] = body;
11800 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011801 smp->ctx.a[2] = msg->chn->buf->data;
11802 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011803 }
11804 }
11805 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11806}
11807
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011808/* Return the signed integer value for the specified url parameter (see url_param
11809 * above).
11810 */
11811static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011812smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011813{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011814 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011815
11816 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011817 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011818 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011819 }
11820
11821 return ret;
11822}
11823
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011824/* This produces a 32-bit hash of the concatenation of the first occurrence of
11825 * the Host header followed by the path component if it begins with a slash ('/').
11826 * This means that '*' will not be added, resulting in exactly the first Host
11827 * entry. If no Host header is found, then the path is used. The resulting value
11828 * is hashed using the url hash followed by a full avalanche hash and provides a
11829 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11830 * high-traffic sites without having to store whole paths.
11831 * this differs from the base32 functions in that it includes the url parameters
11832 * as well as the path
11833 */
11834static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011835smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011836{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011837 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011838 struct hdr_ctx ctx;
11839 unsigned int hash = 0;
11840 char *ptr, *beg, *end;
11841 int len;
11842
11843 CHECK_HTTP_MESSAGE_FIRST();
11844
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011845 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011846 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011847 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011848 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11849 ptr = ctx.line + ctx.val;
11850 len = ctx.vlen;
11851 while (len--)
11852 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11853 }
11854
11855 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011856 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 +000011857 beg = http_get_path(txn);
11858 if (!beg)
11859 beg = end;
11860
11861 for (ptr = beg; ptr < end ; ptr++);
11862
11863 if (beg < ptr && *beg == '/') {
11864 while (beg < ptr)
11865 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11866 }
11867 hash = full_hash(hash);
11868
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011869 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011870 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011871 smp->flags = SMP_F_VOL_1ST;
11872 return 1;
11873}
11874
11875/* This concatenates the source address with the 32-bit hash of the Host and
11876 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11877 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11878 * on the source address length. The URL hash is stored before the address so
11879 * that in environments where IPv6 is insignificant, truncating the output to
11880 * 8 bytes would still work.
11881 */
11882static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011883smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011884{
11885 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011886 struct connection *cli_conn = objt_conn(smp->sess->origin);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011887 unsigned int hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011888
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011889 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011890 return 0;
11891
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011892 /* The returned hash is a 32 bytes integer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011893 hash = smp->data.u.sint;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011894
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011895 temp = get_trash_chunk();
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011896 memcpy(temp->str + temp->len, &hash, sizeof(hash));
11897 temp->len += sizeof(hash);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011898
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011899 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011900 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011901 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011902 temp->len += 4;
11903 break;
11904 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011905 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011906 temp->len += 16;
11907 break;
11908 default:
11909 return 0;
11910 }
11911
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011912 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011913 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011914 return 1;
11915}
11916
Willy Tarreau185b5c42012-04-26 15:11:51 +020011917/* This function is used to validate the arguments passed to any "hdr" fetch
11918 * keyword. These keywords support an optional positive or negative occurrence
11919 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11920 * is assumed that the types are already the correct ones. Returns 0 on error,
11921 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11922 * error message in case of error, that the caller is responsible for freeing.
11923 * The initial location must either be freeable or NULL.
11924 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011925int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011926{
11927 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011928 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011929 return 0;
11930 }
11931 return 1;
11932}
11933
Willy Tarreau276fae92013-07-25 14:36:01 +020011934/* takes an UINT value on input supposed to represent the time since EPOCH,
11935 * adds an optional offset found in args[0] and emits a string representing
11936 * the date in RFC-1123/5322 format.
11937 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011938static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011939{
11940 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11941 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11942 struct chunk *temp;
11943 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011944 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011945 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011946
11947 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011948 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011949 curr_date += args[0].data.sint;
11950
11951 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011952 if (!tm)
11953 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011954
11955 temp = get_trash_chunk();
11956 temp->len = snprintf(temp->str, temp->size - temp->len,
11957 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11958 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11959 tm->tm_hour, tm->tm_min, tm->tm_sec);
11960
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011961 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011962 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011963 return 1;
11964}
11965
Thierry FOURNIERad903512014-04-11 17:51:01 +020011966/* Match language range with language tag. RFC2616 14.4:
11967 *
11968 * A language-range matches a language-tag if it exactly equals
11969 * the tag, or if it exactly equals a prefix of the tag such
11970 * that the first tag character following the prefix is "-".
11971 *
11972 * Return 1 if the strings match, else return 0.
11973 */
11974static inline int language_range_match(const char *range, int range_len,
11975 const char *tag, int tag_len)
11976{
11977 const char *end = range + range_len;
11978 const char *tend = tag + tag_len;
11979 while (range < end) {
11980 if (*range == '-' && tag == tend)
11981 return 1;
11982 if (*range != *tag || tag == tend)
11983 return 0;
11984 range++;
11985 tag++;
11986 }
11987 /* Return true only if the last char of the tag is matched. */
11988 return tag == tend;
11989}
11990
11991/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011992static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011993{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011994 const char *al = smp->data.u.str.str;
11995 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011996 const char *token;
11997 int toklen;
11998 int qvalue;
11999 const char *str;
12000 const char *w;
12001 int best_q = 0;
12002
12003 /* Set the constant to the sample, because the output of the
12004 * function will be peek in the constant configuration string.
12005 */
12006 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012007 smp->data.u.str.size = 0;
12008 smp->data.u.str.str = "";
12009 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012010
12011 /* Parse the accept language */
12012 while (1) {
12013
12014 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012015 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012016 al++;
12017 if (al >= end)
12018 break;
12019
12020 /* Start of the fisrt word. */
12021 token = al;
12022
12023 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012024 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012025 al++;
12026 if (al == token)
12027 goto expect_comma;
12028
12029 /* Length of the token. */
12030 toklen = al - token;
12031 qvalue = 1000;
12032
12033 /* Check if the token exists in the list. If the token not exists,
12034 * jump to the next token.
12035 */
12036 str = args[0].data.str.str;
12037 w = str;
12038 while (1) {
12039 if (*str == ';' || *str == '\0') {
12040 if (language_range_match(token, toklen, w, str-w))
12041 goto look_for_q;
12042 if (*str == '\0')
12043 goto expect_comma;
12044 w = str + 1;
12045 }
12046 str++;
12047 }
12048 goto expect_comma;
12049
12050look_for_q:
12051
12052 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012053 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012054 al++;
12055 if (al >= end)
12056 goto process_value;
12057
12058 /* If ',' is found, process the result */
12059 if (*al == ',')
12060 goto process_value;
12061
12062 /* If the character is different from ';', look
12063 * for the end of the header part in best effort.
12064 */
12065 if (*al != ';')
12066 goto expect_comma;
12067
12068 /* Assumes that the char is ';', now expect "q=". */
12069 al++;
12070
12071 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012072 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012073 al++;
12074 if (al >= end)
12075 goto process_value;
12076
12077 /* Expect 'q'. If no 'q', continue in best effort */
12078 if (*al != 'q')
12079 goto process_value;
12080 al++;
12081
12082 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012083 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012084 al++;
12085 if (al >= end)
12086 goto process_value;
12087
12088 /* Expect '='. If no '=', continue in best effort */
12089 if (*al != '=')
12090 goto process_value;
12091 al++;
12092
12093 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012094 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012095 al++;
12096 if (al >= end)
12097 goto process_value;
12098
12099 /* Parse the q value. */
12100 qvalue = parse_qvalue(al, &al);
12101
12102process_value:
12103
12104 /* If the new q value is the best q value, then store the associated
12105 * language in the response. If qvalue is the biggest value (1000),
12106 * break the process.
12107 */
12108 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012109 smp->data.u.str.str = (char *)w;
12110 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012111 if (qvalue >= 1000)
12112 break;
12113 best_q = qvalue;
12114 }
12115
12116expect_comma:
12117
12118 /* Expect comma or end. If the end is detected, quit the loop. */
12119 while (al < end && *al != ',')
12120 al++;
12121 if (al >= end)
12122 break;
12123
12124 /* Comma is found, jump it and restart the analyzer. */
12125 al++;
12126 }
12127
12128 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012129 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12130 smp->data.u.str.str = args[1].data.str.str;
12131 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012132 }
12133
12134 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012135 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012136}
12137
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012138/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012139static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012140{
12141 /* If the constant flag is set or if not size is avalaible at
12142 * the end of the buffer, copy the string in other buffer
12143 * before decoding.
12144 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012145 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012146 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012147 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12148 smp->data.u.str.str = str->str;
12149 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012150 smp->flags &= ~SMP_F_CONST;
12151 }
12152
12153 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012154 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12155 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012156 return 1;
12157}
12158
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012159static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12160{
12161 struct proxy *fe = strm_fe(smp->strm);
12162 int idx, i;
12163 struct cap_hdr *hdr;
12164 int len;
12165
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012166 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012167 return 0;
12168
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012169 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012170
12171 /* Check the availibity of the capture id. */
12172 if (idx > fe->nb_req_cap - 1)
12173 return 0;
12174
12175 /* Look for the original configuration. */
12176 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12177 hdr != NULL && i != idx ;
12178 i--, hdr = hdr->next);
12179 if (!hdr)
12180 return 0;
12181
12182 /* check for the memory allocation */
12183 if (smp->strm->req_cap[hdr->index] == NULL)
12184 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12185 if (smp->strm->req_cap[hdr->index] == NULL)
12186 return 0;
12187
12188 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012189 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012190 if (len > hdr->len)
12191 len = hdr->len;
12192
12193 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012194 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012195 smp->strm->req_cap[idx][len] = '\0';
12196
12197 return 1;
12198}
12199
12200static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12201{
12202 struct proxy *fe = strm_fe(smp->strm);
12203 int idx, i;
12204 struct cap_hdr *hdr;
12205 int len;
12206
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012207 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012208 return 0;
12209
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012210 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012211
12212 /* Check the availibity of the capture id. */
12213 if (idx > fe->nb_rsp_cap - 1)
12214 return 0;
12215
12216 /* Look for the original configuration. */
12217 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12218 hdr != NULL && i != idx ;
12219 i--, hdr = hdr->next);
12220 if (!hdr)
12221 return 0;
12222
12223 /* check for the memory allocation */
12224 if (smp->strm->res_cap[hdr->index] == NULL)
12225 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12226 if (smp->strm->res_cap[hdr->index] == NULL)
12227 return 0;
12228
12229 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012230 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012231 if (len > hdr->len)
12232 len = hdr->len;
12233
12234 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012235 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012236 smp->strm->res_cap[idx][len] = '\0';
12237
12238 return 1;
12239}
12240
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012241/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012242 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012243 * the relevant part of the request line accordingly. Then it updates various
12244 * pointers to the next elements which were moved, and the total buffer length.
12245 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012246 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12247 * error, though this can be revisited when this code is finally exploited.
12248 *
12249 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12250 * query string and 3 to replace uri.
12251 *
12252 * In query string case, the mark question '?' must be set at the start of the
12253 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012254 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012255int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012256 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012257{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012258 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012259 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012260 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012261 int delta;
12262
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012263 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012264 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012265 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012266 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12267
12268 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012269 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012270 txn->req.sl.rq.m_l += delta;
12271 txn->req.sl.rq.u += delta;
12272 txn->req.sl.rq.v += delta;
12273 break;
12274
12275 case 1: // path
12276 cur_ptr = http_get_path(txn);
12277 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012278 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012279
12280 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012281 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 +010012282 cur_end++;
12283
12284 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012285 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012286 txn->req.sl.rq.u_l += delta;
12287 txn->req.sl.rq.v += delta;
12288 break;
12289
12290 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012291 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012292 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012293 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12294 while (cur_ptr < cur_end && *cur_ptr != '?')
12295 cur_ptr++;
12296
12297 /* skip the question mark or indicate that we must insert it
12298 * (but only if the format string is not empty then).
12299 */
12300 if (cur_ptr < cur_end)
12301 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012302 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012303 offset = 0;
12304
12305 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012306 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012307 txn->req.sl.rq.u_l += delta;
12308 txn->req.sl.rq.v += delta;
12309 break;
12310
12311 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012312 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012313 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12314
12315 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012316 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012317 txn->req.sl.rq.u_l += delta;
12318 txn->req.sl.rq.v += delta;
12319 break;
12320
12321 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012322 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012323 }
12324
12325 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012326 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012327 txn->req.sl.rq.l += delta;
12328 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012329 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012330 return 0;
12331}
12332
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012333/* This function replace the HTTP status code and the associated message. The
12334 * variable <status> contains the new status code. This function never fails.
12335 */
12336void http_set_status(unsigned int status, struct stream *s)
12337{
12338 struct http_txn *txn = s->txn;
12339 char *cur_ptr, *cur_end;
12340 int delta;
12341 char *res;
12342 int c_l;
12343 const char *msg;
12344 int msg_len;
12345
12346 chunk_reset(&trash);
12347
12348 res = ultoa_o(status, trash.str, trash.size);
12349 c_l = res - trash.str;
12350
12351 trash.str[c_l] = ' ';
12352 trash.len = c_l + 1;
12353
12354 msg = get_reason(status);
12355 msg_len = strlen(msg);
12356
12357 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12358 trash.len += msg_len;
12359
12360 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12361 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12362
12363 /* commit changes and adjust message */
12364 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12365
12366 /* adjust res line offsets and lengths */
12367 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12368 txn->rsp.sl.st.c_l = c_l;
12369 txn->rsp.sl.st.r_l = msg_len;
12370
12371 delta = trash.len - (cur_end - cur_ptr);
12372 txn->rsp.sl.st.l += delta;
12373 txn->hdr_idx.v[0].len += delta;
12374 http_msg_move_end(&txn->rsp, delta);
12375}
12376
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012377/* This function executes one of the set-{method,path,query,uri} actions. It
12378 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012379 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012380 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012381 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12382 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012383 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012384enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
12385 struct session *sess, struct stream *s)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012386{
12387 chunk_reset(&trash);
12388
12389 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012390 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012391 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012392 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012393
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012394 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012395 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012396}
12397
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012398/* This function is just a compliant action wrapper for "set-status". */
12399enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
12400 struct session *sess, struct stream *s)
12401{
12402 http_set_status(rule->arg.status.code, s);
12403 return ACT_RET_CONT;
12404}
12405
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012406/* parse an http-request action among :
12407 * set-method
12408 * set-path
12409 * set-query
12410 * set-uri
12411 *
12412 * All of them accept a single argument of type string representing a log-format.
12413 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12414 * 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 +020012415 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012416 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012417enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12418 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012419{
12420 int cur_arg = *orig_arg;
12421
Thierry FOURNIER42148732015-09-02 17:17:33 +020012422 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012423
12424 switch (args[0][4]) {
12425 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012426 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012427 rule->action_ptr = http_action_set_req_line;
12428 break;
12429 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012430 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012431 rule->action_ptr = http_action_set_req_line;
12432 break;
12433 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012434 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012435 rule->action_ptr = http_action_set_req_line;
12436 break;
12437 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012438 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012439 rule->action_ptr = http_action_set_req_line;
12440 break;
12441 default:
12442 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012443 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012444 }
12445
12446 if (!*args[cur_arg] ||
12447 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12448 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012449 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012450 }
12451
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012452 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012453 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012454 parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012455 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12456 proxy->conf.args.file, proxy->conf.args.line);
12457
12458 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012459 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012460}
12461
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012462/* parse set-status action:
12463 * This action accepts a single argument of type int representing
12464 * an http status code. It returns ACT_RET_PRS_OK on success,
12465 * ACT_RET_PRS_ERR on error.
12466 */
12467enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12468 struct act_rule *rule, char **err)
12469{
12470 char *error;
12471
Thierry FOURNIER42148732015-09-02 17:17:33 +020012472 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012473 rule->action_ptr = action_http_set_status;
12474
12475 /* Check if an argument is available */
12476 if (!*args[*orig_arg]) {
12477 memprintf(err, "expects exactly 1 argument <status>");
12478 return ACT_RET_PRS_ERR;
12479 }
12480
12481 /* convert status code as integer */
12482 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12483 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12484 memprintf(err, "expects an integer status code between 100 and 999");
12485 return ACT_RET_PRS_ERR;
12486 }
12487
12488 (*orig_arg)++;
12489 return ACT_RET_PRS_OK;
12490}
12491
Willy Tarreaua9083d02015-05-08 15:27:59 +020012492/* This function executes the "capture" action. It executes a fetch expression,
12493 * turns the result into a string and puts it in a capture slot. It always
12494 * returns 1. If an error occurs the action is cancelled, but the rule
12495 * processing continues.
12496 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012497enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
12498 struct session *sess, struct stream *s)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012499{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012500 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012501 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012502 char **cap = s->req_cap;
12503 int len;
12504
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012505 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 +020012506 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012507 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012508
12509 if (cap[h->index] == NULL)
12510 cap[h->index] = pool_alloc2(h->pool);
12511
12512 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012513 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012514
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012515 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012516 if (len > h->len)
12517 len = h->len;
12518
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012519 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012520 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012521 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012522}
12523
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012524/* This function executes the "capture" action and store the result in a
12525 * capture slot if exists. It executes a fetch expression, turns the result
12526 * into a string and puts it in a capture slot. It always returns 1. If an
12527 * error occurs the action is cancelled, but the rule processing continues.
12528 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012529enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
12530 struct session *sess, struct stream *s)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012531{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012532 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012533 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012534 char **cap = s->req_cap;
12535 struct proxy *fe = strm_fe(s);
12536 int len;
12537 int i;
12538
12539 /* Look for the original configuration. */
12540 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012541 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012542 i--, h = h->next);
12543 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012544 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012545
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012546 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 +020012547 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012548 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012549
12550 if (cap[h->index] == NULL)
12551 cap[h->index] = pool_alloc2(h->pool);
12552
12553 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012554 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012555
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012556 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012557 if (len > h->len)
12558 len = h->len;
12559
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012560 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012561 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012562 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012563}
12564
Willy Tarreaua9083d02015-05-08 15:27:59 +020012565/* parse an "http-request capture" action. It takes a single argument which is
12566 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012567 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012568 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012569 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012570enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12571 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012572{
12573 struct sample_expr *expr;
12574 struct cap_hdr *hdr;
12575 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012576 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012577
12578 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12579 if (strcmp(args[cur_arg], "if") == 0 ||
12580 strcmp(args[cur_arg], "unless") == 0)
12581 break;
12582
12583 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012584 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012585 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012586 }
12587
Willy Tarreaua9083d02015-05-08 15:27:59 +020012588 cur_arg = *orig_arg;
12589 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12590 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012591 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012592
12593 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12594 memprintf(err,
12595 "fetch method '%s' extracts information from '%s', none of which is available here",
12596 args[cur_arg-1], sample_src_names(expr->fetch->use));
12597 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012598 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012599 }
12600
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012601 if (!args[cur_arg] || !*args[cur_arg]) {
12602 memprintf(err, "expects 'len or 'id'");
12603 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012604 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012605 }
12606
Willy Tarreaua9083d02015-05-08 15:27:59 +020012607 if (strcmp(args[cur_arg], "len") == 0) {
12608 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012609
12610 if (!(px->cap & PR_CAP_FE)) {
12611 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012612 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012613 }
12614
12615 proxy->conf.args.ctx = ARGC_CAP;
12616
Willy Tarreaua9083d02015-05-08 15:27:59 +020012617 if (!args[cur_arg]) {
12618 memprintf(err, "missing length value");
12619 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012620 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012621 }
12622 /* we copy the table name for now, it will be resolved later */
12623 len = atoi(args[cur_arg]);
12624 if (len <= 0) {
12625 memprintf(err, "length must be > 0");
12626 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012627 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012628 }
12629 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012630
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012631 if (!len) {
12632 memprintf(err, "a positive 'len' argument is mandatory");
12633 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012634 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012635 }
12636
12637 hdr = calloc(sizeof(struct cap_hdr), 1);
12638 hdr->next = px->req_cap;
12639 hdr->name = NULL; /* not a header capture */
12640 hdr->namelen = 0;
12641 hdr->len = len;
12642 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12643 hdr->index = px->nb_req_cap++;
12644
12645 px->req_cap = hdr;
12646 px->to_log |= LW_REQHDR;
12647
Thierry FOURNIER42148732015-09-02 17:17:33 +020012648 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012649 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012650 rule->arg.cap.expr = expr;
12651 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012652 }
12653
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012654 else if (strcmp(args[cur_arg], "id") == 0) {
12655 int id;
12656 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012657
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012658 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012659
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012660 if (!args[cur_arg]) {
12661 memprintf(err, "missing id value");
12662 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012663 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012664 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012665
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012666 id = strtol(args[cur_arg], &error, 10);
12667 if (*error != '\0') {
12668 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
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 cur_arg++;
12673
12674 proxy->conf.args.ctx = ARGC_CAP;
12675
Thierry FOURNIER42148732015-09-02 17:17:33 +020012676 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012677 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012678 rule->arg.capid.expr = expr;
12679 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012680 }
12681
12682 else {
12683 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12684 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012685 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012686 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012687
12688 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012689 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012690}
12691
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012692/* This function executes the "capture" action and store the result in a
12693 * capture slot if exists. It executes a fetch expression, turns the result
12694 * into a string and puts it in a capture slot. It always returns 1. If an
12695 * error occurs the action is cancelled, but the rule processing continues.
12696 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012697enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
12698 struct session *sess, struct stream *s)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012699{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012700 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012701 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012702 char **cap = s->res_cap;
12703 struct proxy *fe = strm_fe(s);
12704 int len;
12705 int i;
12706
12707 /* Look for the original configuration. */
12708 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012709 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012710 i--, h = h->next);
12711 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012712 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012713
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012714 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 +020012715 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012716 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012717
12718 if (cap[h->index] == NULL)
12719 cap[h->index] = pool_alloc2(h->pool);
12720
12721 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012722 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012723
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012724 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012725 if (len > h->len)
12726 len = h->len;
12727
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012728 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012729 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012730 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012731}
12732
12733/* parse an "http-response capture" action. It takes a single argument which is
12734 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12735 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012736 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012737 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012738enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12739 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012740{
12741 struct sample_expr *expr;
12742 int cur_arg;
12743 int id;
12744 char *error;
12745
12746 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12747 if (strcmp(args[cur_arg], "if") == 0 ||
12748 strcmp(args[cur_arg], "unless") == 0)
12749 break;
12750
12751 if (cur_arg < *orig_arg + 3) {
12752 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012753 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012754 }
12755
12756 cur_arg = *orig_arg;
12757 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12758 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012759 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012760
12761 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12762 memprintf(err,
12763 "fetch method '%s' extracts information from '%s', none of which is available here",
12764 args[cur_arg-1], sample_src_names(expr->fetch->use));
12765 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012766 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012767 }
12768
12769 if (!args[cur_arg] || !*args[cur_arg]) {
12770 memprintf(err, "expects 'len or 'id'");
12771 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012772 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012773 }
12774
12775 if (strcmp(args[cur_arg], "id") != 0) {
12776 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12777 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012778 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012779 }
12780
12781 cur_arg++;
12782
12783 if (!args[cur_arg]) {
12784 memprintf(err, "missing id value");
12785 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012786 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012787 }
12788
12789 id = strtol(args[cur_arg], &error, 10);
12790 if (*error != '\0') {
12791 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12792 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012793 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012794 }
12795 cur_arg++;
12796
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012797 proxy->conf.args.ctx = ARGC_CAP;
12798
Thierry FOURNIER42148732015-09-02 17:17:33 +020012799 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012800 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012801 rule->arg.capid.expr = expr;
12802 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012803
12804 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012805 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012806}
12807
William Lallemand73025dd2014-04-24 14:38:37 +020012808/*
12809 * Return the struct http_req_action_kw associated to a keyword.
12810 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012811struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012812{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012813 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012814}
12815
12816/*
12817 * Return the struct http_res_action_kw associated to a keyword.
12818 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012819struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012820{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012821 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012822}
12823
Willy Tarreau4a568972010-05-12 08:08:50 +020012824/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012825/* All supported ACL keywords must be declared here. */
12826/************************************************************************/
12827
12828/* Note: must not be declared <const> as its list will be overwritten.
12829 * Please take care of keeping this list alphabetically sorted.
12830 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012831static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012832 { "base", "base", PAT_MATCH_STR },
12833 { "base_beg", "base", PAT_MATCH_BEG },
12834 { "base_dir", "base", PAT_MATCH_DIR },
12835 { "base_dom", "base", PAT_MATCH_DOM },
12836 { "base_end", "base", PAT_MATCH_END },
12837 { "base_len", "base", PAT_MATCH_LEN },
12838 { "base_reg", "base", PAT_MATCH_REG },
12839 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012840
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012841 { "cook", "req.cook", PAT_MATCH_STR },
12842 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12843 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12844 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12845 { "cook_end", "req.cook", PAT_MATCH_END },
12846 { "cook_len", "req.cook", PAT_MATCH_LEN },
12847 { "cook_reg", "req.cook", PAT_MATCH_REG },
12848 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012849
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012850 { "hdr", "req.hdr", PAT_MATCH_STR },
12851 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12852 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12853 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12854 { "hdr_end", "req.hdr", PAT_MATCH_END },
12855 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12856 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12857 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012858
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012859 /* these two declarations uses strings with list storage (in place
12860 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12861 * and delete functions are relative to the list management. The parse
12862 * and match method are related to the corresponding fetch methods. This
12863 * is very particular ACL declaration mode.
12864 */
12865 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12866 { "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 +020012867
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012868 { "path", "path", PAT_MATCH_STR },
12869 { "path_beg", "path", PAT_MATCH_BEG },
12870 { "path_dir", "path", PAT_MATCH_DIR },
12871 { "path_dom", "path", PAT_MATCH_DOM },
12872 { "path_end", "path", PAT_MATCH_END },
12873 { "path_len", "path", PAT_MATCH_LEN },
12874 { "path_reg", "path", PAT_MATCH_REG },
12875 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012876
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012877 { "req_ver", "req.ver", PAT_MATCH_STR },
12878 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012879
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012880 { "scook", "res.cook", PAT_MATCH_STR },
12881 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12882 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12883 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12884 { "scook_end", "res.cook", PAT_MATCH_END },
12885 { "scook_len", "res.cook", PAT_MATCH_LEN },
12886 { "scook_reg", "res.cook", PAT_MATCH_REG },
12887 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012888
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012889 { "shdr", "res.hdr", PAT_MATCH_STR },
12890 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12891 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12892 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12893 { "shdr_end", "res.hdr", PAT_MATCH_END },
12894 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12895 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12896 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012897
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012898 { "url", "url", PAT_MATCH_STR },
12899 { "url_beg", "url", PAT_MATCH_BEG },
12900 { "url_dir", "url", PAT_MATCH_DIR },
12901 { "url_dom", "url", PAT_MATCH_DOM },
12902 { "url_end", "url", PAT_MATCH_END },
12903 { "url_len", "url", PAT_MATCH_LEN },
12904 { "url_reg", "url", PAT_MATCH_REG },
12905 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012906
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012907 { "urlp", "urlp", PAT_MATCH_STR },
12908 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12909 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12910 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12911 { "urlp_end", "urlp", PAT_MATCH_END },
12912 { "urlp_len", "urlp", PAT_MATCH_LEN },
12913 { "urlp_reg", "urlp", PAT_MATCH_REG },
12914 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012915
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012916 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012917}};
12918
12919/************************************************************************/
12920/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012921/************************************************************************/
12922/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012923static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012924 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012925 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012926 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12927
Willy Tarreau87b09662015-04-03 00:22:06 +020012928 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012929 { "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 +020012930
12931 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012932 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12933 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12934 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012935
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012936 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12937 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012938
Willy Tarreau409bcde2013-01-08 00:31:00 +010012939 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12940 * are only here to match the ACL's name, are request-only and are used
12941 * for ACL compatibility only.
12942 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012943 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12944 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012945 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12946 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012947
12948 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12949 * only here to match the ACL's name, are request-only and are used for
12950 * ACL compatibility only.
12951 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012952 { "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 +020012953 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012954 { "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 +020012955 { "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 +010012956
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012957 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012958 { "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 +010012959 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012960 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012961 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012962 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012963
12964 /* HTTP protocol on the request path */
12965 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012966 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012967
12968 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012969 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12970 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012971
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012972 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012973 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12974 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012975 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012976
Willy Tarreau18ed2562013-01-14 15:56:36 +010012977 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012978 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12979 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012980
Willy Tarreau18ed2562013-01-14 15:56:36 +010012981 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012982 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012983 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12984 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012985
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012986 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012987 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012988 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012989 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012990 { "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 +010012991 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012992 { "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 +010012993
12994 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012995 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012996 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12997 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012998
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012999 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013000 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013001 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013002 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013003 { "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 +010013004 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013005 { "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 +010013006
Willy Tarreau409bcde2013-01-08 00:31:00 +010013007 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013008 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013009 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13010 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013011 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013012
13013 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013014 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013015 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013016 { "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 +020013017 { "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 +010013018
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013019 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013020 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013021 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013022 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013023 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013024 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013025 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13026 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013027 { "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 +010013028 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013029}};
13030
Willy Tarreau8797c062007-05-07 00:55:35 +020013031
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013032/************************************************************************/
13033/* All supported converter keywords must be declared here. */
13034/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013035/* Note: must not be declared <const> as its list will be overwritten */
13036static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013037 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013038 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013039 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13040 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013041 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013042 { NULL, NULL, 0, 0, 0 },
13043}};
13044
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013045
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013046/************************************************************************/
13047/* All supported http-request action keywords must be declared here. */
13048/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013049struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013050 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013051 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013052 { "set-method", parse_set_req_line },
13053 { "set-path", parse_set_req_line },
13054 { "set-query", parse_set_req_line },
13055 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013056 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013057 }
13058};
13059
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013060struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013061 .kw = {
13062 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013063 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013064 { NULL, NULL }
13065 }
13066};
13067
Willy Tarreau8797c062007-05-07 00:55:35 +020013068__attribute__((constructor))
13069static void __http_protocol_init(void)
13070{
13071 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013072 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013073 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013074 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013075 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020013076}
13077
13078
Willy Tarreau58f10d72006-12-04 02:26:12 +010013079/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013080 * Local variables:
13081 * c-indent-level: 8
13082 * c-basic-offset: 8
13083 * End:
13084 */